Thursday, April 30, 2015

Unix Cron Scheduler

What is a Scheduler

Wiki defines a job scheduler as a computer application for controlling unattended background program execution. i.e if you want your computer to do a task at a particular time without you being there to start the task then you use a scheduler. It could be as simple as playing your favorite song at 6:00 AM every morning to wake you up or finding the most popular post of the day every night.

Cron is the tool that is popular on most Unix/Linux system to setup such tasks.

Scheduling with cron

List all the scheduled jobs on the system.

$ crontab -l

Create or Modify an existing scheduled task.

$ crontab -e

This opens an editor which can be used to modify or add new tasks. You can edit the file and save it.

  • The first line runs the task "rm -Rf /tmp/* > /dev/null" every at 0:00 Hours
  • The second line runs the task "python runDBBackDaily every night at 1:15 AM"

Syntax for cron scheduler

*     *     *     *     *  command to be executed
-     -     -     -     -
|     |     |     |     |
|     |     |     |     +----- day of week (0 - 6) (Sunday=0)
|     |     |     +------- month (0 - 12)
|     |     +--------- day of month (1 - 31)
|     +----------- hour (0 - 23)
+------------- min (0 - 59)

Examples

string
meaning
@reboot
Run once, at startup.
@yearly
Run once a year, "0 0 1 1 *".
@annually
(same as @yearly)
@monthly
Run once a month, "0 0 1 * *".
@weekly
Run once a week, "0 0 * * 0".
@daily
Run once a day, "0 0 * * *".
@midnight
(same as @daily)
@hourly
Run once an hour, "0 * * * *"

Note

Default editor opened by crontab -e can be changed by adding following lines to ~/.bashrc file


export EDITOR="nano"
export VISUAL="nano"

References

1.    https://help.ubuntu.com/community/CronHowto
2.    http://en.wikipedia.org/wiki/Cron

No comments:

Post a Comment