简介
supervisor是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具
用途
在使用swoole时,php server.php开启一个http服务并监听9501端口,即使使用nohup php server.php将其挂到后台运行,该进程可能会挂掉,而使用supervisor可以实现对该进程的监控,实现自动重启等功能,守护进程。
<?php
$http = new swoole_http_server("127.0.0.1", 9501);
$http->set(['work_num' => 2,'reactor_num' => 2]);
$http->on("start", function ($server) {
echo "Swoole http server is started at http://127.0.0.1:9501\n";
});
$http->on("request", function ($request, $response) {
var_dump($request);
$response->header("Content-Type", "text/plain");
$response->end("Hello World\n");
});
$http->start();
安装使用
- 首先安装python的包管理工具pip,不再累述
- pip3 install supervisor
- 在本地自定义目录中执行 echo_supervisord_conf > supervisord.conf 复制一个配置文件
- 修改supervisord.conf
[inet_http_server] ; http地址
port=127.0.0.1:9019 ; ip_address:port specifier, *:port for all iface
username=user ; default is no username (open server)
password=123 ; default is no password (open server)
[include]
files = /usr/local/etc/supervisor.d/*.ini ;本地子进程的配置目录
详细配置见:http://www.supervisord.org/co...
- 在/usr/local/etc/supervisor.d/目录执行
vim swoole.ini
[program:swoole] ;定义进程名称
command=php /Users/jiao/ProjectPhp/test/jiao/server.php ;执行脚本命令
redirect_stderr=false
autostart=true ;启动superviosr就启动该服务
autorestart=true ;进程挂了就自动重启
stderr_logfile=/tmp/swoole_stderr.log ;errorlog输出文件
stdout_logfile=/tmp/swoole_stdout.log ;log输出文件
- 启动supervisor
supervisord -c /usr/local/etc/supervisord.conf
- 查看服务状态
supervisorctl status
显示swoole RUNNING pid 28495, uptime 0:19:43
可以看到swoole服务已经启动
➜ lsof -i:9501
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
php 28495 jiao 4u IPv4 0x44e3475dab74e94d 0t0 TCP localhost:9501 (LISTEN)
通过kill命令结束swoole进程,发现也会自动重启
➜ kill -s TERM 28706
➜ lsof -i:9501
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
php 28825 jiao 4u IPv4 0x44e3475d86909315 0t0 TCP localhost:9501 (LISTEN)
也可通过http://127.0.0.1:9019/查看
附supervisor常用命令
-c --configuration FILENAME -- configuration file path (searches if not given)
-h --help -- print usage message and exit
-i --interactive -- start an interactive shell after executing commands
-s --serverurl URL -- URL on which supervisord server is listening (default "http://localhost:9001").
-u --username USERNAME -- username to use for authentication with server
-p --password PASSWORD -- password to use for authentication with server
-r --history-file -- keep a readline history (if readline is available)
action [arguments] -- see below
Actions are commands like "tail" or "stop". If -i is specified or no action is
specified on the command line, a "shell" interpreting actions typed
interactively is started. Use the action "help" to find out about available
actions.
``
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。