11.12.2024

How to Automate Regular Tasks with Cron on CentOS 8

Cron is a service that runs in the background and launches configured tasks on a schedule. This makes it very useful for server maintenance. In this tutorial, we'll take a look at how to automate common tasks using cron on CentOS 8.

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:

dnf install crontabs

Now let's run it and add it to startup:

systemctl enable --now crond.service

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:

crontab -e

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"):

export EDITOR=nano

To display the current cron settings, run:

crontab -l

To view another user's cron settings, enter:

crontab -u user -l

For editing:

crontab -u user -e

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:

# Example of job definition:
# .---------------- 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:

30 22 15 * * /opt/script.sh

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:

30 22 10,20,30 * *

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:

30 22 */10 * *

Hyphen (-). To specify a range of values there is a hyphen. For example, daily execution from the 10th to the 20th of the month:

30 22 10-20 * *

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:

@daily /opt/script.sh