安装 supervisor
喜爱折腾的小伙伴可以手动折腾一下,不过我还是推荐使用 apt 安装,一行命令能够搞定的事情不要搞的这么复杂,提高效率应该是程序员该有的常识。
sudo apt install supervisor
一行命令完事,虽然不是最新版,但是在一个大版本内,又不是重度用户,没必要纠结。下面是我折腾的过程。
安装 pip
官方文档:https://pip.pypa.io/en/stable/getting-started
使用当前用户安装,程序会被安装在 ~/.local/lib/python*/site-packages/ 目录下,虽然可以执行,但不方便。
使用 sudo 安装,程序会被安装在 /usr/local/lib/ 目录下,可执行文件在 /usr/local/bin/ 目录下,方便使用。
wget https://bootstrap.pypa.io/get-pip.py sudo python3 get-pip.py
安装 supervisor
使用 sudo 安装 pip 才可以使用 sudo pip 安装模块,而且也是在 /usr/local/* 目录下,方便使用
sudo pip install supervisor
创建 supervisord.conf 配置文件
# 创建配置文件目录sudo mkdir /etc/supervisor
执行下面这个命令会提示没有权限,官方文档也给出了解决方案,就是先将配置文件释放到有权限的目录中,然后再移动。
# 会报权限问题echo_supervisord_conf > /etc/supervisor/supervisord.conf
分步骤执行
# 释放到用户目录下echo_supervisord_conf > ~/supervisord.conf# 移动到 /etc/supervisor 目录下sudo mv ~/supervisord.conf /etc/supervisor/supervisord.conf
创建进程配置文件夹
sudo mkdir -p /etc/supervisor/conf.d
修改 supervisord.conf 配置
vim /etc/supervisor/supervisord.conf
添加如下内容,.conf 或者 .ini 都可以,进程配置文件的后缀名与之相同即可
[include]files = /etc/supervisor/conf.d/*.conf
添加 supervisor.service 服务
其实就是手动把 apt 安装过程执行一边
vim /lib/systemd/system/supervisor.service
添加以下内容
ExecStart、ExecStop、ExecReload 要指定可执行文件 supervisord 和 supervisorctl 的路径和配置文件 supervisord.conf 的路径。
[Unit]Description=Supervisor process control system for UNIXDocumentation=http://supervisord.orgAfter=network.target[Service]ExecStart=/usr/local/bin/supervisord -n -c /etc/supervisor/supervisord.confExecStop=/usr/local/bin/supervisorctl $OPTIONS shutdownExecReload=/usr/local/bin/supervisorctl -c /etc/supervisor/supervisord.conf $OPTIONS reloadKillMode=processRestart=on-failureRestartSec=50s[Install]WantedBy=multi-user.target
注册 supervisor.service 服务
两个命令的效果一致
sudo systemctl enable supervisor.service# 或者sudo ln -s /usr/lib/systemd/system/supervisor.service /etc/systemd/system/multi-user.target.wants/supervisor.service
创建服务管理脚本
vim /etc/init.d/supervisor
添加以下内容。复制于使用 apt 安装生成的 /etc/init.d/supervisor 文件。
DAEMON 要指定可执行文件 supervisord 的路径 DAEMON_OPTS 要指定配置文件 supervisord.conf 的路径
#! /bin/sh## skeleton example file to build /etc/init.d/ scripts.# This file should be used to construct scripts for /etc/init.d.## Written by Miquel van Smoorenburg <miquels@cistron.nl>.# Modified for Debian# by Ian Murdock <imurdock@gnu.ai.mit.edu>.# Further changes by Javier Fernandez-Sanguino <jfs@debian.org>## Version: @(#)skeleton 1.9 26-Feb-2001 miquels@cistron.nl#### BEGIN INIT INFO# Provides: supervisor# Required-Start: $remote_fs $network $named# Required-Stop: $remote_fs $network $named# Default-Start: 2 3 4 5# Default-Stop: 0 1 6# Short-Description: Start/stop supervisor# Description: Start/stop supervisor daemon and its configured# subprocesses.### END INIT INFO. /lib/lsb/init-functionsPATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/binDAEMON=/usr/bin/supervisordNAME=supervisordDESC=supervisortest -x $DAEMON || exit 0RETRY=TERM/30/KILL/5LOGDIR=/var/log/supervisorPIDFILE=/var/run/$NAME.pidDODTIME=5 # Time to wait for the server to die, in seconds# If this value is set too low you might not# let some servers to die gracefully and# 'restart' will not work# Include supervisor defaults if availableif [ -f /etc/default/supervisor ] ; then. /etc/default/supervisorfiDAEMON_OPTS="-c /etc/supervisor/supervisord.conf $DAEMON_OPTS"set -erunning_pid(){# Check if a given process pid's cmdline matches a given namepid=$1name=$2[ -z "$pid" ] && return 1[ ! -d /proc/$pid ] && return 1(cat /proc/$pid/cmdline | tr "\000" "\n"|grep -q $name) || return 1return 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 namepid=`cat $PIDFILE`running_pid $pid $DAEMON || return 1return 0}case "$1" instart)echo -n "Starting $DESC: "start-stop-daemon --start --quiet --pidfile $PIDFILE \--startas $DAEMON -- $DAEMON_OPTStest -f $PIDFILE || sleep 1if running ; thenecho "$NAME."elseecho " ERROR."fi;;stop)echo -n "Stopping $DESC: "start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILEecho "$NAME.";;#reload)## If the daemon can reload its config files on the fly# for example by sending it SIGHUP, do it here.## If the daemon responds to changes in its config file# directly anyway, make this a do-nothing entry.## echo "Reloading $DESC configuration files."# start-stop-daemon --stop --signal 1 --quiet --pidfile \# /var/run/$NAME.pid --exec $DAEMON#;;force-reload)## If the "reload" option is implemented, move the "force-reload"# option to the "reload" entry above. If not, "force-reload" is# just the same as "restart" except that it does nothing if the# daemon isn't already running.# check wether $DAEMON is running. If so, restartstart-stop-daemon --stop --test --quiet --pidfile $PIDFILE \--startas $DAEMON \&& $0 restart \|| exit 0;;restart)echo -n "Restarting $DESC: "start-stop-daemon --stop --retry=$RETRY --quiet --oknodo --pidfile $PIDFILEecho "$NAME.";;status)echo -n "$NAME is "if running ; thenecho "running"elseecho " not running."exit 1fi;;*)N=/etc/init.d/$NAME# echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2echo "Usage: $N {start|stop|restart|force-reload|status}" >&2exit 1;;esacexit 0
为防止意外,退出 Ubuntu 终端,并在 powershell 中执行 wsl —shutdown 关闭子系统,重新进入。
开启 supervisor
sudo service supervisor start
设置开机启动
如何设置服务开机启动,请参考
在 services 中添加 supervisor 即可!
