跳转至

如何设置 supervisor 管理的子程序只运行一次?


2017-10-26 by dongnan

目标

  1. 设置 supervisor 管理的子程序,init 优先启动(第一顺序)。
  2. 设置 init 子程序,只运行一次即可(如初始化场景)。

环境

软件版本

supervisord:3.0 On Debian 8 On Docker 17.05.0-ce

supervisor

supervisord 运行在容器之上,并管理 3个子程序:

  • init: /root/init.sh ,一个用于项目初始化的脚本程序。
  • nginx: web 服务器程序。
  • php-fpm: php 代码解析程序。

步骤

配置文件

supervisor

cat /etc/supervisor/supervisord.conf

[supervisord]
nodaemon=true
pidfile=/var/run/supervisord.pid
logfile=/var/log/supervisor/supervisord.log

[program:init]
command=/bin/bash /root/init.sh
priority=998
autorestart=false
startretries=0

[program:nginx]
command=/usr/sbin/nginx -g "daemon off;"

[program:php-fpm]
command=/usr/local/sbin/php-fpm -c /usr/local/etc/php/php.ini -y /usr/local/etc/php-fpm.conf -F

注意,priority 默认值是 999autorestart 默认值是 true

脚本文件

cat /root/init.sh

#!/bin/bash
echo `date -R` >> /tmp/init.log

启动容器

docker start test

验证

子程序输出日志

docker exec -ti test cat /tmp/init.log
Fri, 22 Sep 2017 19:18:09 +0800

守护进程日志

docker exec -ti test tail /var/log/supervisor/supervisord.log

2017-09-22 19:18:08,408 CRIT Supervisor running as root (no user in config file)
2017-09-22 19:18:08,449 INFO supervisord started with pid 1
2017-09-22 19:18:09,452 INFO spawned: 'init' with pid 8
2017-09-22 19:18:09,454 INFO spawned: 'nginx' with pid 9
2017-09-22 19:18:09,457 INFO spawned: 'php-fpm' with pid 10
2017-09-22 19:18:09,476 INFO exited: init (exit status 0; not expected)
2017-09-22 19:18:09,524 INFO gave up: init entered FATAL state, too many start retries too quickly
2017-09-22 19:18:10,567 INFO success: nginx entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)2017-09-22 19:18:10,567 INFO success: php-fpm entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)

参考

  • http://zongming.net/read-1151#1210

欢迎关注微信公众号: 运维录

Back to top