简体   繁体   English

如何从shell启动和停止Python脚本

[英]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.

相关问题 shell启动/停止python脚本 - shell start / stop for python script 如何从 python 脚本启动 shell - How to start a shell from a python script 如何从python脚本运行交互式docker shell - How can I run interactive docker shell from python script 如何从python停止shell脚本(运行无限循环)? - How to stop a shell script (running a infinite loop ) from python? 如何将此Shell脚本转换为python? - How can I translate this shell script to python? 我可以在当前shell中从python脚本运行shell命令吗 - Can I run a shell command from python script in the current shell 如何在 selenium 脚本在 python 中运行时停止它,然后从头开始重新启动它? - How do I stop a selenium script while it's running in python and then start it again from the begining? 如何在Django Web应用程序中的Linux Shell上启动python子进程命令? - How can I start a python subprocess command on linux shell from within Django web app? 从 shell 脚本执行(服务 ssh 开始)有效,但是当我从 python 调用脚本时,它不起作用 - executing (service ssh start) from shell script works, but when I call the script from python, it doesnt work 如何启动python shell并使用某些命令自动初始化它? - How can I start the python shell and automatically initialize it with some commands?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM