How to Automate Tasks with Cron on CentOS 8
Cron is a time-based job scheduler in Unix-like operating systems, including CentOS 8. It runs as a background service and automatically executes predefined commands or scripts at scheduled intervals. This makes Cron an essential tool for automating routine server administration tasks such as backups, log rotation, system updates, and performance monitoring. In this tutorial, we'll show you how to create and manage Cron jobs on CentOS 8 to streamline your server management and ensure regular task execution without manual intervention.
Installing cron on CentOS 8
Cron is present on CentOS 8 by default. If for some reason it is not there, then you can install it with the command:
Now let's run it and add it to startup:
Basic cron settings
To fine-tune the time and frequency of tasks execution, there is the crontab command. We'll talk about it later. In addition, the following folders exist for easier adding tasks:
- /etc/cron.hourly
- /etc/cron.daily
- /etc/cron.weekly
- /etc/cron.monthly
The scripts located in them are executed with the specified frequency. To work successfully, scripts must have execution rights and must not contain dots in their names.
By default, any user can create tasks. To enable creation of tasks for certain users, add them to the /etc/cron.allow file. For all others, access will be denied.
To restrict this ability to a specific user, just add his login to the /etc/cron.deny file.
Cron writes logs to the following file: /var/log/cron.
Fine-tuning cron tasks
To manage tasks with the ability to specify the exact time and frequency of execution, use the command:
The scheduler configuration will open in your default text editor (usually vi). To configure another default text editor use the command (insert your favorite text editor instead of "nano"):
To display the current cron settings, run:
To view another user's cron settings, enter:
For editing:
Using the crontab command provides a syntax check on save to avoid errors. This is why it is a better idea to use this command to configure cron. For information, the scheduler file can be edited directly. It is located at /var/spool/cron/.
It is also worth noting that the assigned tasks will be performed on behalf of the user who added them to his cron.
The cron files use the following syntax:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * command to be executed
The * sign means all valid values. Cron job example:
The script /opt/script.sh will be run on the 15th of every month at 22:30.
Special characters and cron expressions
Comma (,). In the schedule, you can specify several values separated by commas to perform the same task at different times:
With such a record, the task will be launched on the 10th, 20th and 30th of each month.
Slash (/). Slash is used to indicate a step. For example, executing a task every 10 days will look like this:
Hyphen (-). To specify a range of values there is a hyphen. For example, daily execution from the 10th to the 20th of the month:
Special cron variables:
- @reboot - run at system boot
- @yearly or @annually - once a year
- @monthly - once a month
- @weekly - once a week
- @daily - once a day
- @hourly - every hour
- @midnight - at midnight
Example:
Conclusion
In this tutorial, we explored how to install, configure, and use Cron on CentOS 8 to automate routine server tasks. By leveraging Cron’s powerful scheduling capabilities, you can efficiently manage backups, system updates, log rotations, and other maintenance activities without manual intervention. Understanding how to create and edit crontab files, use special scheduling characters, and manage user permissions allows you to tailor automation precisely to your needs. Automating tasks with Cron not only saves time but also helps ensure consistent and reliable server operations.
FAQ
- Q1: Is Cron installed by default on CentOS 8?
Yes, Cron is included by default. If it’s missing, you can install it using dnf install crontabs. - Q2: How do I enable and start the Cron service?
Use the command systemctl enable --now crond.service to start Cron immediately and enable it at boot. - Q3: Where do I place scripts for automatic execution?
You can place executable scripts in /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly, or /etc/cron.monthly depending on the desired frequency. - Q4: How do I edit scheduled tasks for a user?
Run crontab -e to edit your own tasks, or crontab -u username -e to edit tasks for another user (requires root privileges). - Q5: What syntax should I use for Cron schedule expressions?
Cron uses five fields representing minute, hour, day of month, month, and day of week. Special characters like *, ,, -, and / help specify schedules. For example: 30 22 15 * * runs a task on the 15th of every month at 22:30. - Q6: Can I restrict which users can create Cron jobs?
Yes. Add allowed users to /etc/cron.allow or deny users via /etc/cron.deny to control access. - Q7: Where can I find logs of Cron job executions?
Cron logs its activity in /var/log/cron.