简体   繁体   English

使用cron作业检查python脚本是否正在运行

[英]Using cron job to check if python script is running

I currently have a Python webserver application running and I would like to use a python script for cron job to check if the application is running, else start the application. 我目前有一个正在运行的Python Web服务器应用程序,我想对cron作业使用python脚本来检查该应用程序是否正在运行,否则请启动该应用程序。

I have the following code as below: 我有以下代码如下:

import os

string = os.popen('ps aux')
n = 0
for line in string.readlines():
  if "python webserver.py" in line:
    n+=1
if n < 1:
  os.system('python webserver.py')

Now the script has executed the application. 现在脚本已经执行了该应用程序。 But what I want is for the script to start the application and exit, leaving the application running. 但我想要的是脚本启动应用程序并退出,让应用程序继续运行。 Is there any ways to do so? 有没有办法这样做? Thanks in advance. 提前致谢。

I think it would be better if you made your webserver write its process id into a file and then have you cron job read that and check if the process is running. 我认为最好使Web服务器将其进程ID写入文件,然后让cron作业读取该文件并检查进程是否正在运行。

It would also be useful to use some kind of framework that does process management (eg supervisord, upstart etc.) rather than manually launch it using os.system . 使用某种进行过程管理的框架(例如,监督者,新贵等),而不是使用os.system手动启动它,也将os.system

I like to do this with a shell script similar to the following. 我喜欢使用类似于以下的shell脚本来执行此操作。 It is simple and has low likelihood of errors. 它很简单,错误的可能性很小。 Generally I put this in a while loop with sleep at the end, but cron works fine too. 一般来说,我把它放在一个while循环中,最后睡眠,但cron也能正常工作。

if [ -x myproc.pid ] ; then
    pid=`cat myproc.pid`
else
    pid=1
fi
kill -0 $pid #check if process is still running

As you can see it requires your process to write its PID into a file when it starts up. 正如您所看到的,它需要您的进程在启动时将其PID写入文件。 This avoids problems with grepping ps output because it can be tricky to get a regular expression that always works no matter what new process names come onto your server in future. 这避免了grepping ps输出的问题,因为无论将来什么新的进程名称进入您的服务器,获得一个始终有效的正则表达式可能会很棘手。 The PID file is dead certain. PID文件不确定。

kill -0 is a little known way of checking if a process is running and is simpler than checking for a directory in /proc . kill -0是检查进程是否正在运行的一种鲜为人知的方法,比检查/proc的目录更简单。 The line with pid=1 is there because the checker is not root therefore the kill check will always fail if you can't send signals to the process. 那里有pid=1的行,因为检查程序不是root用户,因此,如果您无法向进程发送信号,则终止检查将始终失败。 That is there to cover the first time that myproc is run on this server. 这是第一次在此服务器上运行myproc。 Note that this means that the checking script must not run as root (generally good practice) but must run as the same user which runs the process you are checking. 请注意,这意味着检查脚本不能以root身份运行(通常是好的做法),而必须以与您正在检查的进程相同的用户身份运行。

If you really want to do this in Python, then the os module has access to PIDs and has a kill method. 如果你真的想在Python中这样做,那么os模块可以访问PID并有一个kill方法。 However it doesn't return a shell exitcode, instead os.kill throws OSError exceptions which you can handle in a try - catch block. 但是,它不会返回shell os.kill ,而是os.kill抛出OSError异常,您可以在try - catch块中进行处理。 The same principle applies of using signal 0 to detect whether the process exists or not but there is a slim chance that your process has died and another process now has the same pid, so do handle that case in your try - catch . 使用信号0来检测该进程是否存在的原理相同,但是您的进程已经死亡并且另一个进程现在具有相同的pid的可能性很小,因此请在try - catch处理该情况。

You could probably do it by starting the application in the background and possibly making it immune to hang up signals. 您可以通过在后台启动该应用程序并使其免于挂断信号来做到这一点。

Try changing the system command to: 尝试将系统命令更改为:

nohup python webserver.py &

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

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