linux守护进程管理工具,Supervisor守护进程管理工具-安装说明

正 文:

Supervisor管理进程,是通过fork/exec的方式将这些被管理的进程当作supervisor的子进程来启动,所以我们只需要将要管理进程的可执行文件的路径添加到supervisor的配置文件中就好了。此时被管理进程被视为supervisor的子进程,若该子进程异常中断,则父进程可以准确的获取子进程异常中断的信息,通过在配置文件中设置autostart=ture,可以实现对异常中断的子进程的自动重启。

Supervisor(http://supervisord.org/)是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具,不支持Windows系统。它可以很方便的监听、启动、停止、重启一个或多个进程。用Supervisor管理的进程,当一个进程意外被杀死,supervisort监听到进程死后,会自动将它重新拉起,很方便的做到进程自动恢复的功能,不再需要自己写shell脚本来控制。

因为Supervisor是Python开发的,安装前先检查一下系统否安装了Python2.4以上版本。下面以CentOS7,Python2.7版本环境下,介绍Supervisor的安装与配置步聚:

一、  安装supervisor

yum install python-setuptools

easy_install supervisor

supervisor安装完成后会生成三个执行程序:supervisortd:是supervisor的守护进程服务(用于接收进程管理命令)

supervisorctl:客户端(用于和守护进程通信,发送管理进程的指令)

echo_supervisord_conf:生成初始配置文件程序

二、  配置supervisor

生成配置文件:echo_supervisord_conf > /etc/supervisord.conf

mkdir -p /etc/supervisor/conf.d/

然后在 /etc/supervisord.conf 的 [include] 下添加:[include]

files = /etc/supervisor/conf.d/*.conf

注意: [include] 前面的分号注释也要去掉!不然找不到进程的:supervisorctl error (no such process)

修改 /etc/supervisord.conf 里的配置文件路径 /tmp 为其他,不然linux会自动清理tmp目录,会导致出现错误:unix:///tmp/supervisor.sock no such file

这里把所有的/tmp路径改掉:/tmp/supervisor.sock 改成 /var/run/supervisor.sock

/tmp/supervisord.log 改成 /var/log/supervisor.log

/tmp/supervisord.pid 改成 /var/run/supervisor.pid

创建 supervisor.sock 文件:sudo touch /var/run/supervisor.sock

sudo chmod 777 /var/run/supervisor.sock

三、  开机自动启动

1.  脚本下载方式

下载 CentOS 使用的自动启动服务脚本 centos-systemd-etcswget -O /usr/lib/systemd/system/supervisord.service  https://github.com/Supervisor/initscripts/raw/master/centos-systemd-etcs

将 supervisord 服务设为自启动systemctl enable supervisord.service

2.  自动启动(手动设置,推荐)

手动设置supervisor开机启动,配置systemctl服务,这种方式可以更多的控制启动配置项,比如指定加载目标路径下的配置文件。

a)  创建 /usr/lib/systemd/system/supervisord.service[Unit]

Description=Supervisor daemon

After=network.target

[Service]

Type=forking

ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf

ExecStop=/usr/bin/supervisorctl $OPTIONS shutdown

ExecReload=/usr/bin/supervisorctl $OPTIONS reload

KillMode=process

Restart=on-failure

RestartSec=42s

[Install]

WantedBy=multi-user.target

注意,某些版本下路径会不同,需检查路径是否正确,如centos 7.8下路径为:

/usr/local/bin/supervisord

/usr/local/bin/supervisorctl

b)  设置开机启动systemctl enable supervisord.service

systemctl daemon-reload #重载服务

可以通过下面的命令查看是否已开启成功:systemctl list-unit-files|grep enabled

c)  修改文件权限为766chmod 766 /usr/lib/systemd/system/supervisord.service

四、  利用Supervisor部署LaravelS

通过Supervisord监管主进程, 前提是laravels不能加 -d选项并且设置swoole.daemonize为false, 因为supervisor无法管理后台运行的程序,需要程序为前台运行方式。

新建 /etc/supervisor/conf.d/laravels-jumi.conf 文件(注意,请将laravels版本升级到3.4.2):[program:laravels-jumi]

command=/usr/local/php/bin/php /home/wwwroot/gps.piaoyi.org/bin/laravels start -i

numprocs=1

autostart=true

autorestart=true

startretries=3

user=root

redirect_stderr=true

stdout_logfile=/home/wwwroot/gps.piaoyi.org/storage/logs/supervisord-stdout.log

stdout_logfile_maxbytes=10MB

stdout_logfile_backups=20

五、  启动Supervisor

1.  手动启动supervisorsupervisord -c /etc/supervisord.conf

可以通过下面的命令查看服务已启动:pstree -p | grep supervisord

还可以查看log日志cat /tmp/supervisord.log

2.  修改laravel-jumi配置文件后重新载入supervisorctl reread

supervisorctl update

supervisorctl restart laravels-jumi

3.  修改 /etc/supervisord.conf后重新载入#supervisorctl reload

4.常规管理(启动、停止、状态等)systemctl start supervisord

systemctl stop supervisord

systemctl status supervisord

systemctl reload supervisord

systemctl restart supervisord

5.  其他命令supervisorctl start programxxx      启动某个进程

supervisorctl stop programxxx       停止某个进程

supervisorctl restart programxxx    重启某个进程

supervisorctl start all     启动全部进程

supervisorctl stop all      停止全部进程,注:start、restart、stop都不会载入最新的配置文件。

supervisorctl status            查看状态

supervisorctl shutdown      关闭supervisor

六、  LaravelS修改代码后自动Reload

基于inotify,仅支持Linux。

1.  安装inotify扩展

CentOS7里安装inotify方法:yum --enablerepo=epel install inotify-tools

安装后检查是否安装成功:# which inotifywait

/usr/bin/inotifywait

# /usr/bin/inotifywait --help

inotifywait 3.14

2. 安装php inotify扩展pecl install inotify

安装后在php.ini中加上extension=inotify.so

通过命令 php --ri inotify 检查是否可用inotify

Version => 2.0.0

3.  开启配置项'inotify_reload'           => [

'enable'        => env('LARAVELS_INOTIFY_RELOAD', true),//生产环境建议设为false

'watch_path'    => base_path(),

'file_types'    => ['.php'],

'excluded_dirs' => [],

'log'           => true,

]

4. 调整默认的 inotify 监控文件数量

默认的inotify监控文件数量是有限制的,通过sysctl fs.inotify.max_user_watches fs.inotify.max_queued_events fs.inotify.max_user_instances

查看得到默认值:fs.inotify.max_user_watches = 8192

fs.inotify.max_queued_events = 16384

fs.inotify.max_user_instances = 128

如果不修改这个值,在监控超过上述数字的文件时,就会报错:PHP Warning:  inotify_add_watch(): The user limit on the total number of inotify watches was reached or the kernel failed to allocate a needed resource

要永久修改这个值,通过修改  /etc/sysctl.conf 文件,加入:fs.inotify.max_user_watches = 999999

fs.inotify.max_queued_events = 999999

fs.inotify.max_user_instances = 999999

修改后,执行立即生效命令:sysctl -p

本文结束。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值