简体   繁体   English

看门狗用于特定的python进程

[英]Watchdog for specific python process

I am working on Ubuntu 16.04 and I have a python running process in the background 我正在使用Ubuntu 16.04,后台有python运行进程

python myFunction.py

From time to time, myFunction process gets killed for unknown reason, however, I want to restart it automatically. 有时, myFunction进程会由于未知原因而被杀死,但是,我想自动重新启动它。 I have multiple python process running in the background, and I do not know which one runs myFunctions.py (eg by using the pgrep command). 我在后台运行多个python进程,但我不知道哪个进程运行myFunctions.py (例如,通过使用pgrep命令)。

Is it possible? 可能吗? Can I make a bash or python script to restart the command python myFunction.py whenever the python process running it gets killed? 每当运行它的python进程被杀死时,我可以制作bash或python脚本来重新启动命令python myFunction.py吗?

You can look at Supervisord which is (from its own documentation) : 您可以查看Supervisord ,它是(从其自己的文档中):

a client/server system that allows its users to monitor and control a number of processes on UNIX-like operating systems 允许其用户监视和控制类似UNIX的操作系统上的多个进程的客户机/服务器系统

Supervisord will keep your script in check. Supervisord将检查您的脚本。 If it crashes, it will restart it again. 如果崩溃,它将再次重新启动。 If your raspberry reboots it will make sure the scripts starts automically after booting. 如果您的树莓重新启动,将确保启动后脚本自动启动。

It works based on a config file formatted like this (more info in the docs ) : 它基于这样的配置文件工作(在docs中有更多信息):

[program:myFunction]
command=/path_to_script/myFunction.py
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/var/log/myFunction.log
stderr_logfile=/var/log/myFunction.error.log
directory=/path_to_script

I hope this will help you 我希望这能帮到您

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM