Hits: 4,701
Home >> Software >> Cron

CRONTAB

Edit/Display CRON

The standard way to edit CRONTAB (aka CRON) is via crontab -e. To edit a different user then who you are logged in use crontab -u user -e. Then to display the current CRON, do a simple crontab -l. You can change the editor used to edit CRON via EDITOR= environment variable. I use Vim/Vi so in my ~/.bashrc I have EDITOR=/usr/bin/vim.

CRON Entry

A typical CRONTAB entry looks like:

0 4 * * * /home/user/myscript.sh > /dev/null

This example runs the script

/home/user/myscript.sh
and sends the stdout to /dev/null while errors (stderr) are emailed back to the user. The script is run at 04:00 every day.

Usage Fields

The following fields are used to tell CRON when to run.

FieldAllowed Values
Minute0-59
Hour0-23
Day of Month1-31
Month1-12
Day of Week0-7 or (0 and 7 are both Sunday)

Sample CRON Jobs

Some easy and quick things CRON can be used for take a look at these.

* */1 * * * /usr/bin/find /tmp -atime +7 -exec rm -f {} \; > /dev/null

Delete all files from the /tmp(temp) directory that haven't been accessed in the last 7 days every HOUR.

0 0 * * * /usr/bin/find / -mtime -1 > /tmp/modified_files

Create a list of all files modified in the last 24 hours to /tmp/modified_files every day at 12:00 AM (00:00).

For more good tricks, learn to use find, it is your friend.

Advanced CRON

GNU CRON can use intervals. For example you could say

*/5 * * * *
to run a command every 5 minutes. However, with most things in UNIX this isn't support on other systems. You would have to write out the interval such as
0,5,10,15...
Aren't you glad GNU tools are out there!