Wednesday, March 25, 2009

crontab - scheduled tasks in Linux

crontab is a file which contains the schedule of cron entries to be run and at what times they are to be run.
A cron schedule is a simple ASCII text file. Each user has their own cron schedule. You can edit it only with the crontab command. The default text editor is usually vi

usage:
crontab -e add or edit a scheduled task
crontab -l list the the scheduled tasks
crontab -r remove all scheduled tasks

syntax:
you have 6 positions, 5 for the time, 1 for the command to execute

* * * * * command_to_execute
| | | | |
| | | | |_Weekday 0-6 (0 = Sunday)
| | | |___Month 1-12
| | |_____Day 1-31
| |_______Hour 0-23 (0 = midnight)
|_________Minute 0-59

some example:
30 8 * * * rm /home/{username}/temp/*
this entry remove all files in temp directory every day at 8:30 AM

5,35 * * * 1-5 rm /home/{username}/temp/*
this entry remove all files in temp directory from monday to friday every hour at minute 5 and 35

30 8 * * * rm /home/{username}/temp/* >/dev/null 2>&1
this is the same as the first example but with no email notification and no log file

30 8 * * * rm /home/{username}/temp/* > /home/{username}/logfiles/emptytemp.log >/dev/null 2>&1
this is the same as the first example but with no email notification and the log file redirected to a custom location (> to replace the old log file with the new one, >> to append new log file to old one)

No comments: