Introduction
The name cron is derived from the Greek word for time, chrono. Crontab is a file that contains cron entries to be executed at a specified time. Cron is very useful for scheduling repetitive tasks, automating system maintenance, and administration.
Each system user has a separate individual crontab, and the commands scheduled within it must be executed in the context of the user's permissions. A regular user can only modify their own crontab, while root can modify any user's crontab.
Crontab does not perform error validation, such as syntax or concurrent editing. Errors are only detected during the execution of scheduled commands.
Installation environment
This article was based on Slackware Linux 15.0 and the dcron-4.5 package. Most of the information presented here is applicable to any distribution, but small differences may exist.
Practical examples
To edit the crontab file, use the command crontab -e. Blank lines are ignored, and lines starting with the character # are considered comments.
crontab
# runs the 'date' command every minute * * * * * date # runs the command every 5 minutes */5 * * * * date # runs every hour at the 25th minute of each hour 25 * * * * date # runs every day at 6:10 10 6 * * * date # runs every two hours at the beginning of the hour 0 */2 * * * date # runs every minute between 12 and 13 hours * 12 * * * date # Every two hours between 23:00 and 07:00, and again at 08:00 0 23-7/2,8 * * * date # runs at 4:00 am on January 1st 0 4 1 1 * date # every day at 11 am and logs the output of the command to the date-output file 0 11 * * * date >> /var/log/date-output 2>&1
- * ➟ every (minute, hour, day, month)
- 0-5 ➟ sets a range, 0, 1, 2, 3, 4, 5
- 2,4,5 ➟ list, 2, 4, 5
- */2 ➟ every interval of 2 (minutes, hours, days, months)
Crontab commands
- crontab -e ➟ opens the crontab for editing
- crontab -l ➟ displays the content of the crontab file
- crontab -d ➟ deletes the crontab file (or crontab -r depending on the distro)
- crontab -e -u <user> ➟ edits the crontab of a specific user, must be run as root. -u can be combined with other parameters, -l to list, -d (or -r) to remove.
Choosing the editor
Crontab uses the editor specified in the environment variables VISUAL or EDITOR. If none is set, the vi editor will be used. To change the editor, use:
$ Shell
# Change the editor to emacs export EDITOR=emacs crontab -e # To use the elvis editor export EDITOR=elvis crontab -e # To use the vi editor export EDITOR=vi crontab -e
The above change will be valid for the duration of the session. After restarting, the variable will lose its defined value. To make it permanent, place the export command in the ~/.bashrc file.
Organizing by folders
The root crontab is pre-configured to run scripts placed in specific directories periodically, according to the folder name:
- /etc/cron.hourly/ - Every hour.
- /etc/cron.daily/ - Once a day.
- /etc/cron.weekly/ - Once a week.
- /etc/cron.monthly/ - Once a month.
This is useful for scripts that do not require specific scheduling but need to run regularly. The configuration that enables this functionality can be checked inside the root user's crontab:
Crontab
# Run hourly cron jobs at 47 minutes after the hour: 47 * * * * /usr/bin/run-parts /etc/cron.hourly 1> /dev/null # Run daily cron jobs at 4:40 every day: 40 4 * * * /usr/bin/run-parts /etc/cron.daily 1> /dev/null # Run weekly cron jobs at 4:30 on the first day of the week: 30 4 * * 0 /usr/bin/run-parts /etc/cron.weekly 1> /dev/null # Run monthly cron jobs at 4:20 on the first day of the month: 20 4 1 * * /usr/bin/run-parts /etc/cron.monthly 1> /dev/null
You can change the execution times and days. For example, running the scripts 5 minutes past the hour instead of 47 minutes as originally configured. Remember that this is a folder-based scheduling configuration; for more precise scheduling, you should create an individual configuration for each command execution.
Remember, when placing a script in this folder, ensure that it has execution permissions configured so that it can be executed.
Log
With the root user, you can check the cron log at: /var/log/cron.
# Shell
tail -f /var/log/cron
With a regular user, it is possible to redirect the error (stderr) to a log file in the user's home directory, using the redirection operator 2>>.
Crontab
* * * * * ls /home/user/does-not-exist 2>> /home/user/crontab.log
Conclusion
Scheduling tasks in Linux using crontab is a powerful tool that allows automation of processes, saving time, and reducing the margin for error in manual operations. With simple configuration, users can schedule commands and scripts to be executed at regular intervals, ensuring proper system maintenance and repetitive task execution. By understanding crontab formats and options, it is possible to customize schedules according to your needs, promoting greater efficiency and organization.
References
The main source for this article was the crontab manual (man page) available on Slackware Linux. Artificial intelligence were employed for grammatical and semantic error correction, as well as optimization of the text structure and vocabulary.