使用 cron 作业检查 python 脚本是否正在运行 [英] Using cron job to check if python script is running

查看:59
本文介绍了使用 cron 作业检查 python 脚本是否正在运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个 Python 网络服务器应用程序正在运行,我想使用 Python 脚本进行 cron 作业来检查应用程序是否正在运行,否则启动应用程序.

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.

我有以下代码:

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.

推荐答案

我喜欢使用类似于以下的 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.

这篇关于使用 cron 作业检查 python 脚本是否正在运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆