使用Supervisor监管LaravelS

Supervisor 是一个 Python 写的进程管理工具,有时一个进程需要在后台运行,并且意外挂掉后能够自动重启,就需要这么一个管理进程的工具。

安装

  • 使用 pip 工具进行安装:
1
[root@caoxl ~]# sudo pip install supervisor
  • Ubuntu 系统使用 apt-get
1
sudo apt-get install supervisor
  • 检查是否安装完成
1
2
3
4
5
6
7
8
9
10
11
12
13
[root@caoxl ~]# supervisord --help
supervisord -- run a set of applications as daemons.

Usage: /usr/bin/supervisord [options]

Options:
-c/--configuration FILENAME -- configuration file path (searches if not given)
-n/--nodaemon -- run in the foreground (same as 'nodaemon=true' in config file)
-h/--help -- print this usage message and exit
-v/--version -- print supervisord version number and exit
-u/--user USER -- run supervisord as this user (or numeric uid)
-m/--umask UMASK -- use this umask for daemon subprocess (default is 022)
...

配置

运行这个命令可以生成一个默认的配置文件:

1
echo_supervisord_conf > /etc/supervisord/supervisord.conf

生成成功后,打开编辑这个文件,把最后的 include 块的注释打开,并修改如下:

1
2
[include]
files = /etc/supervisor/*.conf

新增的 Supervisor 配置文件放在 /etc/supervisor 目录下,并且以 conf 结尾。

这时我们使用新的配置文件来启动 Supervisor:

1
supervisord -c /etc/supervisord.conf
  • 如果提示已经有进程在运行,那么先 kill 掉它。
1
2
3
[root@caoxl ~]# supervisord -c /etc/supervisord.conf 
Error: Another program is already listening on a port that one of our HTTP servers is configured to use. Shut this program down first before starting supervisord.
For help, use /usr/bin/supervisord -h
  • 找到旧进程, kill掉
1
2
3
4
5
[root@caoxl ~]# ps -ef | grep supervisord
root 551 31716 0 10:53 pts/3 00:00:00 grep --color=auto supervisord
root 32156 1 0 10:18 ? 00:00:00 /usr/bin/python2 /usr/bin/supervisord -c ./supervisor/supervisord.conf
[root@caoxl ~]# kill -9 32156

使用

Supervisor管理LaravelS进程

  • 新增 /etc/supervisor/supervisord.conf 配置文件
1
2
3
4
5
6
7
8
9
10
11
[program: laravel6]
command=/usr/local/php/php72/bin/php /var/www/laravel6/bin/laravels start -i
directory=/var/www/laravel6
process_name=%(process_num)s
autorestart=true
autostart=true
stderr_logfile=/var/www/laravel6/storage/logs/supervisord-stderr.log
stdout_logfile=/var/www/laravel6/storage/logs/supervisord-stdout.log
user=root
numprocs=1
startsecs=1
  • 说明
  • command: 运行程序的命令
  • directory: 命令执行的目录
  • autorestart: 程序意外退出是否自动重启
  • autostart: 是否自动启动
  • stderr_logfile: 错误日志文件
  • stdout_logfile: 输出日志文件
  • user: 进程执行的用户身份
  • numprocs: 进程数量
  • startsecs: 自动重启间隔

一切就绪后,我们使用如下命令就可以启动进程的监听了:

1
2
3
4
5
6
7
8
[root@caoxl ~]# sudo supervisorctl reread
laravel6: changed

[root@caoxl ~]# sudo supervisorctl update
laravel6: stopped
laravel6: updated process group

[root@caoxl ~]# sudo supervisorctl start laravel6:*
  • 检查开启成功
1
2
3
4
5
6
7
8
9
10
11
12
[root@caoxl ~]# ps -ef | grep laravels
root 2258 593 0 11:15 ? 00:00:00 /var/www/laravel6 laravels: master process
root 2269 2258 0 11:15 ? 00:00:00 /var/www/laravel6 laravels: manager process
root 2275 2269 0 11:15 ? 00:00:00 /var/www/laravel6 laravels: task worker process 4
root 2276 2269 0 11:15 ? 00:00:00 /var/www/laravel6 laravels: task worker process 5
root 2277 2269 0 11:15 ? 00:00:00 /var/www/laravel6 laravels: task worker process 6
root 2278 2269 0 11:15 ? 00:00:00 /var/www/laravel6 laravels: task worker process 7
root 2279 2269 0 11:15 ? 00:00:00 /var/www/laravel6 laravels: worker process 0
root 2280 2269 0 11:15 ? 00:00:00 /var/www/laravel6 laravels: worker process 1
root 2281 2269 0 11:15 ? 00:00:00 /var/www/laravel6 laravels: worker process 2
root 2282 2269 0 11:15 ? 00:00:00 /var/www/laravel6 laravels: worker process 3
root 2337 1412 0 11:16 pts/4 00:00:00 grep --color=auto laravels
  • 检查LaravelS进程
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
[root@caoxl laravel6]# php bin/laravels info
_ _ _____
| | | |/ ____|
| | __ _ _ __ __ ___ _____| | (___
| | / _` | '__/ _` \ \ / / _ \ |\___ \
| |___| (_| | | | (_| |\ V / __/ |____) |
|______\__,_|_| \__,_| \_/ \___|_|_____/

Speed up your Laravel/Lumen
>>> Components
+---------------------------+---------+
| Component | Version |
+---------------------------+---------+
| PHP | 7.2.7 |
| Swoole | 4.4.7 |
| LaravelS | 3.5.12 |
| Laravel Framework [local] | 6.4.1 |
+---------------------------+---------+
>>> Protocols
+------------------------+--------+-------------------------------+----------------+
| Protocol | Status | Handler | Listen At |
+------------------------+--------+-------------------------------+----------------+
| Main HTTP | On | Laravel Framework | 127.0.0.1:5200 |
| Main WebSocket | On | App\Services\WebSocketService | 127.0.0.1:5200 |
| Port#0 TCP IPV4 Socket | On | App\Sockets\SwooleTcpSocket | 127.0.0.1:5300 |
| Port#1 UDP IPV4 Socket | On | App\Sockets\SwooleUdpSocket | 0.0.0.0:5301 |
+------------------------+--------+-------------------------------+----------------+
>>> Feedback: https://github.com/hhxsv5/laravel-s

Powered by Hexo and Hexo-theme-hiker

Copyright © 2017 - 2023 Keep It Simple And Stupid All Rights Reserved.

访客数 : | 访问量 :