
Linux Command Line
The Cron command in Linux is a powerful tool for scheduling tasks and automating processes. It allows users to schedule commands or scripts to run automatically at specified intervals, making it an essential tool for system administrators and developers.
When to use Cron Cron is useful for running repetitive tasks, such as backups, updates, and maintenance scripts. For example, you may want to schedule a daily backup of your website at a specific time, or run a script to update your system packages every week. Cron can also be used to schedule tasks that need to be run at specific intervals, such as sending out a daily email report.
How to use Cron Cron uses a configuration file called the crontab, which contains a list of commands and the schedule on which they should be executed. The crontab file is located in the /etc/cron.d directory and can be edited using the crontab command.
To schedule a new task, use the following syntax:
* * * * * command to be executed
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday = both 0 and 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)
For example, to schedule a script to be run every day at 3:30 am, you would enter the following command in the crontab file:
30 3 * * * /path/to/script.sh
Another example, to schedule a backup script to run every Sunday at 10 pm, the command would be:
0 22 * * 0 /path/to/backup_script.sh
You can also use the special characters *
, */5
, and -
for more flexibility in scheduling. The *
character means that the task should be executed at any value for that field, while */5
means that the task should be executed every 5 values for that field. For example, */5
in the minutes field would mean that the task should be executed every 5 minutes. The -
character can be used to specify a range of values, such as 5-15
in the minutes field, which would mean that the task should be executed every minute between 5 and 15 minutes.
It’s also worth noting that Cron reads the system’s time zone at the time of execution and schedule task accordingly.
Cron is a powerful tool that can help you automate repetitive tasks and schedule important processes on your Linux system. With a little bit of knowledge and some examples to guide you, you can start using Cron to make your life as a system administrator or developer much easier.
See our list of 75 Linux commands you should know about.