Hi, this is Sandra Henry-Stocker, author of the “Unix as a Second Language” blog on NetworkWorld.
Today’s 2-minute tip covers crontab – the tool used to schedule tasks that will run automatically on the day and time that you specify. The only difficult part of using crontab is setting the dates and times that you want your process to run.
If you list crontab jobs using the crontab -l command, you may see something like this:
shs@stinkbug:~/$ crontab -l
# m h dom mon dow command
15 2 1 * * /home/shs/backup
0 6 * * 1 /home/shs/weekly_todo
In this example, we see two cron jobs. The fields on the left represent minutes, hours, day-of-the-month, month-of-the-year, and day-of-the-week. * or the word “all” means “every” minute, hour, day or month or “any” day of the week.
• The first entry says that at 15 minutes after 2 AM on the 1st of every month (regardless of the day of the week), this backup job will run.
• The second says that at 6 AM every Monday the weekly_todo job will run.
Think of the fields as going from small to large except for that day-of-the-week field. Every task scheduled requires the timing fields and the task to be run.
To schedule another task, you have to edit your crontab file with the crontab -e command.
shs@stinkbug:~/$ crontab -e
1 2 3 4 8 echo hello > /tmp/hello
Notice I made a mistake on the day-of-week field.
No problem. Crontab will warn you and offer you a chance to rethink your entry if try to add a cron entry that doesn’t have the right number of fields or has field values that don’t make sense – like 25 for the hour or 8 for the day of the week.
That’s your 2-minute Linux tip for today. If you liked this video, please hit the like and share buttons. For more Linux tips, be sure to follow us on Facebook, YouTube and NetworkWorld.com.