Tips&Tricks

Cron job basics. How to schedule your programs

crontab

Cron is a time-based job scheduler in Unix-like computer operating systems. People who set up and maintain software environments use cron to schedule jobs (commands or shell scripts) to run periodically at fixed times, dates, or intervals.

Format:

Min  Hour Day  Mon  Weekday
* * * * * command to be executed
┬ ┬ ┬ ┬ ┬
│ │ │ │ └─ Weekday (0=Sun .. 6=Sat)
│ │ │ └────── Month (1..12)
│ │ └─────────── Day (1..31)
│ └──────────────── Hour (0..23)
└───────────────────── Minute (0..59)

Examples

Every hour:

0 * * * *

Every 15 mins:

*/15 * * * *

Every 2 hours:

0 */2 * * *

Every Sunday midnight:

0 0 * * 0

Every reboot:

@reboot

Crontab commands

Adding tasks easily:

echo "@reboot echo hi" | crontab

Open in editor:

crontab -e

List tasks:

crontab -l [-u user]

So you can use the cron to define jobs that must be done in fixed time. But you should remember that all programs can fails. So you should check it out sometimes.

On a default installation the cron jobs get logged to

/var/log/syslog

You can easily find Cron messages using grep

grep CRON /var/log/syslog

Good luck! Don’t forget to check logs!