Linux使用supervisor创建守护进程方法
下载最新的supervisor安装包:
安装步骤
wget https://www.wangjikeji.com/uploads/upload/2022/12/202212261672043174518299.gz tar -zxvf 202212261672038902271139.gz cd supervisor-4.2.5 python setup.py install
安装成功如下图
Tips:错误1:ImportError: No module named setuptools
## 下载pip
wget https://files.pythonhosted.org/packages/c2/f7/c7b501b783e5a74cf1768bc174ee4fb0a8a6ee5af6afa92274ff964703e0/setuptools-40.8.0.zip
unzip setuptools-40.8.0.zip
cd setuptools-40.8.0
python setup.py install
Tips:错误2:error: Could not find suitable distribution for Requirement.parse(‘meld3>=0.6.5‘),安装meld扩展
wget https://pypi.python.org/packages/45/a0/317c6422b26c12fe0161e936fc35f36552069ba8e6f7ecbd99bbffe32a5f/meld3-1.0.2.tar.gz#md5=3ccc78cd79cffd63a751ad7684c02c91
tar zxvf meld3-1.0.2.tar.gz
cd meld3-1.0.2
python setup.py install
*** Tips:本地python版本为python3以下
二、配置
1.生成配置文件
echo_supervisord_conf > /etc/supervisord.conf
2.启动
supervisord -c /etc/supervisord.conf
查看 supervisord 是否在运行:
ps aux | grep supervisord
成功则如下图所示:
3.基础配置
(1)打开配置文件
路径 /etc/supervisord.conf,可用宝塔或者FTP进行修改
(2)在配置文件底部,配置include
[include]
files=/etc/supervisor/*.conf #若你本地无/etc/supervisor目录,请自建目录,可在任意位置
(3)用supervisor管理进程,配置如下:
[include]
files=/etc/supervisor/*.conf #若你本地无/etc/supervisor目录,请自建目录,可在任意位置
在这个目录下,编写自己的项目配置文件,名称自定义,后缀为conf,因为上一个步骤我们引入的是这个目录下所有的conf,例如:wangji.conf
(4)在创建的conf文件中,加入以下内容:(以xunsearch迅搜的配置文件举例)
[program:xunsearch]
command=sh monitor.sh
user=www
autostart=true
autorestart=true
startsecs=3
directory=/usr/local/xunsearch/bin/
stdout_logfile=/var/log/supervisor/xunsearch.log
请注意其中directory
目录为你Xunsearch服务端
安装目录中的bin
目录,并在期中创建monitor.sh
文件,文件内容为:
#!/bin/sh
while true;do
count=`ps -ef|grep xs-searchd|grep -v grep`
if [ "$?" != "0" ];then
echo "xs-searchd is stopped"
/bin/sh /usr/local/xunsearch/bin/xs-ctl.sh restart
#break
else
#echo "xs-searchd is runing..."
sleep 5
fi
done
请注意修改其中的Xunsearch服务端
安装目录。该monitor.sh也可加入宝塔的计划任务中定时执行。
(5)这里是启动要配置的参数,请根据自己的项目自定义添加
更改了supervisor配置文件,需要重启,运行以下指令:
supervisorctl reload
(6)服务启动
supervisord -c /etc/supervisord.conf
(7)用浏览器来管理进程配置
打开配置文件
位置:/etc/supervisord.conf
配置 inet_http_server
[inet_http_server]
port=127.0.0.1:9001; 服务器ip
username=username ;自定义
password=password ;自定义
三、supervisorctl的用法
=====================
supervisord : 启动supervisor
supervisorctl reload :修改完配置文件后重新启动supervisor
supervisorctl status :查看supervisor监管的进程状态
supervisorctl start all | 进程名 :启动全部或某进程
supervisorctl stop all | 进程名 :停止全部或某进程
supervisorctl stop all:停止进程,注:start、restart、stop都不会载入最新的配置文件。
supervisorctl update:根据最新的配置文件,启动新配置或有改动的进程,配置没有改动的进程不会受影响而重启
四、设置开机启动
(1)添加开机脚本
位置:/etc/init.d/supervisord
脚本如下:
#! /bin/sh
### BEGIN INIT INFO
# Provides: supervisord
# Required-Start: $remote_fs
# Required-Stop: $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Example initscript
# Description: This file should be used to construct scripts to be
# placed in /etc/init.d.
### END INIT INFO
# Author: Dan MacKinlay <danielm@phm.gov.au>
# Based on instructions by Bertrand Mathieu
# http://zebert.blogspot.com/2009/05/installing-django-solr-varnish-and.html
# Do NOT "set -e"
# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/usr/sbin:/bin:/usr/bin
DESC="Description of the service"
NAME=supervisord
DAEMON=/usr/local/bin/supervisord
DAEMON_ARGS=" -c /etc/supervisord.conf"
#PIDFILE=/var/run/$NAME.pid
PIDFILE=/tmp/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0
# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME
# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh
# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions
#
# Function that starts the daemon/service
#
do_start()
{
# Return
# 0 if daemon has been started
# 1 if daemon was already running
# 2 if daemon could not be started
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
|| return 1
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- \
$DAEMON_ARGS \
|| return 2
# Add code here, if necessary, that waits for the process to be ready
# to handle requests from services started subsequently which depend
# on this one. As a last resort, sleep for some time.
}
#
# Function that stops the daemon/service
#
do_stop()
{
# Return
# 0 if daemon has been stopped
# 1 if daemon was already stopped
# 2 if daemon could not be stopped
# other if a failure occurred
start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
RETVAL="$?"
[ "$RETVAL" = 2 ] && return 2
# Wait for children to finish too if this is a daemon that forks
# and if the daemon is only ever run from this initscript.
# If the above conditions are not satisfied then add some other code
# that waits for the process to drop all resources that could be
# needed by services started subsequently. A last resort is to
# sleep for some time.
start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
[ "$?" = 2 ] && return 2
# Many daemons don't delete their pidfiles when they exit.
rm -f $PIDFILE
return "$RETVAL"
}
#
# Function that sends a SIGHUP to the daemon/service
#
do_reload() {
#
# If the daemon can reload its configuration without
# restarting (for example, when it is sent a SIGHUP),
# then implement that here.
#
start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME
return 0
}
case "$1" in
start)
[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
do_start
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
stop)
[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
do_stop
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
#reload|force-reload)
#
# If do_reload() is not implemented then leave this commented out
# and leave 'force-reload' as an alias for 'restart'.
#
#log_daemon_msg "Reloading $DESC" "$NAME"
#do_reload
#log_end_msg $?
#;;
restart|force-reload)
#
# If the "reload" option is implemented then remove the
# 'force-reload' alias
#
log_daemon_msg "Restarting $DESC" "$NAME"
do_stop
case "$?" in
0|1)
do_start
case "$?" in
0) log_end_msg 0 ;;
1) log_end_msg 1 ;; # Old process is still running
*) log_end_msg 1 ;; # Failed to start
esac
;;
*)
# Failed to stop
log_end_msg 1
;;
esac
;;
*)
#echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
exit 3
;;
esac
:
(2)设置该脚本为可以执行
sudo chmod +x /etc/init.d/supervisord
(3)设置为开机自动运行
sudo update-rc.d supervisord defaults
(4)试一下,是否工作正常
service supervisord stop
service supervisord start
若报错:insserv: warning: script ‘service’ missing LSB tags and overrides,请执行:
sudo apt-get remove insserv
五、错误排查
1、unix:///var/run/supervisor/supervisor.sock no such file
问题描述:安装好supervisor没有开启服务直接使用supervisorctl报的错
解决办法:启动服务
supervisord -c /etc/supervisord.conf
2、启动了多个supervisord服务,导致无法正常关闭服务
问题描述:多次运行supervisord -c /etc/supervisord.conf 或supervisord,导致有些进程被多个superviord管理,无法正常关闭进程。
解决办法:kill相关的进程。
ps -fe | grep supervisord #查看所有启动过的supervisord服务
kill -9 PID #kill相关进程
3、supervisord 启动失败 Error: Another program is already listening on a port that one of our HTTP server
unlink /var/run/supervisor.sock
unlink /tmp/supervisor.sock
这个错误的原因就是supervisor.sock 这个文件会被系统自动删除或者其它原因不存在了,删除软连接就可以了。 supervisor.sock 生成的位置可以去 supervisor 的配置文件中找到。