Hack 74. Crontab Basic Guide

    Using cron you can execute a shell-script or Linux commands at a specific time and date. For example a sysadmin can schedule a backup job that can run every day.

    Description of Cron fields.

    Following is the format of the crontab file.

    1. {minute} {hour} {day-of-month} {month} {day-of-week} {full-path-to-shell-script}
    • minute: Allowed range 0 – 59
    • hour: Allowed range 0 – 23
    • day-of-month: Allowed range 0 – 31
    • Day-of-week: Allowed range 0 – 7. Sunday is either 0 or 7.

    Crontab examples

    2. Run backup every weekday (Mon – Fri) at 11:59 p.m.

    1. 59 11 * * 1,2,3,4,5 /root/bin/backup.sh

    Following will also do the same.

    3. Execute the command every 5 minutes.

    1. */5 * * * * /root/bin/check-status.sh

    5. Execute 11 p.m on weekdays.

    1. 0 23 * * 1-5 /root/bin/incremental-backup.sh

    Crontab Options

    • crontab –e : Edit the crontab file. This will create a crontab, if it doesn’t exist
    • crontab –l : Display the crontab file.
    • crontab -ir : This will prompt user before deleting a crontab.