主机环境:(Centos7)
[root@localhost ~]# cat /proc/version
Linux version 3.10.0-229.el7.x86_64 (builder@kbuilder.dev.centos.org) (gcc version 4.8.2 20140120 (Red Hat 4.8.2-16) (GCC) ) #1 SMP Fri Mar 6 11:36:42 UTC 2015
[root@localhost ~]# yum install python-setuptools
[root@localhost ~]# easy_install supervisor
[root@localhost ~]# echo_supervisord_conf
; Sample supervisor config file.
;
; For more information on the config file, please see:
; http://supervisord.org/configuration.html
;
; Notes:
; - Shell expansion ("~" or "$HOME") is not supported. Environment
; variables can be expanded using this syntax: "%(ENV_HOME)s".
; - Quotes around values are not supported, except in the case of
; the environment= options as shown below.
; - Comments must have a leading space: "a=b ;comment" not "a=b;comment".
; - Command will be truncated if it looks like a config file comment, e.g.
; "command=bash -c 'foo ; bar'" will truncate to "command=bash -c 'foo ".
[unix_http_server]
file=/tmp/supervisor.sock ; the path to the socket file
;chmod=0700 ; socket file mode (default 0700)
;chown=nobody:nogroup ; socket file uid:gid owner
;username=user ; default is no username (open server)
;password=123 ; default is no password (open server)
;[inet_http_server] ; inet (TCP) server disabled by default
;port=127.0.0.1:9001 ; ip_address:port specifier, *:port for all iface
;username=user ; default is no username (open server)
;password=123 ; default is no password (open server)
...
2.创建配置文件创建supervisor配置文件目录/etc/supervisor/
[root@localhost ~]# mkdir -m 755 -p /etc/supervisor/
[root@localhost ~]# cd /etc/
[root@localhost etc]# ll |grep supervisor
drwxr-xr-x 3 root root 42 Jul 11 11:09 supervisor
创建主配文件supervisord.conf
[root@localhost etc]# echo_supervisord_conf > /etc/supervisor/supervisord.conf
[root@localhost etc]# cd supervisor/
[root@localhost supervisor]# ll
total 12
-rw-r--r-- 1 root root 9186 Jul 11 11:09 supervisord.conf
创建项目配置文件目录
[root@localhost supervisor]# mkdir -m 755 conf.d
[root@localhost supervisor]# ll
total 12
drwxr-xr-x 2 root root 37 Jul 11 10:20 conf.d
-rw-r--r-- 1 root root 9186 Jul 11 11:09 supervisord.conf
3.调试
在/data/wwwroot/supervisor_simple 目录下创建test.c
[root@localhost ~]# vim /data/wwwroot/supervisor_simple/test.c
#include<stdio.h>
#include<string.h>
int main() {
FILE *fp = fopen("/data/wwwroot/supervisor_simple/1.txt", "a+");
if(fp==0){
printf("can't open file\n");
return 0;
}
int ix = 0;
for(;; ix++){
fseek(fp, 0, SEEK_END);
char s_add_arr[10];
memset(s_add_arr, '\0', 10);
sprintf(s_add_arr, "%i\n", ix);
fwrite(s_add_arr, strlen(s_add_arr), 1, fp);
sleep(1);
}
fclose(fp);
return 0;
}
编译为test
[root@localhost ~]# cd /data/wwwroot/supervisor_simple/
[root@localhost supervisor_simple]# gcc -o test test.c
在/etc/supervisor/conf.d 目录下创建 test.ini
[root@localhost ~]# cd /etc/supervisor/conf.d/
[root@localhost conf.d]# # vim test.ini
[program:test]
command=/data/wwwroot/supervisor_simple/test
stdout_logfile=/tmp/test_stdout.log;
autostart=true
autorestart=true
startsecs=5
priority=1
stopasgroup=true
killasgroup=true
在主配文档中引入test.ini
[root@localhost supervisor]# vim supervisord.conf
//找到include
;[include]
;files = relative/directory/*.ini
//改为
[include]
files = ./conf.d/*.ini
启动supervisor
[root@localhost supervisor]# supervisord -c supervisord.conf
Unlinking stale socket /tmp/supervisor.sock
[root@localhost supervisor]# pstree
systemd─┬─NetworkManager─┬─dhclient
│ └─3*[{NetworkManager}]
├─VBoxService───7*[{VBoxService}]
├─agetty
├─auditd───{auditd}
├─crond
├─dbus-daemon
├─lvmetad
├─memcached───6*[{memcached}]
├─mysqld_safe───mysqld───23*[{mysqld}]
├─nginx───nginx
├─php-fpm───11*[php-fpm]
├─polkitd───5*[{polkitd}]
├─pure-ftpd
├─redis-server───2*[{redis-server}]
├─rsyslogd───2*[{rsyslogd}]
├─sshd─┬─sshd───bash───pstree
│ ├─6*[sshd───sftp-server]
│ └─sshd───bash
├─supervisord───test
├─systemd-journal
├─systemd-logind
├─systemd-udevd
└─tuned───4*[{tuned}]
[root@localhost supervisor]# pstree -p | grep supervisord
|-supervisord(6271)---test(6281)
[root@localhost supervisor]#
查看supervisord.log发现program test已启动
[root@localhost supervisor]# cat /tmp/supervisord.log
2017-07-11 10:47:33,440 CRIT Supervisor running as root (no user in config file)
2017-07-11 10:47:33,440 INFO Included extra file "/etc/supervisor/./conf.d/test.ini" during parsing
2017-07-11 10:47:33,480 INFO RPC interface 'supervisor' initialized
2017-07-11 10:47:33,480 CRIT Server 'unix_http_server' running without any HTTP authentication checking
2017-07-11 10:47:33,481 INFO daemonizing the supervisord process
2017-07-11 10:47:33,481 INFO supervisord started with pid 6572
2017-07-11 10:47:34,486 INFO spawned: 'test' with pid 6582
2017-07-11 10:47:39,532 INFO success: test entered RUNNING state, process has stayed up for > than 5 seconds (startsecs)
2017-07-11 11:10:06,249 INFO waiting for test, skuld to die
2017-07-11 11:10:06,251 INFO stopped: test (terminated by SIGTERM)
用 supervisorctl 查看已经被监控的program
[root@localhost supervisor]# supervisorctl
test RUNNING pid 6735, uptime 3:08:29
supervisor>
注:直接用运行supervisorctl命令没效果的话,换成下面的命令。效果一样
[root@localhost supervisor]# supervisorctl -c /etc/supervisor/supervisord.conf
test RUNNING pid 6735, uptime 3:08:29
supervisor>
增加一例监控php脚本
[root@localhost supervisor]# cd conf.d/
[root@localhost conf.d]# vim skuld.ini
[program:skuld]
command=php -f /data/wwwroot/supervisor_simple/skuld.php
stdout_logfile=/tmp/skuld_stdout.log;
autostart=true
autorestart=true
startsecs=5
priority=1
stopasgroup=true
killasgroup=true
在/data/wwwroot/supervisor_simple目录下创建skuld.php
<?php
$fn = fopen('/data/wwwroot/supervisor_simple/skuld.log', 'a');
$i = 0;
while(1){
sleep(1);
fwrite($fh, $i++ . "\n");
}
fclose($fn);
重启监控服务
[root@localhost conf.d]# supervisorctl reload
Restarted supervisord
[root@localhost conf.d]# pstree
systemd─┬─NetworkManager─┬─dhclient
│ └─3*[{NetworkManager}]
├─VBoxService───7*[{VBoxService}]
├─agetty
├─auditd───{auditd}
├─crond
├─dbus-daemon
├─lvmetad
├─memcached───6*[{memcached}]
├─mysqld_safe───mysqld───23*[{mysqld}]
├─nginx───nginx
├─php-fpm───11*[php-fpm]
├─polkitd───5*[{polkitd}]
├─pure-ftpd
├─redis-server───2*[{redis-server}]
├─rsyslogd───2*[{rsyslogd}]
├─sshd─┬─2*[sshd───bash]
│ ├─6*[sshd───sftp-server]
│ └─sshd───bash───pstree
├─supervisord─┬─php
│ └─test
├─systemd-journal
├─systemd-logind
├─systemd-udevd
└─tuned───4*[{tuned}]
[root@localhost conf.d]#
supervisord : supervisor的服务器端部分,用于supervisor启动。
supervisorctl:启动supervisor的命令行窗口,在该命令行中可执行start、stop、status、reload等操作。
;[inet_http_server] ; inet (TCP) server disabled by default
;port=127.0.0.1:9001 ; (ip_address:port specifier, *:port for all iface)
;username=user ; (default is no username (open server))
;password=123 ; (default is no password (open server))
修改成:
[inet_http_server] ; inet (TCP) server disabled by default
port=127.0.0.1:9001 ; (ip_address:port specifier, *:port for all iface)
username=user ; (default is no username (open server))
password=123 ; (default is no password (open server))
port:绑定访问IP和端口,这里是绑定的是本地IP和9001端口(写上你自己的服务器IP或是本地IP)
username:登录管理后台的用户名
password:登录管理后台的密码
4.如果9001端口拒绝访问,服务器允许9001端口访问 ,保存iptables规则 。
[root@localhost ~]# iptables -I INPUT 4 -p tcp -m state --state NEW -m tcp --dport 9001 -j ACCEPT
[root@localhost ~]# service iptables save
访问web管理界面