Home > linux > Linux Automation for Absolute Beginners

Linux Automation for Absolute Beginners

Linux command-line 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 on your system 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.

Categories: linux Tags: