且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-12-05 09:35:28

我喜欢使用类似于以下内容的shell脚本。它很简单,并且具有低的错误可能性。一般来说,我把它放在一个while循环,睡眠结束,但cron也很好。

I like to do this with a shell script similar to the following. 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.

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

正如您所看到的,它需要您的进程在启动时将其PID写入文件。这避免了greps ps输出的问题,因为得到一个正则表达式可能是棘手的,它总是工作,无论什么新的进程名称在未来进入您的服务器。 PID文件是死的确定。

As you can see it requires your process to write its PID into a file when it starts up. 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. The PID file is dead certain.

kill -0 是一个已知的方法来检查进程是否正在运行并且比检查 / proc 中的目录更简单。 pid = 1 的行是存在的,因为检查器不是root,因此如果您无法向进程发送信号,则kill检查将总是失败。这是覆盖第一次myproc在此服务器上运行。注意,这意味着检查脚本不能以 root (通常是良好做法)运行,而必须作为运行正在检查的进程的同一个用户运行。

kill -0 is a little known way of checking if a process is running and is simpler than checking for a directory in /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. That is there to cover the first time that myproc is run on this server. 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.

如果你真的想在Python中这样做,那么 os 模块可以访问PID,并且有一个 kill method。但它不返回一个shell退出代码,而是 os.kill throws OSError 异常,你可以在 try - catch 同样的原理适用于使用信号0来检测进程是否存在,但是有一个很小的机会,你的进程已经死了,另一个进程现在有相同的pid,所以在 try中处理这种情况 - catch

If you really want to do this in Python, then the os module has access to PIDs and has a kill method. However it doesn't return a shell exitcode, instead os.kill throws OSError exceptions which you can handle in a try - catch block. 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.