Cron is a time-based scheduling utility in Unix-like operating systems that allows you to schedule tasks to run automatically at predetermined intervals. It is often used to automate system maintenance or administration, but can be used to schedule any kind of script or program to run automatically.
Cron uses a configuration file called the crontab, which specifies the tasks to be run and the intervals at which they should be run. Each line in the crontab represents a single task, and consists of six fields:
- Minute (0-59)
- Hour (0-23)
- Day of the month (1-31)
- Month (1-12)
- Day of the week (0-6, with 0 being Sunday
- The command to be run
For example, to schedule a task to run at 10:30am every day, you would use the following entry in the crontab:
30 10 * * * /path/to/command
Each field can contain a single value, a list of values separated by commas, or a range of values separated by a hyphen. For example, to run a task at 10:30am every day and at 5:00pm on weekdays, you could use the following crontab entry:
30 10 * * * /path/to/command
0 17 * * 1-5 /path/to/command
Cron also supports special characters for more advanced scheduling. For example, the asterisk (*) can be used to match any value, and the forward slash (/) can be used to specify step values.
Here are a few more examples of crontab entries:
- Run a task every hour:
0 * * * * /path/to/command
- Run a task every day at midnight:
0 0 * * * /path/to/command
- Run a task every week at midnight on Sunday:
0 0 * * 0 /path/to/command
- Run a task every 10 minutes:
*/10 * * * * /path/to/command
To edit the crontab, you can use the crontab
command with the -e
flag to open the crontab file in a text editor. You can then add or edit the tasks as needed, and save the file to apply your changes.
Cron is a powerful and versatile tool that can help you automate a wide variety of tasks in your Unix-like operating system. Whether you are using it to schedule system maintenance tasks or to run custom scripts and programs, it can save you time and effort by running tasks automatically at predetermined intervals.