Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run a supervisord program conditionally

Tags:

supervisord

Lets say I have two programs in supervisord. Is there a way to run the first program (background process) conditionally without having to move that to a separate script file?

[supervisord]
nodaemon=true
logfile=/tmp/supervisord.log

#Need this program to run conditionally - say based off an environment variable being set
[program:prog1]
command=/bin/prog1

[program:prog2]
command=/bin/prog2 -DFOREGROUND
like image 417
Nagendra Kakarla Avatar asked Mar 29 '16 16:03

Nagendra Kakarla


People also ask

How do I start a Supervisord process?

To start supervisord, run $BINDIR/supervisord. The resulting process will daemonize itself and detach from the terminal. It keeps an operations log at $CWD/supervisor. log by default.

How do I know if Supervisorctl is running?

The supervisor service runs automatically after installation. You can check its status: sudo systemctl status supervisor.

How do I set environment variables in supervisor?

Refer to http://supervisord.org/subprocess.html#subprocess-environment for more info. Environment Variables PORT=8000 command=uwsgi --ini uwsgi. ini --http :<PORT> How do I use it on the command line?

How do you stop a supervisor process?

Start / Stop a Service To start a non-running service or stop a running one, use supervisorctl start my-daemon and supervisorctl stop my-daemon . To restart a service, you can also use supervisorctl restart my-daemon .


2 Answers

Resurrecting an old thread, but @nelson's answer is incomplete. Namely, environment variables in supervisor need to be formatted as %(ENV_YOURVARIABLE)s in order to be recognized (notice the prefix ENV_, % sign before brackets and s at the end). Additionally, you have to export it before sending it to bash script:

[program:prog1]
command=bash -c "export INIT_PROG=%(ENV_INIT_PROG)s  && ./conditional-startup.sh"

conditional-startup.sh:

#!/bin/bash

if [ $INIT_PROG = "some value" ]; then
    /bin/prog1
fi

See also:

  • Supervisor configuration
like image 140
Miljen Mikic Avatar answered Sep 20 '22 09:09

Miljen Mikic


Pass conditional value to SERVER1_START and control the flow.

[program:somecommand]
command=bash -c "if [ ${SERVER1_START} = "VALUE-X" ]; then /apps/bin/start.sh 
/apps/server.properties; fi"
like image 42
nelson Avatar answered Sep 17 '22 09:09

nelson