我有以下supervisord配置:
[supervisord]
nodaemon=true
logfile=NONE
[program:service1]
command=/usr/sbin/service1
user=someone
autostart=true
autorestart=true
startsecs=30
[program:service2]
command=/usr/sbin/service2
user=root
autostart=true
autorestart=true
startsecs=30
我在 docker 容器中使用此配置。问题是,如果 service1 崩溃,容器会继续运行,就好像一切正常一样。我怎样才能改变这种行为,以便在一个服务崩溃时整个容器退出?
答案1
本次 SF 问答标题为:如果退出时结果为 0,如何退出所有主管进程听起来像你正在寻找的东西。
笔记:这种方法使用一个事件监听器。
例子#1
[supervisord]
nodaemon=true
logfile=NONE
[program:service1]
command=/usr/sbin/service1
user=someone
autostart=true
;autorestart=true ; disabled
;startsecs=30 ; disabled
process_name=service1
[program:service2]
command=/usr/sbin/service2
user=root
autostart=true
;autorestart=true ; disabled
;startsecs=30 ; disabled
process_name=service2
[eventlistener:service1_exit]
command=kill.py
process_name=service1
events=PROCESS_STATE_EXITED
[eventlistener:service2_exit]
command=kill.py
process_name=service2
events=PROCESS_STATE_EXITED
剧本kill.py
:
$ cat kill.py
#!/usr/bin/env python
import sys
import os
import signal
def write_stdout(s):
sys.stdout.write(s)
sys.stdout.flush()
def write_stderr(s):
sys.stderr.write(s)
sys.stderr.flush()
def main():
while 1:
write_stdout('READY\n')
line = sys.stdin.readline()
write_stdout('This line kills supervisor: ' + line);
try:
pidfile = open('/var/run/supervisord.pid','r')
pid = int(pidfile.readline());
os.kill(pid, signal.SIGQUIT)
except Exception as e:
write_stdout('Could not kill supervisor: ' + e.strerror + '\n')
write_stdout('RESULT 2\nOK')
if __name__ == '__main__':
main()
import sys
main issue I forgot to point to **process_name**
例子#2
此示例显示了一种更简化的方法,它仍然使用事件侦听器,但它显示了如何执行上面所示的相同操作,但只使用单个侦听器和 shell 脚本。
执行查杀操作的 shell 脚本:
$ cat stop-supervisor.sh
#!/bin/bash
printf "READY\n";
while read line; do
echo "Processing Event: $line" >&2;
kill -3 $(cat "/var/run/supervisord.pid")
done < /dev/stdin
Supervisord.conf:
$ cat supervisord.conf
[supervisord]
nodaemon=true
loglevel=debug
logfile=/var/log/supervisor/supervisord.log
pidfile=/var/run/supervisord.pid
childlogdir=/var/log/supervisor
[program:service1]
command=/usr/sbin/service1
user=someone
autostart=true
;autorestart=true ; disabled
;startsecs=30 ; disabled
process_name=service1
[program:service2]
command=/usr/sbin/service2
user=root
autostart=true
;autorestart=true ; disabled
;startsecs=30 ; disabled
process_name=service2
[eventlistener:processes]
command=stop-supervisor.sh
events=PROCESS_STATE_STOPPED, PROCESS_STATE_EXITED, PROCESS_STATE_FATAL