首先,我不得不承认,博客里面我写的部分文章过于偏向于理论化,简单说,就是看不懂,就拿 Linux下Supervisor详细配置 这篇文章来说,我可能过多解释了如何在服务器安装Supervisor软件,但是对于如何使用Supervisor的问题,我选择了一笔带过,如果对服务器非常熟练的人一看也就清楚如何使用了,因为我个人觉得,一个软件安装好了,基本使用是没问题的了,但是忽略了一个最重要的问题,有些同学可能就需要使用,安装如果可以可能,一梭子就完成了,不会在意。所以才有了这篇文章,如何使用Supervisord来守护程序。
这篇文章更多侧重于如何使用Supervisor,所以安装方法我采用通用的方法,python来安装。
CentOS安装:
yum install epel-release -y
yum install python-pip git -y
yum -y install git gcc python-setuptools lsof lrzsz python-devel libffi-devel openssl-devel iptables
yum -y groupinstall "Development Tools"
easy_install supervisor
pip install --upgrade pip
wget -N -P /etc/ --no-check-certificate https://raw.githubusercontent.com/mmmwhy/ss-panel-and-ss-py-mu/master/supervisord.conf
supervisorctl shutdown
yum install python-pip git -y
yum -y install git gcc python-setuptools lsof lrzsz python-devel libffi-devel openssl-devel iptables
yum -y groupinstall "Development Tools"
easy_install supervisor
pip install --upgrade pip
wget -N -P /etc/ --no-check-certificate https://raw.githubusercontent.com/mmmwhy/ss-panel-and-ss-py-mu/master/supervisord.conf
supervisorctl shutdown
Debian&Ubuntu安装:
apt-get update -y
apt-get upgrade -y
apt-get install supervisor python-pip lsof -y
apt-get install build-essential wget -y
apt-get install iptables git -y
apt-get install python-pip git -y
wget -N -P /etc/ --no-check-certificate https://raw.githubusercontent.com/mmmwhy/ss-panel-and-ss-py-mu/master/supervisord.conf
supervisorctl shutdown
apt-get upgrade -y
apt-get install supervisor python-pip lsof -y
apt-get install build-essential wget -y
apt-get install iptables git -y
apt-get install python-pip git -y
wget -N -P /etc/ --no-check-certificate https://raw.githubusercontent.com/mmmwhy/ss-panel-and-ss-py-mu/master/supervisord.conf
supervisorctl shutdown
这样我们就安装完supervisor了。为什么要下载supervisord.conf文件呢?
因为python安装的不带配置文件,详见Linux下Supervisor详细配置
上面下载的supervisord.conf文件自带了魔改V3的后端守护进程,我也不打算做修改,具体在最后这几行:
;[include]
;files = relative/directory/*.ini
[program:ssr]
command = python /root/shadowsocks/server.py
user = root
autostart = true
autorestart = true
;files = relative/directory/*.ini
[program:ssr]
command = python /root/shadowsocks/server.py
user = root
autostart = true
autorestart = true
一般人看到这个也就明白了。 command这行这就是你守护的进程如何启动,举个例子:
启动SSR服务:
command = python /usr/local/shadowsocks/server.py -c /etc/shadowsocks.json -d start
启动魔改V3后端:
command = python /root/shadowsocks/server.py
如何守护多个进程?
[program:1]
command = python /root/shadowsocks/server.py
user = root
autostart = true
autorestart = true
[program:2]
command = python /root/shadowsocks/server.py
user = root
autostart = true
autorestart = true
command = python /root/shadowsocks/server.py
user = root
autostart = true
autorestart = true
[program:2]
command = python /root/shadowsocks/server.py
user = root
autostart = true
autorestart = true
所以,对于配置文件,我们只需要手动修改/etc/supervisord.conf文件就行。
守护一个,底部的那个修改下command
守护两个,底部追加一个[program:2] 开头的守护进程即可。
守护脚本写好了,我们接下来需要启动Supervisor了
测试下:
[root@test ~]# supervisord
/usr/lib/python2.7/site-packages/supervisor-3.3.4-py2.7.egg/supervisor/options.py:461: UserWarning: Supervisord is running as root and it is searching for its configuration file in default locations (including its current working directory); you probably want to specify a "-c" argument specifying an absolute path to a configuration file for improved security.
'Supervisord is running as root and it is searching '
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 supervisord.
For help, use /usr/bin/supervisord -h
/usr/lib/python2.7/site-packages/supervisor-3.3.4-py2.7.egg/supervisor/options.py:461: UserWarning: Supervisord is running as root and it is searching for its configuration file in default locations (including its current working directory); you probably want to specify a "-c" argument specifying an absolute path to a configuration file for improved security.
'Supervisord is running as root and it is searching '
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 supervisord.
For help, use /usr/bin/supervisord -h
这个告诉我们,需要带参数运行。
下面附上常用命令:
关闭supervisor:supervisorctl shutdown
开启supervisor:supervisord -c /etc/supervisord.conf
开启supervisor:supervisord -c /etc/supervisord.conf
开机自启:
echo "/usr/bin/supervisord -c /etc/supervisord.conf" >> /etc/rc.local
chmod +x /etc/rc.local
chmod +x /etc/rc.local
注意一个问题,运行的程序需要绝对路径+绝对指令运行,所以无法执行例如:
command = ./etc/init.d/shadowsocks start
这样的程序,我们如何找到程序的入口呢?以ssr为例子:
终端执行:
ps -aux
里面可以找到ssr的进程具体脚本:
复制这个指令到配置文件,然后关闭进程,再开启supervisor即可进行守护。
上个测试:
简单说说运行方法:
安装-修改配置文件-启动程序-配置开机自启。