Cron Jobs
The cron
command-line utility is a job scheduler. You can use cron jobs, to run periodically at fixed times, dates, or intervals.
Create or Edit Jobs
Login to your server using an ssh client. Type crontab -e
to create or edit your cron jobs.
Limitations
Your crontab file has to end with a new line, otherwise the last cron job will not be executed.
Run Times
The run times in the crontab file are defined as followed this:
┌───────────── minute (0 - 59)
│ ┌───────────── hour (0 - 23)
│ │ ┌───────────── day of month (1 - 31)
│ │ │ ┌───────────── month (1 - 12)
│ │ │ │ ┌───────────── weekday (0 - 6)
│ │ │ │ │
* * * * * /home/www-data/scripts/job.sh
*
A wildcard for every minute / hour and so on. This cron job runs every minute, in every hour, on every day of the month, every month, on every weekday.
More examples can be found on crontab.guru/examples.html.
Shell, PATH and mailings
SHELL=/bin/sh
PATH=/home/www-data/scripts:/usr/local/bin:/bin:/usr/sbin:/usr/bin
MAILTO=user@example.org
* * * * * job.sh
The cron daemon runs with a basic PATH variable: /usr/bin:/bin
.
If you want to use commands with relative paths, you can extend your PATH
at the beginning of the crontab file. Alternatively, you can use absolute paths.
The default shell of the cron jobs can be defined with the variable SHELL
. Without changes, the dafault shell is dash
.
By setting the variable MAILTO=user@domain.org
, the output of your cron jobs will be sent as an email to the given address.
Lock Functionality
Cron jobs should have a locking functionality to prevent processing the same command multiple times.
It is common to execute cron jobs frequently, for example, every minute. The execution time, in tendency, increases with a growing data set.
If the execution time of a cron job is greater than the frequency, job overlaps will occur.
This will frequently lead to an exponential increase in execution time as well as system resource usage (CPU, RAM).
We have made available the run-one
wrapper which will ensure cron jobs will only run one instance of a job at a time.
The wrapper can be used as a prefix for cron jobs like this:
* * * * * /usr/bin/run-one /home/www-data/scripts/job.sh &> /dev/null
run-one
is a wrapper from the likewise named Ubuntu package.
More information about "run-one" and similar helpful wrappers can be found in the Ubuntu manpages:
Optional Output Control
- The optional parameter
&> /dev/null
"throws away" the output of the command in the cron job (SYSOUT) - The optional parameter
>/dev/null 2>&1
"throws away" both the output and the error output of the command in the cron job (SYSOUT+SYSERR)
Examples
Every 5th Minute at Every Hour
Request an URL using curl
:
5 * * * * /usr/bin/run-one /usr/bin/curl https://domain.org/cron/run &> /dev/null
Every 4th Hour
Information: The script has to be executable:
chmod u+x /home/www-data/scripts/refresh_cache.sh
0 */4 * * * /usr/bin/run-one /home/www-data/scripts/refresh_cache.sh &> /dev/null
Every Wednesday at 00:30 AM
Information: The script has to be executable:
chmod u+x /home/www-data/scripts/weekly_report.sh
30 5 * * 3 /usr/bin/run-one /home/www-data/scripts/weekly_report.sh &> /dev/null
More information regarding the syntax of the crontab file and more examples can be found in the following Wikipedia article: https://en.wikipedia.org/wiki/Cron