Cron

By flw/Dan with resources from Paul Vixie man page 1993 and linuxforums.org team

What is cron?
It is the scheduling daemon of the Linux operating system

It maintains crontab files for users with the daemon known as crond. Cron is be started from /etc/rc or /etc/rc.local or /etc/rc.d. It will return immediately, so you don’t need to start it with ’&’.

Cron searches /var/spool/cron for crontab files which are named after accounts in /etc/passwd; crontabs found are loaded into memory. Cron also searches for /etc/crontab and the files in the /etc/cron.d/ directory, which are in a different format then wakes up every minute, examining all stored crontabs, checking each command to see if it should be run in the current minute. When executing commands, any output is mailed to the owner of the crontab (or to the user named in the MAILTO environment variable in the crontab, if such exists).

Additionally, cron checks each minute to see if its spool directory’s modtime (or the modtime on crontab) has changed, and if it has, cron will then examine the modtime on all crontabs and reload those which have changed. Thus cron need not be restarted whenever a crontab file is modified. Note that the crontab command updates the modtime of the spool directory whenever it changes a crontab.

If the allow file exists, then you must be listed there in order to be allowed to use this command. If the allow file does not exist but the deny file does exist, then you must not be listed in the deny file in order to use this command. If neither of these files exists, then depending on site-dependent configuration parameters, only the super
user will be allowed to use this command, or all users will be able to use this command.

Why would I want to use it? Cron is useful even for home users in running regular tasks with out your manual intervention. A simple example would be backing up your directories with your configuration file and or data file like /etc and /home to another physical drive or computer or logical drive.

Directories, Files and Locations used in cron
/etc/crontab file lists the times to run various cron directories with any files.
/etc/cron.hourly directory listing of hourly files to be run
/etc/cron.daily directory listing of daily files to be run
/etc/cron.weekly directory listing of weekly files to be run
/etc/cron.monthly directory listing of monthly files to be run
/etc/cron.allow file list of users allowed to use crontab (not always present)
/etc/cron.deny file list of users denied to use crontab (not always present)

The locations above are where you put your file to be run. Like a daily script to be run goes into the /etc/cron.daily directory. And a monthly file to the monthly directory (/etc/cron.monthly).

What do I need to know how to do before I can use it?Well you need to know how to use any text editor like VI, EMACS or one of the many others. You'll also have to know some commands you will want to run like tar, cp, mv and how to chain the commands together. When just using linux commands you'll start the first line of all files to be run with #!/bin/sh (tells linux to use the command shell) or for perl scripts #!/usr/bin/perl (tells linux to get ready to run some perl commands) . This may vary depending on the distribution or setup of linux your running.

How do I use it? You can use your favorite text editor to edit /etc/crontab is where you would change any times you want your files to run at. Locating the files to be run in cron/hourly or daily or weekly or monthly directories for the frequency you want your file(s) run on.

Typical crontab file contents:

/etc/crontab:
SHELL=/Bin/Bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/root

# run-parts

01 * * * * root run-parts /etc/cron/hourly
02 4 * * * root run-parts /etc/cron/daily
22 4 * * 0 root run-parts /etc/cron/weekly
42 4 1 * * root run-parts /etc/cron/monthly

The time schedule is in the following format:
minute, hour, date, month, day of the week 0-7 (0-5 are monday through friday with 6 and 7 being Sat/Sun)

*= is a wild card like in a poker game. It can represent any number.

Using the example of times above lets see when each should run.

01 * * * * hourly should run 1 minute after each and every hour (00:01) 1 minute after midnight

02 4 * * * daily should run 2 minutes after the fourth hour of the begining of the each day (04:02) thats A.M. :>

22 4 * * 0 weekly should run 22 minutes after the fourth hour on every monday. (04:22 on every Monday)

42 4 1 * * monthly should run 42 minutes after the fourth hour only on the 1st of the month. (04:42 only on the 1st of the month)


Typical file starts by telling linux to run in shell by:

#!/bin/sh (for command line scripts only)
or
#!/usr/bin/perl (for perl scripts only)

Then the command line instructions or a call to another file as:
Rotate the log file based off another file called logroate with "/usr/sbin/logrotate"

or

copy a file from one location to another while renaming it "cp /home/httpd/apache/index.html /bk/web/indexhtml.old"

or automated reboot like “shutdown -r now”

or backup data from etc and home directory with "tar cfz /tar/filename.tgz /etc /home

or chain commands like "tar cfz /tar/filename.tgz /etc /home | mv -f /tar/filename.tgz /public/directory

Diagnostics
A informational message appears if you run it with a bad
command line.