python supervisord program dependency
supervisord
does not directly support dependencies. Your options instead are:
Use priorities. Set
priority
forA
to a low value and it'll be started beforeB
, and shut down afterB
. The default value forpriority
is999
.If you put the two programs into one group as well, that'd let you start and stop them in tandem, with the priorities regulating their start and stop order.
Write an event listener that listens for
PROCESS_STATE
STARTING
-to-RUNNING
transition andSTOPPING
events forA
, then instructsupervisord
to start and stopB
according to those events. HaveA
autostart, but disable autostarting forB
, so that the event handler controls it.
If you want to take a shortcut, and skip reading the documentation about event listeners and skip modifying your programs so they understand events, then:
Instead of starting program B
(which depends on A
) directly, you could start a Bash script that sleeps until A
has been started, and then starts B
. For example, if you have a PostgreSQL database and a server that shouldn't start before PostgreSQL:
[program:server]autorestart=truecommand=/.../start-server.sh[program:postgres]user=postgresautorestart=truecommand=/usr/lib/postgresql/9.3/bin/postgres ...
And then inside start-server.sh
:
#!/bin/bash# Wait until PostgreSQL started and listens on port 5432.while [ -z "`netstat -tln | grep 5432`" ]; do echo 'Waiting for PostgreSQL to start ...' sleep 1doneecho 'PostgreSQL started.'# Start server.echo 'Starting server...'/.../really-start-the-server
One solution is to use supervisorctl: set autostart to false for program B, and in the program launched by A, write supervisorctl start B
.
Example:
supervisor.cfg
:
[supervisord]nodaemon=falsepidfile=/tmp/supervisor.pidlogfile=/logs/supervisor.log[unix_http_server]file=/var/run/supervisor.sock[rpcinterface:supervisor]supervisor.rpcinterface_factory=supervisor.rpcinterface:make_main_rpcinterface[supervisorctl]serverurl=unix:///var/run/supervisor.sock[program:A]command=do_a[program:B]command=do_bautostart=false
The do_a
program contains:
#!/bin/bash#do thingssupervisorctl start B
TBH it is the solution suggested by @drrzmr but I did not understand it at the time.