Cron

From TWUUG

Experience has taught me that the cron files and locations vary among
distributions, versions and networking hardware,
so the locations listed are from Slackware Linux 12.0,
use one of the Searching Linux commands to find the individual files.

Contents

Meaning of the Name

The name cron is derived from the greek word chronos which means time.

Purpose

Cron is a time-based scheduling service for Unix-like operating systems. It can be used to schedule functions from system backups to gags on users.

Related Files

  • Crontab - Each user on the system has a crontab file, this file contains the actual scheduling of scripts and/or commands to be run. In Slackware 12.0 These files are contained in the /var/spool/crontabs/ directory.
  • /etc/cron.daily directory - Any executable scripts you place in this directory will be run by root once a day at the time listed in /var/spool/crontabs/root.
  • /etc/cron.hourly directory - Any executable scripts you place in this directory will be run by root once an hour at the time listed in /var/spool/crontabs/root.
  • /etc/cron.monthly directory - Any executable scripts you place in this directory will be run by root once a month on the date and time listed in /var/spool/crontabs/root.
  • /etc/cron.weekly directory - Any executable scripts you place in this directory will be run by root once a week on the date and time listed in /var/spool/crontabs/root.
  • /usr/sbin/crond - The cron daemon which must be activated for any scheduled tasks to run. This daemon must be restarted to load any changes to the crontab files.
  • /usr/bin/crontab - this is the executable that is used to change the crontab files

Usage

To schedule a cron job you have three choices, drop an executable script in one of the /etc/cron.??? directories or make a manual entries in the applicable user's crontab file or use the crontab command to edit the user's crontab files.

The line structure in the crontab files is:

+---------------- minute (0 - 59)
|  +------------- hour (0 - 23)
|  |  +---------- day of month (1 - 31)
|  |  |  +------- month (1 - 12)
|  |  |  |  +---- day of week (0 - 6) (Sunday=0 or 7)
|  |  |  |  |
*  *  *  *  *  command to be executed

By leaving the * in any of the numeric fields you tell it to run on every occurrence.

The contrab program has the following options: crontab file <opts> replace crontab from file crontab - <opts> replace crontab from stdin crontab -u user specify user crontab -l [user] list crontab for user crontab -e [user] edit crontab for user crontab -d [user] delete crontab for user crontab -c dir specify crontab directory

Examples

As an example I will show you a gag. I will tell cron to play a filename called blind.mp3 using amp every hour on the hour as the user david, this makes it where the song is only audible if david is logged into the computer. As the root entered "crontab -u david" into the command line and typed the following into the end of the file.

 0 * * * * "amp /home/david/music/blind.mp3"

now you will need to save and exit the file, since it is using vi the use the following keys in the exact order, :, enter, wq. Next you need to restart crond to use the updated crontab file.

root@linux:/#crond restart

And now a good musical gag has been enabled.

Personal tools