Shell-Script

在 Docker 容器中的一項服務崩潰後,Supervisord 不退出

  • July 22, 2018

我有以下主管配置:

[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 崩潰,容器會繼續執行,就好像一切正​​常。我怎樣才能改變這種行為,以便在一個服務崩潰時整個容器退出?

這個 SF Q&A 的標題是:如果一個退出結果為 0,如何退出所有主管程序聽起來就像您要找的一樣。

**注意:**此方法使用eventlistener

範例 #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

參考

引用自:https://unix.stackexchange.com/questions/457473