[英]How can I start and stop a Python script from shell
thanks for helping! 谢谢你的帮助!
I want to start and stop a Python script from a shell script. 我想从shell脚本启动和停止Python脚本。 The start works fine, but I want to stop / terminate the Python script after 10 seconds.
开始工作正常,但我想在10秒后停止/终止Python脚本。 (it's a counter that keeps counting).
(这是一个不断计数的计数器)。 bud is won't stop.... I think it is hanging on the first line.
芽是不会停止....我认为它挂在第一行。
What is the right way to start wait for 10 seconds en stop? 什么是开始等待10秒的正确方法?
Shell script: Shell脚本:
python /home/pi/count1.py
sleep 10
kill /home/pi/count1.py
It's not working yet. 它还没有工作。 I get the point of doing the script on the background.
我明白在后台编写脚本。 That's working!.
那工作! But I get another comment form my raspberry after doing:
但在做完后,我从树莓树上得到另一条评论:
python /home/pi/count1.py &
sleep 10; kill /home/pi/count1.py
/home/pi/sebastiaan.sh: line 19: kill: /home/pi/count1.py: arguments must be process or job IDs /home/pi/sebastiaan.sh:line 19:kill:/home/pi/count1.py:参数必须是进程或作业ID
It's got to be in the: (but what? Thanks for helping out!) 它必须在:(但是什么?感谢您的帮助!)
sleep 10; kill /home/pi/count1.py
You're right, the shell script "hangs" on the first line until the python script finishes. 你是对的,shell脚本在第一行“挂起”,直到python脚本完成。 If it doesn't, the shell script won't continue.
如果没有,则shell脚本将不会继续。 Therefore you have to use
&
at the end of the shell command to run it in the background. 因此,您必须在shell命令的末尾使用
&
在后台运行它。 This way, the python script starts and the shell script continues. 这样,python脚本启动,shell脚本继续。
The kill
command doesn't take a path, it takes a process id. kill
命令不带路径,它需要一个进程id。 After all, you might run the same program several times, and then try to kill the first, or last one. 毕竟,您可能会多次运行相同的程序,然后尝试杀死第一个或最后一个。
The bash shell supports the $!
bash shell支持
$!
variable, which is the pid of the last background process. 变量,这是最后一个后台进程的pid。
Your current example script is wrong, because it doesn't run the python job and the sleep job in parallel. 您当前的示例脚本是错误的,因为它不会并行运行python作业和睡眠作业。 Without adornment, the script will wait for the python job to finish, then sleep 10 seconds, then kill.
没有装饰,脚本将等待python作业完成,然后睡10秒,然后杀死。
What you probably want is something like: 你可能想要的是:
python myscript.py & # <-- Note '&' to run in background
LASTPID=$! # Save $! in case you do other background-y stuff
sleep 10; kill $LASTPID # Sleep then kill to set timeout.
You can terminate any process from any other if OS let you do it. 如果操作系统允许,您可以终止任何其他进程。 Ie if it isn't some critical process belonging to the OS itself.
即如果它不是属于操作系统本身的一些关键过程。
The command kill uses PID to kill the process, not the process's name or command. 命令kill使用PID来终止进程,而不是进程的名称或命令。
Use pkill for that. 使用pkill。
You can also, send it a different signal instead of SIGTERM (request to terminate a program) that you may wish to detect inside your Python application and respond to it. 您也可以发送一个不同的信号,而不是SIGTERM(请求终止程序),您可能希望在Python应用程序中检测并响应它。
For instance you may wish to check if the process is alive and get some data from it. 例如,您可能希望检查进程是否处于活动状态并从中获取一些数据。
To do this, choose one of the users custom signals and register them within your Python program using signal module. 为此,请选择一个用户自定义信号,并使用信号模块在Python程序中注册它们。
To see why your script hangs, see Austin's answer. 要查看脚本挂起的原因,请参阅Austin的答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.