10

I've got a worker process which processes 1 RabbitMq message at a time. Right now, as soon as the worker exits, supervisord is restarting it (which will process the next message).

I'd like to set an interval X seconds, so that supervisord does not restart immidietly, but it waits a given amount of time before starting another worker.

Is this possible? How?

loostro
  • 499
  • 3
  • 7
  • 18

3 Answers3

10

There is no way to specify interval in supervisor program section, but what you could do is put "sleep()" into your code so that after program waits for specified period of time after it finishes with message processing.

If you don't want/cant alter the program code, you may try wrapping it into bash script, for example:

#!/bin/bash
/usr/local/bin/myprogram
sleep 30

And alter your supervisor program section to run that script, instead of your program:

command=/usr/local/bin/myprogram.sh
Jakov Sosic
  • 5,157
  • 3
  • 22
  • 33
9

I needed a simple way to run a command from inside a docker container, where there is no cron. Here is what I'm using:

[program:runevery]
directory = /my/workdir
command = sh -c "sleep 5;date >>/root/test.ts"
stdout_logfile = /var/log/supervisor/%(program_name)s.log
stderr_logfile = /var/log/supervisor/%(program_name)s.log
autorestart = true
startsecs = 0
exitcodes = 0,1,2

startsecs = 0 ensures that supervisor thinks the command was started successfully even if it exits after a few secs. Otherwise supervisor will stop restarting it, thinking it's in a loop.

Here is what you would see in /root/date.ts with the example above:

# tail -f /root/test.ts 
Tue Nov 17 20:42:58 UTC 2015
Tue Nov 17 20:43:04 UTC 2015
Tue Nov 17 20:43:10 UTC 2015
[...]

Tune the sleep to your liking and replace 'date >>/root/test.ts' with whatever you need.

This solution also comes handy if you need to run a cronjob more often than every minute.

Luca Gibelli
  • 2,611
  • 1
  • 21
  • 29
  • +1 for the clever solution. I want to do something similar. Unfortunately, this doesn't work for me as I need to get the exit code in a listener using an RPC call. Unfortunately, supervisor restarts the program immediately after exit, making the code 0, sigh... Any other ideas? – Onema Feb 13 '16 at 01:52
4
[program:yourapp]
command = bash -c "sleep 60 && exec urcmd'
startsecs = 65 ; 

and then

supervisorctl -c your_config_file reload

1. you need to use exec command, otherwise it will fork a subprogress from sleep 60 && exec your command and your progress will look like as the following

$ ps -ef|grep urcmd
work      1818  1698  0 17:35 ?        00:00:00 bash -c sleep 60 && urcmd
work      3872  1818  0 17:36 ?        00:00:00 urcmd

and then when you use supervisorctl to stop urApp, you will stop 1818 progress and leave 3872 an orphan progress

2.recommend to change the startsecs to 5 more than the sleep secs, then when you start this app and check the status, it will show you it's starting

$supervisorctl -c your_config_file status;echo;ps -ef|grep urcmd
urapp                          STARTING  
otherapp                       RUNNING   pid 13502, uptime 0:00:55

$supervisorctl -c your_config_file status;echo;ps -ef|grep urcmd
urapp                          RUNNING   pid 13503, uptime 0:00:05
otherapp                       RUNNING   pid 13502, uptime 0:00:65

else if you set the value less than the sleep secs, when you start the app and check the status,you will get a running status, but it's still sleeping cmd before real executing

3.when you change your configuration file, you need to use reload cmd or just restart your supervisord to make it work

qingxin wang
  • 141
  • 2