Supervisor (http://supervisord.org) 是一個用 Python 寫的進程管理工具,可以很方便的用來啟動、重啟、關閉進程(不僅僅是 Python 進程)。除了對單個進程的控制,還可以同時啟動、關閉多個進程,比如很不幸的服務器出問題導致所有應用程序都被殺死,此時可以用 supervisor 同時啟動所有應用程序而不是一個一個地敲命令啟動。
安裝:
1、首先配置epel源,這里省略
2、安裝
yum install python-pip pip install supervisor
上步驟可能遇到報錯:
pkg_resources.DistributionNotFound: meld3>=0.6.5
解決辦法:
1. git clone https://github.com/Supervisor/meld3 2. cd meld3 3. python setup.py install
安裝完成后,做如下操作:
生成默認配置文件:
# echo_supervisord_conf > /etc/supervisord.conf
簡單的為nginx和weblogic服務配置,在上面生成的配置文件末尾添加:
[program:weblogic] directory = /home/weblogic/Oracle/Middleware/user_projects/domains/base_domain command = sh startWebLogic.sh(記得weblogic的服務不要&,不要指定后台運行) user = weblogic startsecs = 90 autorestart = true autostart = true stdout_logfile = /var/log/weblogic_stdout.log [program:nginx] command = /usr/sbin/nginx -c /etc/nginx/nginx.conf(配置nginx服務不要使用后台daemon運行:daemon off;添加到nginx配置文件中) startsecs = 90 autorestart = true autostart = true stdout_logfile = /var/log/nginx_stdout.log
開啟supervisor服務:
# /usr/bin/supervisord -c /etc/supervisord.conf
查看該服務日志:
[root@weblogic etc]# tail -f /tmp/supervisord.log 2017-12-12 15:33:02,868 CRIT Supervisor running as root (no user in config file) 2017-12-12 15:33:02,878 INFO RPC interface 'supervisor' initialized 2017-12-12 15:33:02,878 CRIT Server 'unix_http_server' running without any HTTP authentication checking 2017-12-12 15:33:02,880 INFO daemonizing the supervisord process 2017-12-12 15:33:02,880 INFO supervisord started with pid 5173 2017-12-12 15:33:03,887 INFO spawned: 'nginx' with pid 5174 2017-12-12 15:33:03,897 INFO spawned: 'weblogic' with pid 5175 2017-12-12 15:34:34,389 INFO success: nginx entered RUNNING state, process has stayed up for > than 90 seconds (startsecs) 2017-12-12 15:34:34,389 INFO success: weblogic entered RUNNING state, process has stayed up for > than 90 seconds (startsecs)
查看服務運行情況:
[root@weblogic ~]# netstat -tunlp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 5174/nginx tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1165/sshd tcp 0 0 :::80 :::* LISTEN 5174/nginx tcp 0 0 :::22 :::* LISTEN 1165/sshd tcp 0 0 ::ffff:127.0.0.1:7001 :::* LISTEN 5232/java tcp 0 0 fe80::20c:29ff:fe3b:78:7001 :::* LISTEN 5232/java tcp 0 0 ::ffff:192.168.101.16:7001 :::* LISTEN 5232/java tcp 0 0 ::1:7001 :::* LISTEN 5232/java tcp 0 0 :::7005 :::* LISTEN 5232/java udp 0 0 :::1161 :::* 5232/java
附加:上面配置文件的一些參數信息:
[program:usercenter] directory = /home/leon/projects/usercenter ; 程序的啟動目錄 command = gunicorn -c gunicorn.py wsgi:app ; 啟動命令,可以看出與手動在命令行啟動的命令是一樣的 autostart = true ; 在 supervisord 啟動的時候也自動啟動 startsecs = 5 ; 啟動 5 秒后沒有異常退出,就當作已經正常啟動了 autorestart = true ; 程序異常退出后自動重啟 startretries = 3 ; 啟動失敗自動重試次數,默認是 3 user = leon ; 用哪個用戶啟動 redirect_stderr = true ; 把 stderr 重定向到 stdout,默認 false stdout_logfile_maxbytes = 20MB ; stdout 日志文件大小,默認 50MB stdout_logfile_backups = 20 ; stdout 日志文件備份數 ; stdout 日志文件,需要注意當指定目錄不存在時無法正常啟動,所以需要手動創建目錄(supervisord 會自動創建日志文件) stdout_logfile = /data/logs/usercenter_stdout.log ; 可以通過 environment 來添加需要的環境變量,一種常見的用法是修改 PYTHONPATH ; environment=PYTHONPATH=$PYTHONPATH:/path/to/somewhere