The use of process management tool Supervisor

Time:2020-8-2

brief introduction

Supervisor is a client / server service developed in Python and a process management tool under linux / unix system

purpose

When using spool, PHP server.php Turn on an HTTP service and listen on port 9501, even if using nohup PHP server.php If you hang it to the background to run, the process may hang up, but using supervisor can realize the monitoring of the process, automatic restart and other functions, the daemons.

<?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();

Installation and use

  1. First, install the package management tool pip of Python
  2. pip3 install supervisor
  3. Execute echo in local custom directory_ supervisord_ conf > supervisord.conf Copy a profile
  4. modify supervisord.conf
[inet_ http_ Server]; HTTP address
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; configuration directory of the local subprocess

For detailed configuration, please refer to: http://www.supervisord.org/co…

  1. Execute in the / usr / local / etc / Supervisor. D / directory

vim swoole.ini

[ program:swoole ]; define process name
command=php /Users/jiao/ProjectPhp/test/jiao/ server.php  ; execute script commands
redirect_stderr=false  
Autostart = true; start Supervisr to start the service
Autorestart = true; the process will restart automatically when it is hung
stderr_ logfile=/tmp/swoole_ stderr.log  ; errorlog output file
stdout_ logfile=/tmp/swoole_ stdout.log  ; log output file
  1. Start Supervisor

supervisord -c /usr/local/etc/supervisord.conf

  1. View service status

supervisorctl status
display
swoole RUNNING pid 28495, uptime 0:19:43

You can see that the spoole service has been started

➜  lsof -i:9501
COMMAND   PID USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
php     28495 jiao    4u  IPv4 0x44e3475dab74e94d      0t0  TCP localhost:9501 (LISTEN)

Use the kill command to end the spoole process, and it will restart automatically

➜  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)

Also available through http://127.0.0.1 : 9019 / view
The use of process management tool Supervisor

Common commands of supervisor are attached

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

Recommended Today

ASP.NET Example of core MVC getting the parameters of the request

preface An HTTP request is a standard IO operation. The request is I, which is the input; the responsive o is the output. Any web development framework is actually doing these two things Accept the request and parse to get the parameters Render according to the parameters and output the response content So let’s learn […]