苟哥的笔记本
首页
文章归档
关于
文章归档
关于
首页
软件安装
正文
CentOS6.9安装Supervisor管理后台进程并设置开机启动
苟哥
2020-08-14 AM
3202℃
0条
> 本篇笔记记录了CentOS6.9中安装Supervisor管理后台进程,并将Supervisord加入服务,设置开机启动,通过service管理的过程 ### 创建并进入源码存放目录 ```shell mkdir -p /usr/local/src cd /usr/local/src ``` ### 下载并解压supervisor源码 ```shell wget -c https://files.pythonhosted.org/packages/ba/65/92575a8757ed576beaee59251f64a3287bde82bdc03964b89df9e1d29e1b/supervisor-3.3.5.tar.gz tar -zxvf supervisor-3.3.5.tar.gz cd supervisor-3.3.5 ``` ### 安装supervisor ```shell python setup.py install ``` 如果出现错误:ImportError: No module named setuptools 安装python-setuptoolsyum install python-setuptools 下面的结果表示supervisor安装成功 Installed /usr/lib/python2.6/site-packages/meld3-1.0.2-py2.6.egg Finished processing dependencies for supervisor==3.3.5 ### 创建supervisord.d目录 ```shell mkdir /etc/supervisord.d ``` ### 创建并编辑配置文件 ```shell echo_supervisord_conf > /etc/supervisord.conf vim /etc/supervisord.conf ``` 修改以下几处 ```shell file=/var/run/supervisor.sock logfile=/var/log/supervisord.log pidfile=/var/run/supervisord.pid serverurl=unix:///var/run/supervisor.sock [include] files = supervisord.d/*.conf ``` ### 启动supervisord ```shell supervisord -c /etc/supervisord.conf ``` 查看进程 ```shell ps aux | grep supervisord root 3110 0.0 1.0 196612 10816 ? Ss 20:12 0:00 /usr/bin/python /usr/bin/supervisord -c /etc/supervisord.conf root 3504 0.0 0.0 103336 908 pts/0 S+ 20:19 0:00 grep supervisord kill 3110 ``` ### 创建启动脚本 ```shell vim /etc/init.d/supervisord ``` 写入如下配置 ```shell #!/bin/bash # # supervisord This scripts turns supervisord on # # Author: Mike McGrath
(based off yumupdatesd) # Jason Koppe
adjusted to read sysconfig, # use supervisord tools to start/stop, conditionally wait # for child processes to shutdown, and startup later # Mikhail Mingalev
Merged # redhat-init-jkoppe and redhat-sysconfig-jkoppe, and # made the script "simple customizable". # Brendan Maguire
Added OPTIONS to # SUPERVISORCTL status call # # chkconfig: 345 83 04 # # description: supervisor is a process control utility. It has a web based # xmlrpc interface as well as a few other nifty features. # Script was originally written by Jason Koppe
. # # source function library . /etc/rc.d/init.d/functions set -a PREFIX=/usr SUPERVISORD=$PREFIX/bin/supervisord SUPERVISORCTL=$PREFIX/bin/supervisorctl PIDFILE=/var/run/supervisord.pid LOCKFILE=/var/lock/subsys/supervisord OPTIONS="-c /etc/supervisord.conf" # unset this variable if you don't care to wait for child processes to shutdown before removing the $LOCKFILE-lock WAIT_FOR_SUBPROCESSES=yes # remove this if you manage number of open files in some other fashion ulimit -n 96000 RETVAL=0 running_pid() { # Check if a given process pid's cmdline matches a given name pid=$1 name=$2 [ -z "$pid" ] && return 1 [ ! -d /proc/$pid ] && return 1 (cat /proc/$pid/cmdline | tr "\000" "\n"|grep -q $name) || return 1 return 0 } running() { # Check if the process is running looking at /proc # (works for all users) # No pidfile, probably no daemon present [ ! -f "$PIDFILE" ] && return 1 # Obtain the pid and check it against the binary name pid=`cat $PIDFILE` running_pid $pid $SUPERVISORD || return 1 return 0 } start() { echo "Starting supervisord: " if [ -e $PIDFILE ]; then echo "ALREADY STARTED" return 1 fi # start supervisord with options from sysconfig (stuff like -c) $SUPERVISORD $OPTIONS # show initial startup status $SUPERVISORCTL $OPTIONS status # only create the subsyslock if we created the PIDFILE [ -e $PIDFILE ] && touch $LOCKFILE } stop() { echo -n "Stopping supervisord: " $SUPERVISORCTL $OPTIONS shutdown if [ -n "$WAIT_FOR_SUBPROCESSES" ]; then echo "Waiting roughly 60 seconds for $PIDFILE to be removed after child processes exit" for sleep in 2 2 2 2 4 4 4 4 8 8 8 8 last; do if [ ! -e $PIDFILE ] ; then echo "Supervisord exited as expected in under $total_sleep seconds" break else if [[ $sleep -eq "last" ]] ; then echo "Supervisord still working on shutting down. We've waited roughly 60 seconds, we'll let it do its thing from here" return 1 else sleep $sleep total_sleep=$(( $total_sleep + $sleep )) fi fi done fi # always remove the subsys. We might have waited a while, but just remove it at this point. rm -f $LOCKFILE } restart() { stop start } case "$1" in start) start RETVAL=$? ;; stop) stop RETVAL=$? ;; restart|force-reload) restart RETVAL=$? ;; reload) $SUPERVISORCTL $OPTIONS reload RETVAL=$? ;; condrestart) [ -f $LOCKFILE ] && restart RETVAL=$? ;; status) $SUPERVISORCTL $OPTIONS status if running ; then RETVAL=0 else RETVAL=1 fi ;; *) echo $"Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}" exit 1 esac exit $RETVAL ``` ### 加入服务 ```shell chkconfig --add supervisord ``` ### 设置权限 ```shell chmod 755 /etc/init.d/supervisord ``` 开机启动 ```shell chkconfig supervisord on ``` ### 创建测试php文件 ```shell vim /www/test.php ``` 写入如下代码,让进程启动时,先休眠10秒,然后写入log后立即退出 ```php than 1 seconds (startsecs) 2019-01-27 21:45:48,022 INFO exited: jmsite_00 (exit status 0; expected) 2019-01-27 21:45:49,025 INFO spawned: 'jmsite_00' with pid 8105 2019-01-27 21:45:50,023 INFO exited: jmsite_01 (exit status 0; expected) 2019-01-27 21:45:51,026 INFO success: jmsite_00 entered RUNNING state, process has stayed up for > than 1 seconds (startsecs) 2019-01-27 21:45:51,027 INFO spawned: 'jmsite_01' with pid 8106 2019-01-27 21:45:51,045 INFO exited: jmsite_02 (exit status 0; expected) 2019-01-27 21:45:52,046 INFO success: jmsite_01 entered RUNNING state, process has stayed up for > than 1 seconds (startsecs) 2019-01-27 21:45:52,047 INFO spawned: 'jmsite_02' with pid 8107 2019-01-27 21:45:53,050 INFO success: jmsite_02 entered RUNNING state, process has stayed up for > than 1 seconds (startsecs) 2019-01-27 21:45:59,040 INFO exited: jmsite_00 (exit status 0; expected) 2019-01-27 21:46:00,043 INFO spawned: 'jmsite_00' with pid 8111 2019-01-27 21:46:01,043 INFO success: jmsite_00 entered RUNNING state, process has stayed up for > than 1 seconds (startsecs) 2019-01-27 21:46:01,045 INFO exited: jmsite_01 (exit status 0; expected) 2019-01-27 21:46:02,048 INFO spawned: 'jmsite_01' with pid 8112 2019-01-27 21:46:02,063 INFO exited: jmsite_02 (exit status 0; expected) 2019-01-27 21:46:03,064 INFO success: jmsite_01 entered RUNNING state, process has stayed up for > than 1 seconds (startsecs) 2019-01-27 21:46:03,065 INFO spawned: 'jmsite_02' with pid 8113 2019-01-27 21:46:04,067 INFO success: jmsite_02 entered RUNNING state, process has stayed up for > than 1 seconds (startsecs) ...... ``` 查看test.php产生的log ```php tail -f /var/log/test.php.log 2019-01-27 21:45:14:8052 2019-01-27 21:45:15:8053 2019-01-27 21:45:17:8087 2019-01-27 21:45:25:8088 2019-01-27 21:45:26:8089 2019-01-27 21:45:29:8092 2019-01-27 21:45:36:8093 2019-01-27 21:45:39:8096 2019-01-27 21:45:40:8097 2019-01-27 21:45:48:8100 2019-01-27 21:45:50:8101 2019-01-27 21:45:51:8102 2019-01-27 21:45:59:8105 2019-01-27 21:46:01:8106 2019-01-27 21:46:02:8107 ...... ``` 通过以上日志我们看到,php的进程在写入log后即退出,而supervisor在php进程退出后会再次启动进程。 本文转自: https://www.jmsite.cn/blog-635.html
标签:
supervisord
非特殊说明,本博所有文章均为博主原创。
如若转载,请注明出处:
http://www.i366211.com/archives/148/
上一篇
微信营销平台框架KyPHP开源啦
下一篇
将网页生成图片的几个开源项目
取消回复
评论啦~
提交评论
栏目分类
软件安装
10
开发工具
8
算法
2
测试
1
架构
3
填坑记
2
开源
6
科普
6
私域
2
读书笔记
4
编程
48
运营
3
管理
1
标签云
算法
C程序设计语言
C语言
Java
mysql
PHP
ffmpeg
golang
VueJs
脚手架
VueJs实战项目
Intellij IDEA
Centos7
Hyperf
抖音运营
杰克韦尔奇
跌荡一百年
生成海量测试数据
企业管理
习题2-3
习题2-4
习题2-6
异常分类
File
习题2-7
习题2-8
习题2-9
习题3-3
习题3-4
习题3-5
友情链接
申请
SaaS引擎
机器人框架
京东捡漏