简体   繁体   中英

How to kill Linux process with Python

I want to automate capturing logs from serial port with 2 functions:

1) trigger to start capture

2) trigger to stop

First looks like

def start_capture_output():
  file = '/home/test/Desktop/log.txt'
  os.system('touch %s' % file)
  os.system('chmod +rwx %s' % file)
  os.system('cat </dev/ttyUSB0>%s' % file)

and it works, but I wonder how to stop this process without manually pressing Ctrl+C

If you spawn the process with

child = subprocess.Popen("command")

Then you can call

child.terminate()
child.kill()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM