如果退出结果为0的情况下如何退出所有主管程序


14

我像这样用主管运行docker容器:

Docker文件

CMD ["/run.sh"]

运行

#!/usr/bin/env bash
exec supervisord -n

Supervisor-serf.conf

[group:job]
programs=serf,producer

[program:serf]
command=/start-serf-agent.sh
numprocs=1
autostart=true
autorestart=unexpected
stopasgroup=true
killasgroup=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0

start-self-agent.sh

#!/bin/bash
exec serf agent --join=serf:7946 -tag role=producer

主管服务

[program:producer]
command=/start.sh
numprocs=1
stopasgroup=true
killasgroup=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0

start.sh

#!/bin/bash
exec /producer --project=${NAME}

生产者停止后,我得到:

producer_1 |     2016/02/29 21:59:50 [INFO] serf: EventMemberLeave: 7c4fbc80af97 172.19.0.2
producer_1 | 2016/02/29 21:59:51 INF    1 stopping
producer_1 | 2016/02/29 21:59:51 INF    1 exiting router
producer_1 | 2016-02-29 21:59:51,281 INFO exited: producer (exit status 0; expected)
producer_1 |     2016/02/29 21:59:51 [INFO] agent: Received event: member-leave

但是农奴代理会保持容器处于运行状态。当生产者以状态0正确完成工作时,我想停止Docker容器。我尝试将进程加入一个组,但似乎不起作用。伙计们,我跳过了什么?请帮帮我!


Answers:


8

我解决了主管eventlistener的问题:

[program:worker]
command=/start.sh
priority=2
process_name=worker
numprocs=1
stopasgroup=true
killasgroup=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0

[eventlistener:worker_exit]
command=/kill.py
process_name=worker
events=PROCESS_STATE_EXITED

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

在Docker容器中也很好的商品过程管理


kill.py脚本的内容是什么?
PiotrKieszczyński17年

@PiotrKieszczyński帖子已更新,请看看
Vitaly Velikodny

3

这是一个稍微简化的版本,该版本使用Shell脚本而不是python脚本,并且还涵盖多种服务,如果其中任何一个失败,则将杀死整个Supervisor。

主管
$ cat /etc/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
startsecs=30
process_name=service1

[program:service2]
command=/usr/sbin/service2
user=root
autostart=true
autorestart=true
startsecs=30
process_name=service2

[eventlistener:processes]
command=stop-supervisor.sh
events=PROCESS_STATE_STOPPED, PROCESS_STATE_EXITED, PROCESS_STATE_FATAL
停止主管
$ 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

参考文献


1
您是否忘了这里的配置中的eventlsteners?而且您的配置没有引用stop-supervisor.sh
rfay

stop-supervisor.sh在哪里触发?
Pieter

@Pieter-谢谢,粘贴时将其切碎,已修复。
slm

0

这是Docker的简单解决方案。在您的中supervisord.conf,替换为:

[program:something]
command = something

有了这个:

[program:something]
command = sh -c 'something && kill 1'
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.