cron

Table of Contents

1. Description

Both Cron and Anacron can schedule execution of recurring tasks to a certain point in time defined by the exact time, day of the month, month, day of the week, and week.

2. How it works

Example of job definition:
.---------------- minute (0 - 59)
|  .------------- hour (0 - 23)
|  |  .---------- day of month (1 - 31)
|  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
|  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
|  |  |  |  |
*  *  *  *  * user-name  command to be executed

2.1. List user crontab

crontab -l

2.2. Edit user crontab

crontab -e

2.3. Remove user crontab

crontab -r

2.4. Backup user crontab to a remote file

crontab -l | ssh user@remote 'cat > ~/backup/crontab.txt'

2.5. Restore user crontab from a file

crontab file-containing-cronjobs.txt

3. Troubleshooting cronjobs

Cronjobs can have different environments variables than the user, e.g. the $PATH not including binaries from /usr/local/bin, compare both environments with this cronjob

0 * * * * env > /tmp/cronjob-environment-variables.txt

Using the env command to print user environment

env > /tmp/user-environment-variables.txt

Then compare both files to check if cronjobs are missing something

diff /tmp/cronjob-environment-variables.txt /tmp/user-environment-variables.txt

4. Related nodes

  • at to schedule a one-time task

5. References