php使用supervisor管理进程脚本

supervisor是用python开发的一个在linux系统下的进程管理工具,能够方便的监听,启动,中止一个或多个进程。当一个进程被意外杀死后,supervisor监听到后,会自动从新拉起进程。php

1、supervisor的安装python

一、经过easy_install安装linux

yum -y install python-setuptools
easy_install supervisor

二、经过yum安装web

yum -y install supervisor

三、经过pip安装socket

yum -y install epel-release
yum -y install python-pip
pip install supervisor

安装好后,会生成三个执行命令,echo_supervisord_conf,supervisorctl,supervisord。tcp

 

2、supervisor的配置文件工具

supervisor的默认配置文件在 /etc/supervisord.conf 下,若是没有能够经过以下命令生成url

echo_supervisord_conf > /etc/supervisord.conf

经常使用的配置项以下:debug

[unix_http_server]
file=/tmp/supervisor.sock   ; unix socket文件,supervisorctl会使用
;chmod=0700                 ; socket文件权限
;chown=nobody:nogroup       ; socket文件所属用户和用户组

[inet_http_server]          ; web管理界面
port=127.0.0.1:9001         ; 管理界面的IP和端口
username=admin              ; 登录管理界面的用户名
password=123456             ; 登录管理界面的密码

[supervisord]
logfile=/tmp/supervisord.log ; 日志文件
logfile_maxbytes=50MB        ; 日志文件大小,为0表示不限制
logfile_backups=10           ; 日志文件备份数量,为0表示不备份
loglevel=info                ; 日志级别,也可设置为 debug,warn,trace
pidfile=/tmp/supervisord.pid ; PID文件路径
nodaemon=false               ; 是否前台启动,为false表示守护进程方式
minfds=1024                  ; 打开文件描述符的最小值
minprocs=200                 ; 建立进程数的最小值

[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ; 经过 unix sokcet 链接supervisord
;serverurl=http://127.0.0.1:9001 ; 经过http方式链接supervisord

[include]
files = /etc/supervisord/confs/*.conf ; 包含其余配置文件,能够是.conf或.ini

咱们须要把 [include] 前面的注释打开,并配置 files 的路径。unix

建立 files 中配置的目录。

mkdir -p /etc/supervisord/confs/

  

3、配置一个php脚本进程

咱们在 /etc/supervisord/confs/ 目录下建立一个 demo.conf 文件。

;demo表示程序名称
[program:demo]
;须要执行的命令
command=php demo.php
;命令执行的目录
directory=/data/wwwroot
;环境变量
environment=PATH="/data/nmp/php/bin/"
;哪一个用户运行
user=root
;是否自启动
autostart=true
;是否自动重启
autorestart=true
;自动重启时间间隔,单位秒
startsecs=3
;错误日志文件
stderr_logfile=/tmp/demo.err.log
;输出日志文件
stdout_logfile=/tmp/demo.out.log

demo.php的代码以下:

<?php

$i = 0;
while(true) {
    $i++;
    echo $i, PHP_EOL;
    sleep(1);
}

  

4、管理进程。

管理进程,须要咱们启动 supervisor 服务,这里咱们配置 systemctl,开机自动启动 supervisor。

建立 /usr/lib/systemd/system/supervisord.service 文件,配置以下:

[Unit]
Description=Supervisor daemon

[Service]
Type=forking
ExecStart=/usr/bin/supervisord
ExecStop=/usr/bin/supervisorctl $OPTIONS shutdown
ExecReload=/usr/bin/supervisorctl $OPTIONS reload
KillMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target

启用配置

systemctl enable supervisord.service

启动 supervisord

systemctl start supervisord.service

成功后,就能够经过 supervisorctl 交互命令管理进程脚本了。

读取有更新的配置文件

supervisorctl reread

更新配置文件修改过的程序

supervisorctl update

若是修改过 /etc/supervisord.conf 请使用以下命令

supervisorctl reload

启动,中止,重启,程序。

supervisorctl start 程序名
supervisorctl stop 程序名
supervisorctl restart 程序名

  

5、supervisor图形化管理界面

须要开启 /etc/supervisord.conf 文件中的 [inet_http_server]

[inet_http_server]
port=0.0.0.0:9001
username=admin
password=123456

设置完后,要开放 9001 端口,并重启 supervisor

firewall-cmd --zone=public --add-port=9001/tcp --permanent
firewall-cmd --reload

重启 supervisor

supervisorctl reload
相关文章
相关标签/搜索