爲應用進程添加守護,在應用意外退出或服務器重啓時,保證應用會被自動重啓
安裝和配置守護程序(supervisor)
- easy_install supervisor (下載安裝supervisor)
- /usr/local/bin/echo_supervisord_conf > /etc/supervisord.conf (添加配置文件,從default複製)
- printf "[include]\nfiles = supervisor/conf.d/*.conf\n" >> /etc/supervisord.conf (在配置文件中指定子配置位置)
- mkdir -p /etc/supervisor/conf.d/ (建立子配置文件目錄)
- mkdir -p /opt/deploy/ (建立supervisor啓動配置目錄)
- vim /opt/deploy/supervisord (添加supervisor啓動配置,內容以下:
#!/bin/sh # Source function library. . /etc/rc.d/init.d/functions supervisorctl="/usr/local/bin/supervisorctl" supervisord="/usr/local/bin/supervisord" name="supervisor-python" [ -f $supervisord ] || exit 1 [ -f $supervisorctl ] || exit 1 RETVAL=0 start() { echo -n "Starting $name: " $supervisord -c /etc/supervisord.conf RETVAL=$? echo return $RETVAL } stop() { echo -n "Stopping $name: " $supervisorctl shutdown RETVAL=$? echo return $RETVAL } case "$1" in start) start ;; stop) stop ;; restart) stop start ;; esac exit $REVAL
) -
chmod +x /opt/deploy/supervisord (添加可執行權限)java
-
ln -s /opt/deploy/supervisord /etc/init.d (註冊supervisord,使其能夠使用service xxx restart/stop/start)python
-
chkconfig supervisord on (改變服務的啓動信息)vim
添加須要守護的應用
- vim /etc/supervisor/conf.d/xxx.conf (添加xxx應用的守護配置,內容以下:
[program:xxxx] command=/usr/bin/java -Xms2048m -Xmx2048m -server -jar /home/ec2-user/xxx/xxx.jar user=ec2-user directory=/home/ec2-user/xxx/logs/.. autostart=true autorestart=true startsecs=30 startretries=1
-
service supervisord start (啓動守護服務)ruby
-
supervisorctl reload (添加或更新xxx.conf後執行這個命令能夠使配置更新)服務器
-
supervisorctl update (添加或更新xxx.conf後執行這個命令能夠使改動當即生效)spa
其餘命令
-
supervisorctl status (列出被守護應用)rest
-
supervisorctl start/stop xxx (啓動/中止xxx應用,notes:用service xxx stop/kill -9 *** 中止某個應用服務會失效,由於會被守護重啓,因此若是要主動中止某個應用要使用這行命令 supervisorctl stop xxx)code