Python自动重启系统

发布于 2024-07-20 09:23:36 字数 252 浏览 5 评论 0原文

我需要使用 python 检测程序何时崩溃或未运行并重新启动它。 我需要一种不一定依赖于 python 模块作为父进程的方法。

我正在考虑实现一个 while 循环,该循环本质上是这样的

ps -ef | grep process name

,当找不到该进程时,它会启动另一个循环。 也许这不是最有效的方法。 我是 python 新手,所以可能已经有一个 python 模块可以做到这一点。

I need to detect when a program crashes or is not running using python and restart it. I need a method that doesn't necessarily rely on the python module being the parent process.

I'm considering implementing a while loop that essentially does

ps -ef | grep process name

and when the process isn't found it starts another. Perhaps this isn't the most efficient method. I'm new to python so possibly there is a python module that does this already.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

国粹 2024-07-27 09:23:36

为什么要自己实现呢? 现有的实用程序,例如 daemon 或 Debian 的 start-stop-daemon 更有可能获得关于运行长期存在的服务器进程的其他困难的事情。

无论如何,当您启动服务时,将其 pid 放入 /var/run/.pid 中,然后使您的 ps 命令仅查找该进程 ID,并检查它是否是正确的过程。 在 Linux 上,您只需查看 /proc//exe 即可检查它是否指向正确的可执行文件。

Why implement it yourself? An existing utility like daemon or Debian's start-stop-daemon is more likely to get the other difficult stuff right about running long-living server processes.

Anyway, when you start the service, put its pid in /var/run/<name>.pid and then make your ps command just look for that process ID, and check that it is the right process. On Linux you can simply look at /proc/<pid>/exe to check that it points to the right executable.

梦言归人 2024-07-27 09:23:36

请不要重新发明 init. 您的操作系统有能力做到这一点,几乎不需要任何系统资源,并且肯定会比您可以重现的任何东西做得更好、更可靠。

Classic Linux 有 /etc/inittab

Ubuntu 有 /etc/event.d (upstart)

OS X 已经 launchd

Solaris 有 smf

Please don't reinvent init. Your OS has capabilities to do this that require nearly no system resources and will definitely do it better and more reliably than anything you can reproduce.

Classic Linux has /etc/inittab

Ubuntu has /etc/event.d (upstart)

OS X has launchd

Solaris has smf

百思不得你姐 2024-07-27 09:23:36

以下代码在给定的时间间隔内检查给定的进程,并重新启动它。

#Restarts a given process if it is finished.
#Compatible with Python 2.5, tested on Windows XP.
import threading
import time
import subprocess

class ProcessChecker(threading.Thread):
    def __init__(self, process_path, check_interval):
        threading.Thread.__init__(self)
        self.process_path = process_path
        self.check_interval = check_interval

    def run (self):
        while(1):
            time.sleep(self.check_interval)
            if self.is_ok():
                self.make_sure_process_is_running()

    def is_ok(self):
        ok = True
        #do the database locks, client data corruption check here,
        #and return true/false
        return ok

    def make_sure_process_is_running(self):
        #This call is blocking, it will wait for the
        #other sub process to be finished.
        retval = subprocess.call(self.process_path)

def main():
    process_path = "notepad.exe"
    check_interval = 1 #In seconds
    pm = ProcessChecker(process_path, check_interval)
    pm.start()
    print "Checker started..."

if __name__ == "__main__":
    main()

The following code checks a given process in a given interval, and restarts it.

#Restarts a given process if it is finished.
#Compatible with Python 2.5, tested on Windows XP.
import threading
import time
import subprocess

class ProcessChecker(threading.Thread):
    def __init__(self, process_path, check_interval):
        threading.Thread.__init__(self)
        self.process_path = process_path
        self.check_interval = check_interval

    def run (self):
        while(1):
            time.sleep(self.check_interval)
            if self.is_ok():
                self.make_sure_process_is_running()

    def is_ok(self):
        ok = True
        #do the database locks, client data corruption check here,
        #and return true/false
        return ok

    def make_sure_process_is_running(self):
        #This call is blocking, it will wait for the
        #other sub process to be finished.
        retval = subprocess.call(self.process_path)

def main():
    process_path = "notepad.exe"
    check_interval = 1 #In seconds
    pm = ProcessChecker(process_path, check_interval)
    pm.start()
    print "Checker started..."

if __name__ == "__main__":
    main()
梦年海沫深 2024-07-27 09:23:36

我自己没有尝试过,但是有一个 Python System Information 模块可以使用查找进程并获取有关它们的信息。 AFAIR 有一个 ProcessTable 类可用于检查正在运行的进程,但它似乎没有很好的文档记录......

I haven't tried it myself, but there is a Python System Information module that can be used to find processes and get information about them. AFAIR there is a ProcessTable class that can be used to inspect the running processes, but it doesn't seem to be very well documented...

李不 2024-07-27 09:23:36

我会采用命令行路线(恕我直言,这更容易),只要您只每隔一两秒检查一次,与任何不到 10 年历史的系统上的可用处理相比,资源使用量应该是无限小的。

I'd go the command-line route (it's just easier imho) as long as you only check every second or two the resource usage should be infintesimal compared to the available processing on any system less than 10 years old.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文