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

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

问题描述

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

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循环,睡眠结束,但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.

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

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