[英]How to launch and kill a process (python script requiring sudo)
I'm making gpio scripts on my rpi and I want a core program to run and kill another python script. 我在我的rpi上制作gpio脚本,我想要一个核心程序来运行并杀死另一个python脚本。
I had some trouble to find an explanation on how to do this but finally went for this solution : How to terminate a python subprocess launched with shell=True . 我有一些麻烦找到如何做到这一点的解释,但终于找到了这个解决方案: 如何终止使用shell = True启动的python子进程 。 So my code is : 所以我的代码是:
# // on button pressed, turn on the lcd
spawnProcess = subprocess.Popen("python jukebox_lcd.py", shell=True, preexec_fn=os.setsid)
print "pid : ", os.getpgid(spawnProcess.pid)
# // off button pressed, turn off the lcd
os.killpg(os.getpgid(spawnProcess.pid), signal.SIGTERM)
It will return for example pid : 5030 but will not turn off the lcd... The process I need to kill is 5031 : 它将返回例如pid:5030但不会关闭液晶显示器...我需要杀死的过程是5031:
volumio@volumio:~$ ps -elf | grep python
4 S root 4991 2760 0 80 0 - 1307 - 23:11 pts/0 00:00:00 sudo python jukebox-buttons.py
4 S root 4995 4991 3 80 0 - 8352 - 23:11 pts/0 00:00:00 python jukebox-buttons.py
0 S root 5030 4995 0 80 0 - 456 - 23:11 ? 00:00:00 /bin/sh -c python /home/FTP/jukebox_lcd.py
0 S root 5031 5030 1 80 0 - 2062 - 23:11 ? 00:00:00 python /home/FTP/jukebox_lcd.py
0 S volumio 5035 3952 0 80 0 - 648 pipe_w 23:11 pts/1 00:00:00 grep python
I don't understand why my subprocess runs "/bin/sh -c python myscript" and how to fix this. 我不明白为什么我的子进程运行“/ bin / sh -c python myscript”以及如何解决这个问题。 Any help please? 有什么帮助吗?
spawnProcess = subprocess.Popen(['python', 'jukebox_lcd.py'], preexec_fn=os.setsid)
一切都很好,没关系:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.