【问题标题】:Constantly monitor a program/process using Python使用 Python 持续监控程序/进程
【发布时间】:2012-11-15 21:58:12
【问题描述】:

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

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

现在我运行另一个程序,该程序具有以下代码,以在 Linux 环境中监控程序 run_constantly.py

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的调度程序来调度程序。但是,我目前面临的主要问题是,一旦我使用scheduler.add_cron_job() 函数安排run_constantly.py,该程序的进程ID 和run_constantly.py 程序变成相同。所以如果程序run_constantly.py崩溃了,下面的程序仍然认为run_constantly.py正在运行(因为两个进程ID相同),因此继续进入else循环休眠并再次监控。

谁能告诉我如何解决这个问题?有没有一种简单的方法可以持续监控程序并在程序崩溃时重新安排它?

【问题讨论】:

    标签: python scheduling daemon cron-task


    【解决方案1】:

    有很多程序可以做到这一点。

    在 Ubuntu 上有 upstart(默认安装)

    很多人喜欢http://supervisord.org/

    @nathan 提到的monit

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

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

    选择实际上取决于您更喜欢哪一个,但您最好使用其中之一而不是自己编写。

    希望有帮助

    【讨论】:

    • 在supervisord上+1。我使用它,它非常简单且跨平台。
    • @Mark Lakewood 谢谢,Monit 似乎很有趣,尽管它缺乏适当的文档。它工作得非常整洁
    • 自己编写一个可以让我们扩展它以满足我们的需要(例如,记录数据以便以后可视化)
    【解决方案2】:

    如果你愿意直接从python而不是使用cron来控制被监控的程序,看看subprocess module

    The subprocess module allows you to spawn new processes,
    connect to their input/output/error pipes, and obtain their return codes.
    

    查看 SO 上的 track process status with python 等示例以获取示例和参考。

    【讨论】:

      【解决方案3】:

      你可以只使用监视器 http://mmonit.com/monit/

      它监视进程并重新启动它们(和其他东西。)

      【讨论】:

      • monit 可以做很多有用的思考。
      • 但无论如何,您的调度程序是如何运行应用程序的?从您的代码示例中不清楚。要解决 PID 问题,您已在系统中正确运行它。
      【解决方案4】:

      我想我会添加一个更通用的解决方案,这也是我个人一直使用的解决方案。

      它的名字是Immortal(来源是https://github.com/immortal/immortal

      要让它监控并在程序停止时立即重新启动,只需运行以下命令:

      immortal <command>
      

      所以在你的情况下,我会像这样运行run_constantly.py

      immortal python run_constantly.py
      

      命令ps aux | grep run_constantly.py 应该返回2 个进程ID,一个用于Immortal 命令,一个用于单独的命令Immortal 启动(只是常规命令。只要Immortal 进程正在运行,run_constantly.py将继续运行。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-11-27
        • 1970-01-01
        • 1970-01-01
        • 2016-02-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多