以守护某一进程为例 :whistle_capture

环境:ubuntu 
运行Supervisor需要root权限 

 

1 安装Supervisor

apt-get install supervisor

安装完成后我们可以在/etc/supervisor/找到Supervisor

2.vi /etc/supervisor/supervisord.conf 
配置文件中注释是用冒号注释;

[include] 
files = /etc/supervisor/conf.d/*.conf 
我们在[include]后面添加新program

program相关参数说明:

[program:nginx] nginx为我们定义进程名, 
directory=/home/ubuntu/webvideo/nginx 待守护程序所在根目录 
command=/home/ubuntu/webvideo/nginx/sbin/nginx -g ‘daemon off;’ 启动程序命令 
autostart =true 在 supervisord 启动的时候也自动启动 
startsecs=5 启动 5 秒后没有异常退出,就当作已经正常启动了 
autorestart =true 程序异常退出后自动重启 
startretries=3 启动失败自动重试次数,默认是 3 
user=ubuntu 用哪个用户启动 
redirect_stderr=true 把 stderr 重定向到 stdout,默认 false,用于保存程序输出作为log 
stdout_logfile_maxbytes=20MB stdout 日志文件大小,默认 50MB 
stdout_logfile_backups=20 stdout 日志文件备份数,即一个log写满后会接这开一个log往下写 
stdout_logfile =/home/ubuntu/webvideo/nginx/logs/nginx_stdout.loglog存储路径
 

[program:whistle_capture]
directory=/home/V3/build/
command=/home/V3/build/whistle_capture
autostart=true
startsecs=3
autorestart=true
startretries=3
user=root
redirect_stderr=true
stdout_logfile_maxbytes=20MB
stdout_logfile_backups=20
stdout_logfile = /home/V3/nginx_stdout.log

3.更新在编辑好的配置文件

#supervisorctl reload

启动supervisord   

sudo supervisord -c /etc/supervisor/supervisord.conf

4.supervisord 常用操作

supervisorctl status          #查看守护程序状态
supervisorctl stop whistle_capture #停止nginx
supervisorctl start whistle_capture #启动nginx
supervisorctl restart whistle_capture #重启 nginx 程序
supervisorctl reread #读取有更新(增加)的配置文件,但不会启动新添加的程序
supervisorctl update #重启配置文件修改过的程序

5 supervisord 进程状态

STOPPED (0) 进程已停止
STARTING (10) 该进程由于启动请求而开始。
RUNNING (20) 该过程正在运行。
BACKOFF (30)该过程进入“ 启动”状态,但随后退出的速度太快而无法移至“ 运行”状态。 前面有startsecs 这个参数设定
STOPPING (40) 由于停止请求,该进程正在停止。
EXITED (100)该进程从RUNNING状态退出(预期或意外)。
FATAL (200)该过程无法成功启动。
UNKNOWN (1000)该进程处于未知状态(supervisord 编程错误)。

错误解决方法:

Supervisor on Debian Wheezy: another program is already listening on a port that one of our HTTP ser

 

解决办法

Terminal上输入

ps -ef | grep supervisord
  • 1

获取所有supervisord正在运行的pid

root   2503  1  0 Nov19 ?  00:03:23 /usr/bin/python /usr/bin/supervisord
root 21337 2556 0 18:15 pts/8 00:00:00 grep --color=auto supervisord
  • 1
  • 2

pid=2503

kill -s SIGTERM 2503  

增加到开机自启动项

#vim /etc/rc.local

sleep 3
/usr/src/MacTcp/IpCfg enp2s0 MZ &
insmod /home/driver/dvrs_hw.ko
#cd /home/V3/build
#/home/V3/build/whistle_capture > /dev/null &
#/home/V3/build/whistle_capture >> /root/whistle_capture.log &

cd /etc/openvpn
openvpn /etc/openvpn/client.conf > /dev/null &

sudo supervisord -c /etc/supervisor/supervisord.conf
exit 0
~
错误2:Error: Another program is already listening on a port that one of our HTTP servers is configured to use. Shut this program down first before starting

解决方法:

find / -name supervisor.sock

unlink /name/supervisor.sock