使用Python持续监控程序/进程 [英] Constantly monitor a program/process using Python

查看:1260
本文介绍了使用Python持续监控程序/进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图不断监视基本上是Python程序的进程。如果程序停止,那么我必须再次启动该程序。我正在使用另一个Python程序来执行此操作。

I am trying to constantly monitor a process which is basically a Python program. If the program stops, then I have to start the program again. I am using another Python program to do so.

例如,说我必须不断运行名为 run_constantly.py 。我最初手动运行该程序,它将其进程ID写入文件 PID(在out / PROCESSID / PID中)。

For example, say I have to constantly run a process called run_constantly.py. I initially run this program manually, which writes its process ID to the file "PID" (in the location out/PROCESSID/PID).

现在我运行另一个具有以下代码的程序来监视程序 run_constantly.py 在Linux环境中:

Now I run another program which has the following code to monitor the program run_constantly.py from a Linux environment:

def Monitor_Periodic_Process():

    TIMER_RUNIN = 1800
    foo = imp.load_source("Run_Module","run_constantly.py")
    PROGRAM_TO_MONITOR = ['run_constantly.py','out/PROCESSID/PID']
    while(1):
        # call the function checkPID to see if the program is running or not
        res = checkPID(PROGRAM_TO_MONITOR)
        # if res is 0 then program is not running so schedule it
        if (res == 0):
            date_time = datetime.now()
            scheduler.add_cron_job(foo.Run_Module, year=date_time.year, day=date_time.day, month=date_time.month, hour=date_time.hour, minute=date_time.minute+2)
            scheduler.start()
            scheduler.get_jobs()
            time.sleep(TIMER_NOT_RUNIN)
            continue
        else:
            #the process is running sleep and then monitor again
            time.sleep(TIMER_RUNIN)
            continue

我这里没有包含 checkPID()函数。 checkPID()基本上检查进程ID是否仍然存在(即程序是否仍在运行),如果不存在,则返回 0 。在上面的程序中,我检查 res == 0 ,如果是,则使用Python的调度程序来调度程序。但是,我目前面临的主要问题是该程序的进程ID和 run_constantly.py 程序一旦我变成使用 scheduler.add_cron_job()函数安排 run_constantly.py 。因此,如果程序 run_constantly.py 崩溃,下面的程序仍会认为 run_constantly.py 正在运行(因为两者进程ID相同),因此继续进入else循环以再次睡眠并进行监视。

I have not included the checkPID() function here. checkPID() basically checks if the process ID still exists (i.e. if the program is still running) and if it does not exist, it returns 0. In the above program, I check if res == 0, and if so, then I use Python's scheduler to schedule the program. However, the major problem that I am currently facing is that the process ID of this program and the run_constantly.py program turns to be same once I schedule the run_constantly.py using the scheduler.add_cron_job() function. So if the program run_constantly.py crashes, the following program still thinks that the run_constantly.py is running (since both process IDs are same), and therefore continues to go into the else loop to sleep and monitor again.

有人可以告诉我如何解决此问题吗?有没有一种简单的方法可以持续监视程序并在发生崩溃时重新计划它?

Can someone tell me how to solve this issue? Is there a simple way to constantly monitor a program and reschedule it when it has crashed?

推荐答案

有很多程序可以执行

在Ubuntu上有 upstart (默认安装)

On Ubuntu there is upstart (installed by default)

很多人,例如 http://supervisord.org/

监视,如@nathan提到的

monit as mentioned by @nathan

如果您正在寻找python替代品,则有一个刚刚发布的名为 circus 的库,看起来很有趣

If you are looking for a python alternative there is a library that has just been released called circus which looks interesting.

几乎每个Linux发行版都可能内置了其中之一。

And pretty much every linux distro probably has one of these built in.

选择实际上只是缩小到您更喜欢的那一个,但是使用其中之一比您自己编写要好得多。

The choice is really just down to which one you like better, but you would be far better off using one of these than writing it yourself.

希望有帮助

这篇关于使用Python持续监控程序/进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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