source

Supervisor and Crontab working together

Supervisor helps up control a number of processes in UNIX like operating systems, Crontab is a task scheduler that runs scripts.

Supervisor Setup

  1. Go to your server / device CLI
  2. Install supervisor (sudo apt-get install) — if it fails try running sudo apt-get update
  3. Create a script, any script! — I will create one at /usr/local/bin/template.sh
  4. Give it exec permission — chmod +x /usr/local/bin/template.sh
  5. Configure supervisor to start your script — mcedit /etc/supervisor/conf.d/template.conf
# Define name and path to the 
[program:your_program_name]
command=/usr/local/bin/tempalte.sh
# Define behaviour
autostart=true
# false - do not restart program after it exists
# true - restart the program after it exists
# unexpected - only restart if the program exists with an unexpected error code
autorestart=true
# Where the output of the script should go in each case
stderr_logfile=/var/log/template.err.log
stdout_logfile=/var/log/tempalte.out.log
Supervisor schema

You will have to make a supervisor configuration for every script in the /conf.d directory

For indepth configuration go here.

6. Once we have created the config file, inform supervisor about it — supervisorctl reread

7. Tell it to enact any changes — supervisorctl update

Any time we make changes to any configuration file we have to do steps 6 and 7 to bring them into effect, otherwise supervisor will run on the previous configurations.

Crontab Setup

  1. Go in /etc and locate /cron.d
  2. Edit /etc/cron.d crontab -e

The format of the crontab setup is as follows:

a b c d e command output

a — minute (0–59)
b — hour(0–23)
c — day(0–31)
d — month(0–12)
e—day of the week(0–7)

Or we can use the * wildcard to specify any. Examples:

# at every minute
* * * * * command
# at every minute on monday
* * * * 1 command
# at every minute on 1st day of the month and every monday
* * 1 * 1 command
# at every minute 5 on day of the month 1 through 3
5 * 1-3 * * command
... and so on

If we want to:

Disable the output, just point it like this:

* * * * * command >/dev/null 2>&1

List our existing crontabs:

crontab -l

How do we use supervisor and crontab together?

Supervisor will be configured to start commands and make sure they are restarted if they fail, crontab will run them.