Cron Job

On Linux, cron is a utility for automating tasks. This means we can schedule a task to run “every minute” and the cron would all that task every minute. Task can be deleting old files, back up database at 1.00am at midnight etc.

The daemon that needs to run for cron to work is “crond” and the program is “crontab”.

Following command on the shell allows you to write your cron jobs in the editor:

crontab -e

Once, you log out, they are saved in the /var/spool/cron/ directory. The crond daemon reads this regularly and executes the cron tasks at the scheduled time.

For scheduling the cron task, time is represented using following fields:

minute (0-59)
hour (0-23)
day of month (1-31)
month (1-12, or name such as jan, feb, etc)
day of week ( 0-6(6 = Sunday) or name such as mon, tue,etc)

Example code in ‘crontab -e’:

0 * * * * /home/cron/

0 */2 * * * /home/cron/

This means cron job to run every alternate hour. “*/2”, means every alternate hour cron job scheduling.

30 11 * * * /home/cron/ >/dev/null

This runs cron task every day at 11.30 pm.