Understanding crontab

If we need to run a specific task repetitively, then the solution is to use crontab. The syntax of the command is as follows:

    $ crontab -e

This will open a new editor. The following diagram is the syntax to add tasks. The fields to use for repeating tasks at a particular time are explained here:

Finally, to save the jobs, use the following:

    Press Esc then type :wq
    

The preceding operations will save the job and quit crontab.

The following are a few examples of the crontab command:

  • Use the following command to run a script every hour at the fifth minute, every day:
    5 * * * *      $HOME/bin/daily.job >> $HOME/tmp/out  2>&1
  • Use the following command to run 5 minutes after midnight every day:
    5 0 * * * $HOME/bin/daily.job ...

Get Learning Linux Shell Scripting - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.