Linux / Python · 2020年9月16日 0

supervisor守护进程,linux系统运维必备

我们的linux服务器上都会跑很多进程,有的是以服务的形式启动,有的是启动系统后自动启动的,但是在运行过程中会因为某些错误而退出,这时候我们就需要一个守护进程用来检查这些服务是否启动了,当然你可以自己的写个定时脚本定时检查,但是更好的方式是使用supervisor来守护他们。

supervisor是python写的,所以需要python环境。

现在的系统都带着python,如果没有的话需要先安装一下,建议使用python3.7.x

有了python,就可以直接通过pip安装,我这边使用的一般是centos

yum install python-setuptools
pip install supervisor

安装好后,配置文件的目录为/etc/supervisord/

echo_supervisord_conf > supervisord.conf
cp supervisord.conf /etc/supervisord.conf
mkdir /etc/supervisord.d/
mkdir -p /var/log/supervisor

然后编辑配置文件vi /etc/supervisord.conf

在文件中修改include,/etc/supervisord.d/*.conf

然后mkdir /etc/supervisord.d建立目录

vim /etc/supervisord.d/*.conf
[program:这里起个名字]
command=python /html/*.py -c /html/*.conf
autorestart=true
user=root
redirect_stderr=true          ; redirect proc stderr to stdout (default false)
stdout_logfile = /var/log/supervisor/name.log
stderr_logfile = /var/log/supervisor/name-err.log
stdout_logfile_maxbytes=50MB   ; max # logfile bytes b4 rotation (default 50MB)
stdout_logfile_backups=10     ; # of stdout logfile backups (default 10)
;stderr_logfile=/a/path        ; stderr log path, NONE for none; default AUTO
;stderr_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
;stderr_logfile_backups=10     ; # of stderr logfile backups (default 10)
;stderr_capture_maxbytes=1MB   ; number of bytes in 'capturemode' (default 0)

然后启动即可

systemctl enable supervisord

systemctl start supervisord

可以添加任意数量的被守护进程,还可以打开web管理页面,非常方便。