Every now and then I need to create jobs that run on intervalls or at special times. In *nix there is a handy command called crontab that takes care of this. However the crontab command is not always self-explanatory so I made this quick intro to the command so I don’t have to keep it in my head
Command line options:
crontab -l # Show my current cron jobs crontab -e # Edit my cron jobs. Opens your default editor (e.g. VIM) crontab -r # Removes all cron jobs
Crontab syntax (user):
#Example #min hour day month weekday command 5 * * * * echo Hi #Runs 5 min past every hour */5 * * * * echo Hi #Runs in 5 min intervall * 8,20 * * * echo Hi #Runs at 08:00 and 20:00 * 12 */2 * * echo Hi #Runs every other day at 12:00 30 23 * * 0 echo Hi #Runs every Sunday at 23:30
Crontab syntax (root):
#Example #min hour day month weekday user command 0 * * 1 * root echo Hi #Runs every hour in January */10 * * * * niklas echo Hi #Runs in 10 min intervall
Only differens between root crontab and a user crontab is that root needs to tell cron who will be running the job
Syntax details:
min = minutes (0-59)
hour = hours (0-23)
day = day of month (1-31)
month = month (1-12)
weekday = day of week (0-6) (0 is Sunday)
user = the user that runs the job
command = command to run. Same syntax as if you would have run it on the command line. More then one command can be run, just separate the commands with a semicolon (;)
Run a cron job in “silent mode”:
Cron likes to mail that everything has gone well after running the command. If you do not want these mails you can put a cryptic pice of code at the end of your command like this:
#min hour day month weekday command 0 0 * * * rm -rf /home/niklas/tmp/* >/dev/null 2>&1
This will delete everything in my /home/niklas/tmp directory every night at 00:00. No mail will be sent
Tested on Red Hat Enterprise Linux AS release 3 (Taroon Update 9)