How to Automate your Life in Linux using Cron

  • : Function ereg() is deprecated in /home/sparxsys_linux/www.linuxindya.com/includes/file.inc on line 647.
  • : Function ereg() is deprecated in /home/sparxsys_linux/www.linuxindya.com/includes/file.inc on line 647.
  • : Function ereg() is deprecated in /home/sparxsys_linux/www.linuxindya.com/includes/file.inc on line 647.
  • : Function ereg() is deprecated in /home/sparxsys_linux/www.linuxindya.com/includes/file.inc on line 647.
  • : Function ereg() is deprecated in /home/sparxsys_linux/www.linuxindya.com/includes/file.inc on line 647.
  • : Function ereg() is deprecated in /home/sparxsys_linux/www.linuxindya.com/includes/file.inc on line 647.
  • : preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/sparxsys_linux/www.linuxindya.com/includes/unicode.inc on line 311.
  • : preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/sparxsys_linux/www.linuxindya.com/includes/unicode.inc on line 311.
  • : preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/sparxsys_linux/www.linuxindya.com/includes/unicode.inc on line 311.
  • : preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/sparxsys_linux/www.linuxindya.com/includes/unicode.inc on line 311.
in

One of the great benefits of using computers is that they are ideal candidates for performing the uninteresting jobs humans get tired of doing. Most of us would be bored senseless making database
backups everyday, cleaning out disks, updating report files, and managing system logs. These activities are typically linear, mundane processes that are straightforward enough to require little intervention, which makes them ideal candidates for automation.

One of the most fundamental pieces of the toolbox within all Unix-type operating systems is called cron. This small utility is like an alarm clock. When the alarm goes off, it tells the computer to do whatever you have configured it to do. As an example, if you perform a backup every day, you can get cron to perform this for you at a specific time. Another example is if you need to generate a weekly report about a project—cron can generate this report automatically.

An important point to note is that cron does not actually perform these activities itself. cron's only function is to trigger a specific process or series of commands at a certain time. When the specified time occurs, the commands and tools that are needed to complete the activity are run. As such, to automate a process on your computer, you need to determine how you can complete your task with a series of command-line tools. This usually means you need to create a script with the commands for cron to run at specified intervals.

Create a Cronjob

The cron program reads in a special file called a crontab. This file specifies jobs to be run and their times. You can access this file by running:

[email protected]:~$ crontab -e

This command uses the system's default command-line editor so that you can edit the crontab. If you want to set this editor to a different one (such as jed), set the $EDITOR environment variable prior to editing crontab:

[email protected]:~$ export EDITOR=jed

If this is your first use of cron, it is likely that your crontab is empty; unless some special system cronjobs were added (these automatically added crontabs are quite common in a number of Linux tools and utilities). Each cronjob consists of a single line containing the time of the cronjob, as well as the command to run.

A simple example of a crontab entry is:

30 3 1 * * /home/foo/createreport.sh

This example runs a script called createreport.sh at 3:30 a.m. on the first day of each month. The numbers on the left side of the line identify the time at which the script should be run, and the /home/foo/createreport.sh part shows the location and name of the script. It is always important that you use full and absolute paths when referring to scripts and files, as cron does not understand relative paths.

The numbers and stars on the left side of the line are entries in five different columns. Here are the columns, from left to right.

Minutes

This column indicates the minute part of the time in which the cronjob is to be run.

Hours

This column indicates the hour of the job and is given in 23-hour time (0_23). The time between midnight and 1:00 a.m. is the 0 hour.

Day

This is the number of the day in the month when the job is to be run.

Month

This is the month number between 1 and 12.

Day of the week

This is the day of the week, between 0 and 6, where 0 is Sunday.

In this example, you indicated the time as 3:30 a.m., and the day as the first day of the month. The two asterisks (*) show you want to run the script for each time increment in that column—i.e., every day or month. As per the example, the * in the month and day-of-the-week columns means the cronjob will run every month and every day.

When you have finished editing the crontab, saved the file, and exited, the changes are automatically enabled. You can view the entries in the crontab file by running:

[email protected]:~$ crontab -l

You can also remove the entire crontab by using the -r option:

[email protected]:~$ crontab -r