8i | 9i | 10g | 11g | 12c | 13c | 18c | 19c | 21c | 23c | Misc | PL/SQL | SQL | RAC | WebLogic | Linux

Home » Articles » Linux » Here

CRON : Scheduling Tasks on Linux

This article introduces CRON on Linux, with specific reference to the information needed for the RHCSA EX200 and RHCE EX300 certification exams.

Remember, the exams are hands-on, so it doesn't matter which method you use to achieve the result, so long as the end product is correct.

crontab File Format

Each line in the crontab file represents a separate scheduled task. The entries have the following elements.

field          allowed values
-----          --------------
minute         0-59
hour           0-23
day of month   1-31
month          1-12
day of week    0-7 (both 0 and 7 are Sunday)
command        Valid command or script.

The first 5 fields can be specified using the following rules.

*       - All available values or "first-last".
3-4     - A single range representing each possible from the start to the end of the range inclusive.
1,2,5,6 - A specific list of values.
1-3,5-8 - A specific list of ranges.
0-23/2  - Every other value in the specified range.

The following entry runs a cleanup script called "/root/weekly_cleanup.sh" at 01:00 each Sunday. Any output or errors from the script are redirected to the "/tmp/weekly_cleanup.log" file, to prevent a build up of mails to root.

0 1 * * 0 /root/weekly_cleanup.sh >> /tmp/weekly_cleanup.log 2>&1

It is a good idea to consider where the standard output and standard error should be directed. Use regular output redirection to control this. Using "/dev/null" means you are throwing it away.

Editing crontab File

You can directly edit the current users crontab file using the "crontab -e" command. Edit the file using the standard vi commands, then save it and the scheduler will instantly see the changes. Using the "-u" option allows you to edit the specified users crontab.

# crontab -e
# crontab -e -u oracle

An alternative, possibly safer, method is push the current crontab contents to a file, edit the file, then load it. Using this method you can keep a copy of the previous settings in case you need to revert.

# crontab -l > oldcron
# cp oldcron newcron
# vi newcron
# crontab newcron

The "-r" options removes the crontab file.

# crontab -r -u tim_hall

The user-specific crontab files are located in the "/var/spool/cron" directory, but you should avoid editing them directly. Scheduled system tasks are located in the "/etc/cron.d" directory.

Predefined Intervals

An alternative to editing the crontab is to place executable scripts into one of the following directories. The script will be run at the appropriate interval.

/etc/cron.hourly
/etc/cron.daily
/etc/cron.weekly
/etc/cron.monthly

This is actually using anacron, rather than cron, but it achieves a similar goal.

Prevent Overlapping Runs

This is not part of the exam criteria, but it is an important point when using CRON.

In some cases you will have jobs scheduled to run at very short intervals. If a job is slow or hangs, it's possible the next job will start before the current one completes. If this is a problem, you need to include some code in your scripts to prevent it. The script below represents one way to control this.

#!/bin/ksh

# Check is process is already running.
# If lock file already exists exit.
LOCK_FILE=/tmp/my-process.lock
if [ -e $LOCK_FILE ]
then
  exit 0;
fi

# Create a lock file containing the current
# process id to prevent other tests from running.
echo $$ > $LOCK_FILE

# Add your processing here.
#
#

# Clear down lock file ready for the next run.
rm -f $LOCK_FILE

When the script starts, the first thing it does is check for the presence of a lock file. If the file is present the script exits as a process must still be running. If no lock file is present, the script creates one, to lock out subsequent runs, performs the necessary tasks, then removes the lock file.

For more information see:

Hope this helps. Regards Tim...

Back to the Top.