[英]How to send command to python script?
I have a python script running in background via nohup. 我有一个通过nohup在后台运行的python脚本。
It is an alarm system. 这是一个警报系统。 Once I run the script it checks if a motion is detected.
一旦运行脚本,它就会检查是否检测到运动。 Now i have the problem of how to disable the alarm.
现在我有如何禁用警报的问题。
Could you help me to find a good way to send a command to disable/kill the running script? 您能帮我找到一种发送命令来禁用/终止正在运行的脚本的好方法吗?
Quick and dirty method was: 快速而肮脏的方法是:
Checking a txt file content: if I want to shut down the script, I modify the txt. 检查txt文件的内容:如果要关闭脚本,请修改txt。
Something like: 就像是:
if txt content is "disable":
quit
I am pretty new to python maybe you can help me. 我对python很陌生,也许您可以帮助我。
If you're using linux/unix/macos, you can kill the script from the command line 如果您使用的是linux / unix / macos,则可以从命令行终止脚本
$ ps aux | grep python
nate 16210 0.2 0.0 20156 5176 pts/2 S+ 17:02 0:00 python3
nate 16215 0.0 0.0 8044 932 pts/1 S+ 17:02 0:00 grep python
The second field is the PID of the running process, kill it with the kill command: 第二个字段是正在运行的进程的PID,使用kill命令将其杀死:
$ kill 16210
$ ps aux | grep python
nate 16220 0.0 0.0 8044 932 pts/1 S+ 17:02 0:00 grep python
To grab the pid automatically, you can add this to the top of your python script: 要自动获取pid,可以将其添加到python脚本的顶部:
import os
pid = os.getpid()
with open('pid', 'w') as procfile:
procfile.write(pid)
Now, you can grab that pid with one line of bash: 现在,您可以使用一行bash来获取该pid:
kill -TERM $(cat pid)
replace pid
with the location of the file pid
替代
pid
与文件的位置pid
if os.path.exists(os.path.expanduser("~/disable_alarm.txt")):
#then dont sound alarm
and just put a file in your home directoy when you want it disabled ... 并在禁用时将文件放在您的家庭目录中...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.