Scheduling for Absolute Beginners

March 27, 2007 |

Linux scheduling is done via a program called cron. Cron is a system program that is running all the time and is similar to the Windows scheduler which allows you to run commands/programs at predefined times and intervals. To find out if it’s running do the following:

$ ps aux | grep crond

root 2223 0.0 0.0 4508 840 ? Ss Mar09 0:00 crond

The output above shows that crond is running.

The commands or scripts that you want cron to run are defined in a file called crontab, and every user has their own independent crontab file. The crontab file has 7 specific fields. Each of the sections is separated by a space, with the final section having one or more spaces in it. No spaces are allowed within Sections 1-5, only between them. Sections 1-5 are used to indicate when and how often you want the task to be executed. This is how a cron job is layed out:

minute (0-59), hour (0-23, 0 = midnight), day (1-31), month (1-12), weekday (0-6, 0 = Sunday), Task

Further definition of each field:

Minute = Minute of the hour, 00 to 59. * Will indicate every minute (details later)
Hour = Hour of the day in 24-hour format, 00 to 23. * Will indicate every hour (details later)
Day = Day of the month, 1 to 31. * Will indicate every day (details later)
Month = Month of the year, 1 to 12. * Will indicate every month (details later)
Weekday = Day of the week, 3 chars - sun, mon, tue, or numeric (0=sun, 1=mon etc)
Task
= The command you want to execute

NOTE: An asterisk (*) in a field can be used so that every instance (every hour, every weekday, every month, etc.) of a time period is used.

Some examples of a crontab entry:

01 02 * * * /usr/bin/program

- The above example will run /usr/bin/program at 2:01AM on every day of every month.

*/5 * * * * /usr/bin/program

- The */5 is known as a short form equivalent to 0,5,10,15,20 etc…basically executing ‘program’ every 5 minutes. Other examples are */2 for every 2 minutes, */30 every 30 minutes and so on.

00 23 * * * /usr/bin/program

- Run a task at 11PM every night.

15 02 * * 1-5 /usr/bin/programĀ 

- Run task at 2:15am every weekday morning

0 0 1,15 * * /usr/bin/program

- Run task the first and fifteenth of each month

0 0 * * 1 /usr/bin/program

- Run task every Monday.

NOTE: When entering commands into the crontab file, make sure that you use absolute pathnames (i.e /usr/bin/program.sh) to specify the script in crontab. This is because when cron executes your task, it does not have the same $PATH variable defined.

Crontab Options

  • The -l option causes the current crontab to be displayed on standard output.
  • The -r option causes the current crontab to be removed.
  • The -e option is used to edit the current crontab using the editor specified by the VISUAL or EDITOR environment variables.
  • The -u option is used by root to change other user’s crontab.

After you exit from the editor, the modified crontab will be checked for accuracy and, if there are no errors, installed automatically.

To create a crontab file, simply type ‘crontab -e’ at your command prompt. This’ll bring up crontab in your default editor. If you prefer a different editor, you can type:

EDITOR=vi && crontab -e

You can replace vi with your favorite editor :)

You can find more examples by typing: ‘man 5 crontab’ at your shell prompt. On second part of this article, I will include many tips and tricks one can do with the cron.



Comments

11 Comments so far

  1. Linux Scheduling for Absolute Beginners, Part 2 : lxpages.com blog on April 12, 2007 4:35 pm

    […] Scheduling for Absolute Beginners […]

  2. Severin on April 13, 2007 6:38 am

    If this article is aimed at Absolute Beginners, wouldn’t it make more sense to talk about anacron? As far as I’ve understood, cron is only suitable for servers running 24/7, which will hardly be administered by Absolute Beginners.

  3. virens on April 14, 2007 6:50 am

    Interesting and helpfull article.
    For example, my Linux box is not a server, but it works 24/7. And quick help about cron is realy usefull.
    Thanks for article again.

  4. rjc on May 4, 2007 6:02 am

    If we’re talking about Linux, and we are, the days of the week can be numbered from 0 to 7 - 0 and 7 being Sunday (not in BSD or AT&T cron).

  5. Linux file recovery : lxpages.com blog on June 21, 2007 4:34 pm

    […] rsync to copy the important directories and files to another system or partition. You can set a crontab to do this daily or even hourly. Makes restoring so much easier in case you delete something by […]

  6. 101 Ubuntu Tutorials : lxpages.com blog on July 11, 2007 3:06 pm

    […] Scheduling for Absolute Beginners […]

  7. Blux 2.0 » Blog Archive » 101 Tips, trucos y tutoriales de Ubuntu on July 20, 2007 12:21 am

    […] Scheduling for Absolute Beginners […]

  8. Idetrorce on December 15, 2007 5:48 am

    very interesting, but I don’t agree with you
    Idetrorce

  9. rfnaufal blog! on December 31, 2007 1:22 am

    […] Scheduling for Absolute Beginners […]

  10. Rot-13 [de | en] Coder » Blog Archive » 101 tips, triks dan tutorial Ubuntu on January 22, 2008 5:10 am

    […] Scheduling for Absolute Beginners […]

  11. Answers to Linux Questions | Ubuntu Linux Help on March 13, 2008 10:13 am

    […] There’s a good tutorial here: Scheduling for Absolute Beginners […]

Name

Email

Website

Speak your mind

  • Categories

  • Sponsors