How to use crontab to run timed events
Cron is a Linux utility that runs tasks in the background automatically at time intervals. The cron daemon, or service, initiates this task. That task is called a Cron job. You can find a list of these jobs in the Cron Table, also known as the Crontab. In this article you will learn about the command, how to use the command and command's syntax.
NOTE: If your name is on /usr/lib/cron/cron.allow, you will be able to create jobs in the crontab. If there is no file, check if there's /usr/lib/cron/cron.deny and see if your name is on there. If not you'll be free to create jobs. If neither file exists then only root can execute crontab.
crontab -e — Creates or edits a crontab file
crontab -l — Displays your crontab file
crontab -r — Removes your crontab file
A crontab file contains five fields specified by these parameters:
- Minutes: 0-59
- Hours: 0-23
- Day of the month: 1-31
- Month: 1-12
- Day of the week: 0-6 (0 = Sunday, 1 = Monday, etc)
These fields are aligned from left to right as so:
* <- min * <- hour * <- day of * <- month * <- day of the week
EXAMPLE: 59 23 31 12 6 (command to be executed at specified interval)
* can be set to whatever value as long as it's within the stated parameters. Here's an example of an crontab:
45 20 1 * * rm /var/logs/targetsite/*
This means that on the 1st of every month at 8:30PM logs from the above directory will be removed.
Now when a cronjob is ran, it will send an email to the user account executing the job. If you wish to disable that feature please append this command at the end of the cron job line: