cron
Linux Commands - Administrative Commands

cron

cron is designed to help the administrator run commands on a regular
basis unattended. In order to protect cron, two files are used to limit access
to the program.

/etc/cron.allow
/etc/cron.deny

If both of these files are empty, all users will have the ability to run cron files.

Submitting a cron job:

/var/spool/cron/username - individual cron jobs
/etc/crontab - root user for the system cron jobs
/etc/cron.d - root user for the system cron jobs
/etc/cron.hourly - each has a shell script that runs
/etc/cron.daily
/etc/cron.weekly
/etc/cron.monthly

 

 


Here is an example of /etc/cron.daily
If you wanted to add a program into this file, edit it as root and on one line include the command you need to run.
" Press ? for keyboard shortcuts
" Sorted by name (.bak,~,.swp,.o,.info,.aux,.log,.dvi,.bbl,.b"= /etc/cron.daily/
../
.placeholder
0anacron
apt
bsdmainutils
find.notslocate
logrotate
man-db
netkit-inetd
samba
slocate
standard
sysklogd


Here is an example of /etc/cron.weekly

" Press ? for keyboard shortcuts
" Sorted by name (.bak,~,.swp,.o,.info,.aux,.log,.dvi,.bbl,.b"= /etc/cron.weekly/
../
.placeholder
0anacron
man-db
popularity-contest
sysklogd

Here is an example of /etc/cron.monthly

" Press ? for keyboard shortcuts
" Sorted by name (.bak,~,.swp,.o,.info,.aux,.log,.dvi,.bbl,.b"= /etc/cron.monthly/
../
.placeholder
0anacron
scrollkeeper
standard

 

cron Format

There are seven fields that must be used.

field 1 - minute 0-59, a - between numbers means a range 1-30
a comma between numbers means individual 1,5,8
2 hour 0-23
3 day of month 0-31
4 month 0-12
5 day of week 0-7 (both and 7 are Sunday)
6 user
7 command

Example:

7 0 * * 1 /home/mike/bk.sh

This will run the backup script in /home/mike at 12:07 Mondays. The asterisk
in a field indicates that all possible values are run for that field.


Create a cron Job

Three commands are important for creating cron jobs.

crontab -l list all the current cron jobs for user
crontab -r remove cron jobs
crontab -e edit cron jobs

When crontab -e is run the vi editor opens and the user is allowed to edit
the file. It is imperative that the user be familiar with vi so that the file can be edited and saved.

Example:

crontab -e (this opens crontab file)
click i (edit mode in vi)
10 * * * * /home/mike/bk (runs backup script at 10 after the hour)
esc (exits edit mode in vi, now in command mode)
shift: w! (writes the file that you have created)
shift: q (quits vi editor)