安装 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/* 目录下,方便使用

  1. sudo pip install supervisor

创建 supervisord.conf 配置文件

  1. # 创建配置文件目录
  2. sudo mkdir /etc/supervisor

执行下面这个命令会提示没有权限,官方文档也给出了解决方案,就是先将配置文件释放到有权限的目录中,然后再移动。

  1. # 会报权限问题
  2. echo_supervisord_conf > /etc/supervisor/supervisord.conf

分步骤执行

  1. # 释放到用户目录下
  2. echo_supervisord_conf > ~/supervisord.conf
  3. # 移动到 /etc/supervisor 目录下
  4. sudo mv ~/supervisord.conf /etc/supervisor/supervisord.conf

创建进程配置文件夹

  1. sudo mkdir -p /etc/supervisor/conf.d

修改 supervisord.conf 配置

  1. vim /etc/supervisor/supervisord.conf

添加如下内容,.conf 或者 .ini 都可以,进程配置文件的后缀名与之相同即可

  1. [include]
  2. files = /etc/supervisor/conf.d/*.conf

添加 supervisor.service 服务

其实就是手动把 apt 安装过程执行一边

  1. vim /lib/systemd/system/supervisor.service

添加以下内容

ExecStart、ExecStop、ExecReload 要指定可执行文件 supervisord 和 supervisorctl 的路径和配置文件 supervisord.conf 的路径。

  1. [Unit]
  2. Description=Supervisor process control system for UNIX
  3. Documentation=http://supervisord.org
  4. After=network.target
  5. [Service]
  6. ExecStart=/usr/local/bin/supervisord -n -c /etc/supervisor/supervisord.conf
  7. ExecStop=/usr/local/bin/supervisorctl $OPTIONS shutdown
  8. ExecReload=/usr/local/bin/supervisorctl -c /etc/supervisor/supervisord.conf $OPTIONS reload
  9. KillMode=process
  10. Restart=on-failure
  11. RestartSec=50s
  12. [Install]
  13. WantedBy=multi-user.target

注册 supervisor.service 服务

两个命令的效果一致

  1. sudo systemctl enable supervisor.service
  2. # 或者
  3. sudo ln -s /usr/lib/systemd/system/supervisor.service /etc/systemd/system/multi-user.target.wants/supervisor.service

创建服务管理脚本
  1. vim /etc/init.d/supervisor

添加以下内容。复制于使用 apt 安装生成的 /etc/init.d/supervisor 文件。

DAEMON 要指定可执行文件 supervisord 的路径 DAEMON_OPTS 要指定配置文件 supervisord.conf 的路径

  1. #! /bin/sh
  2. #
  3. # skeleton example file to build /etc/init.d/ scripts.
  4. # This file should be used to construct scripts for /etc/init.d.
  5. #
  6. # Written by Miquel van Smoorenburg <miquels@cistron.nl>.
  7. # Modified for Debian
  8. # by Ian Murdock <imurdock@gnu.ai.mit.edu>.
  9. # Further changes by Javier Fernandez-Sanguino <jfs@debian.org>
  10. #
  11. # Version: @(#)skeleton 1.9 26-Feb-2001 miquels@cistron.nl
  12. #
  13. ### BEGIN INIT INFO
  14. # Provides: supervisor
  15. # Required-Start: $remote_fs $network $named
  16. # Required-Stop: $remote_fs $network $named
  17. # Default-Start: 2 3 4 5
  18. # Default-Stop: 0 1 6
  19. # Short-Description: Start/stop supervisor
  20. # Description: Start/stop supervisor daemon and its configured
  21. # subprocesses.
  22. ### END INIT INFO
  23. . /lib/lsb/init-functions
  24. PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  25. DAEMON=/usr/bin/supervisord
  26. NAME=supervisord
  27. DESC=supervisor
  28. test -x $DAEMON || exit 0
  29. RETRY=TERM/30/KILL/5
  30. LOGDIR=/var/log/supervisor
  31. PIDFILE=/var/run/$NAME.pid
  32. DODTIME=5 # Time to wait for the server to die, in seconds
  33. # If this value is set too low you might not
  34. # let some servers to die gracefully and
  35. # 'restart' will not work
  36. # Include supervisor defaults if available
  37. if [ -f /etc/default/supervisor ] ; then
  38. . /etc/default/supervisor
  39. fi
  40. DAEMON_OPTS="-c /etc/supervisor/supervisord.conf $DAEMON_OPTS"
  41. set -e
  42. running_pid()
  43. {
  44. # Check if a given process pid's cmdline matches a given name
  45. pid=$1
  46. name=$2
  47. [ -z "$pid" ] && return 1
  48. [ ! -d /proc/$pid ] && return 1
  49. (cat /proc/$pid/cmdline | tr "\000" "\n"|grep -q $name) || return 1
  50. return 0
  51. }
  52. running()
  53. {
  54. # Check if the process is running looking at /proc
  55. # (works for all users)
  56. # No pidfile, probably no daemon present
  57. [ ! -f "$PIDFILE" ] && return 1
  58. # Obtain the pid and check it against the binary name
  59. pid=`cat $PIDFILE`
  60. running_pid $pid $DAEMON || return 1
  61. return 0
  62. }
  63. case "$1" in
  64. start)
  65. echo -n "Starting $DESC: "
  66. start-stop-daemon --start --quiet --pidfile $PIDFILE \
  67. --startas $DAEMON -- $DAEMON_OPTS
  68. test -f $PIDFILE || sleep 1
  69. if running ; then
  70. echo "$NAME."
  71. else
  72. echo " ERROR."
  73. fi
  74. ;;
  75. stop)
  76. echo -n "Stopping $DESC: "
  77. start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE
  78. echo "$NAME."
  79. ;;
  80. #reload)
  81. #
  82. # If the daemon can reload its config files on the fly
  83. # for example by sending it SIGHUP, do it here.
  84. #
  85. # If the daemon responds to changes in its config file
  86. # directly anyway, make this a do-nothing entry.
  87. #
  88. # echo "Reloading $DESC configuration files."
  89. # start-stop-daemon --stop --signal 1 --quiet --pidfile \
  90. # /var/run/$NAME.pid --exec $DAEMON
  91. #;;
  92. force-reload)
  93. #
  94. # If the "reload" option is implemented, move the "force-reload"
  95. # option to the "reload" entry above. If not, "force-reload" is
  96. # just the same as "restart" except that it does nothing if the
  97. # daemon isn't already running.
  98. # check wether $DAEMON is running. If so, restart
  99. start-stop-daemon --stop --test --quiet --pidfile $PIDFILE \
  100. --startas $DAEMON \
  101. && $0 restart \
  102. || exit 0
  103. ;;
  104. restart)
  105. echo -n "Restarting $DESC: "
  106. start-stop-daemon --stop --retry=$RETRY --quiet --oknodo --pidfile $PIDFILE
  107. echo "$NAME."
  108. ;;
  109. status)
  110. echo -n "$NAME is "
  111. if running ; then
  112. echo "running"
  113. else
  114. echo " not running."
  115. exit 1
  116. fi
  117. ;;
  118. *)
  119. N=/etc/init.d/$NAME
  120. # echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
  121. echo "Usage: $N {start|stop|restart|force-reload|status}" >&2
  122. exit 1
  123. ;;
  124. esac
  125. exit 0

为防止意外,退出 Ubuntu 终端,并在 powershell 中执行 wsl —shutdown 关闭子系统,重新进入。

开启 supervisor

  1. sudo service supervisor start

设置开机启动

如何设置服务开机启动,请参考
在 services 中添加 supervisor 即可!