如何用 Python 杀死 Linux 进程 [英] How to kill Linux process with Python

查看:96
本文介绍了如何用 Python 杀死 Linux 进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 2 个功能自动从串口捕获日志:

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

1) 触发开始捕获

2) 触发停止

乍一看

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)

它可以工作,但我想知道如何在不手动按 Ctrl+C 的情况下停止此过程

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

推荐答案

如果你用

child = subprocess.Popen("command")

然后你可以打电话

child.terminate()
child.kill()

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆