简体   繁体   English

终止正在运行的Python应用程序

[英]Terminate Running Python Apps

I have a Raspberry Pi running Raspbian controlling a home automation system as part of a project for college. 我有一个运行Raspbian的Raspberry Pi,它控制一个家庭自动化系统,这是一个大学项目的一部分。 To control this I'm using an ASP.NET web app to fire SSH commands at the Pi to start various Python apps. 为了控制这一点,我使用ASP.NET Web应用程序在Pi上触发SSH命令以启动各种Python应用程序。 I need a way to terminate another app over SSH before starting a new one. 我需要一种在启动新应用之前通过SSH终止其他应用的方法。

For example: 例如:

a.py and b.py are running a.py和b.py正在运行

User selects c.py from the web app 用户从网络应用中选择c.py

a.py must be stopped before starting c.py leaving b.py and c.py running. 在启动c.py之前,必须先停止a.py,然后再运行b.py和c.py。

Thanks 谢谢

Jake 杰克

find the PID (Process ID) of your python script using the command (should be the second column) 使用命令查找python脚本的PID(进程ID)(应为第二列)

ps -ef | grep python
kill <PID found previously>
kill -9 <PID found previously>

If the program is still running then it means you are using the wrong PID Nothing can stop a kill -9 command :-) 如果程序仍在运行,则表示您使用了错误的PID。Nothing无法停止kill -9命令:-)

If you want to kill every running instance of python: 如果要杀死每个正在运行的python实例:

$ kill `pidof python`

If you want to kill every running instance of a specific python script: 如果要杀死特定python脚本的每个正在运行的实例,请执行以下操作:

$ kill `pidof -x myscript.py`
or
$ pkill myscript.py
or
$ killall glances

It's generally not advisable to send SIGKILL to a running program ( kill -9 ). 通常不建议将SIGKILL发送到正在运行的程序( kill -9 )。 SIGTERM is usually sufficient unless the program is frozen. 除非程序被冻结,否则SIGTERM通常就足够了。 All the above commands send SIGTERM. 以上所有命令都会发送SIGTERM。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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