且构网

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

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

更新时间:2022-06-25 07:41:45

我喜欢使用类似于以下的 shell 脚本来做到这一点.它很简单,出错的可能性很小.通常我把它放在一个 while 循环中,最后是 sleep,但 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 写入文件.这避免了 grepping 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 方法.但是它不返回 shell 退出代码,而是 os.kill 抛出 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.