Skip to Content

Cron Job

Alaa's picture

Cron is a tool that lets you specify jobs (could be command or scripts or whatever you like) in the file /etc/crontab these commands will then be executed according to a particular schedule (for instance every Wednesday at 3:00 do a fsck, or every day at 8:00 play, or even every 1st of Jan say happy new year).

usually your crontab file will look like this:- SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root HOME=/
  1. run-parts 01 * * * * root run-parts /etc/cron.hourly 02 4 * * * root run-parts /etc/cron.daily 22 4 * * 0 root run-parts /etc/cron.weekly 42 4 1 * * root run-parts /etc/cron.monthly

    the first few lines are environment variables and explain themselves the run-parts is the important one its obvious here that my system has four separate tables for hourly, daily, weekly and monthly jobs

    Now let us see what is the run-parts

    • The numbers in the beginning stand for :-
    • minute
    • hour
    • dayofmonth
    • month
    • dayofweek
    • then comes the user that will be used to run these jobs.
    • Then the last field is the job (run-parts is a small tool that runs scripts by guessing which interpreter to use, so run-parts foo is basically equivalent to bash foo, or python foo or whatever).

    lets us notice :-

    • The hourly jobs are executed on the first minute of every hour in every day in every month (could be fetchmail or sendmail etc.)
    • The daily jobs are executed on the second minute of the fourth hour of every day in every month (a fortune maybe or a some check on your logs)
    • The monthly jobs are executed on the 22ND minute of the 4Th hour of anyday in anymonth that is a Sunday(thats what the last 0 stands for, and this could be a fsck or a cleanup of your tmp dirs)
    • The monthly jobs are executed on the 42ND minute of the 4Th hour of the first day of any month (a backup to your /etc maybe)

    The happy new year setting will look like this:- 01 0 1 1 * alaa echo 'happy new year' 02 0 1 1 * alaa mail -s 'happy new year' friends < greetings.txt

    Thats Will Do Two Things:-

    1. on the first minute at midnight on the first of Jan write happy new year on the console.
    2. on the 2ND minute of the same date send an email to all friends with

    the subject 'happy new year' and have the content of greetings.txt as the message body.

    however cron is of limited use to your usual home user since it assumes your pc is on all the time so if I don't open my pc on 1st of Jan until the morning (which is probably what will happen) my friends will not get a message from me and my computer will not greet me thus ruining the whole year from day one.

    a tool that would be more useful is anacron, it doesn't assume your computer will be on all the time but rather you have to describe a frequency for this particular job (like I want this done once a month and that done once a year). however anacron is a totally different story.

    both cron and anacron are useful if you like your command/script to run only once (not periodically) in this case you use the tool at

    read the cron and crontab man pages:-

    • man cron
    • man crontab
    • man 5 crontab

    you may want to check the man pages for anacron and at too your GNU/Linux system comes with a whole lot of docs and manuals try to read as many of them as you can.

Dr. Radut | book