php 进程管理工具,进程管理工具 Supervisor 的使用

简介

supervisor是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具

用途

在使用swoole时,php server.php开启一个http服务并监听9501端口,即使使用nohup php server.php将其挂到后台运行,该进程可能会挂掉,而使用supervisor可以实现对该进程的监控,实现自动重启等功能,守护进程。

$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 ;本地子进程的配置目录

在/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)

附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.

``

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值