1

I have the following supervisord configuration:

[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

I'm using this configuration in a docker container. The problem is that if service1 crashes, the container continues to run as if everything is normal. How could I change this behaviour, so that the whole container exits if one service crashed?

1

This SF Q&A titled: How to exit all supervisor processes if one exited with 0 result sounds like what you're looking for.

NOTE: This approach uses an eventlistener.

Example #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

The kill.py script:

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

Example #2

This example shows a more streamlined approach, it's still using an eventlistener but it shows how you can do the same shown above, but with just a single listener instead & shell script.

The shell script that does the killing:

$ 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

The 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

References

  • yeah, I solved it. Each service must run in foreground. Thanks again dude! – chevallier Jul 21 '18 at 12:33
  • @chevallier - sure thing, I"ll update it per your comments. I figured those wouldn't be wanted but wasn't sure if you want those autorestart=true and startsecs=30. I've left them but commented them out. – slm Jul 21 '18 at 13:53
  • @chevallier - updated some of this, what method did you use to run the services in foreground? redirect_stderr=true or something else? - supervisord.org/subprocess.html. – slm Jul 21 '18 at 14:11
  • I just ran each process with his corresponding "no daemon" option, e.g. nginx -g "daemon off;" or php-fpm7.0 --nodaemonize, that stopped it from crashing and I had no more errors in the supervisord logs. Also I think you must use ; instead # to add comments to supervisord.conf. – chevallier Jul 22 '18 at 7:16
  • @chevallier - OK cool. Oh yeah on the comments, I'll fix thanks. – slm Jul 22 '18 at 7:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.