Skip to main content

How can I set up Cronjobs?

Login to your server using an ssh client. Type crontab -e to edit your cronjobs.

Limitations

Your crontab file has to end with a new line, otherwise the last cronjob will not be executed.

Lock functionality

Cronjobs should have a locking functionality to prevent processing the same command multiple times. This can be provided through a built-in function in the application, the script, or by using the command run-one.

You can find more information about the lock functionality in this support article: /a/u8kZWWHIqeo

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 cronjob runs every minute, in every hour, on every day of the month, every month, on every weekday.

/home/www-data/scripts/job.sh The script to execute with absolute path definition.

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 cronjobs 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 cronjobs will be sent as an email to the given address.

Optional output control

  • The optional parameter &> /dev/null "throws away" the output of the command in the cronjob (SYSOUT)
  • The optional parameter >/dev/null 2>&1 "throws away" both the output and the error output of the command in the cronjob (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

How can I prevent Cronjobs from running multiple times?

It is common to execute cronjobs frequently, for example, every minute.

The execution time, in tendency, increases with a growing data set.

If the execution time of a cronjob 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 cronjobs will only run one instance of a job at a time.

The wrapper can be used as a prefix for cronjobs 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: https://manpages.ubuntu.com/manpages/focal/man1/run-one.1.html