×


Using Cron to Automate Tasks on CentOS 8

The Cron daemon is a built-in Linux utility that runs processes on your system at a scheduled time.
Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to set up cron jobs on their CentOS 8 server.
In this context, we shall look into the process to configure cron jobs on Linux.

Steps to automate tasks by using Cron job on CentOS 8?

Cron reads the crontab (cron tables) for predefined commands and scripts. By using a specific syntax, you can configure a cron job to schedule scripts or other commands to run automatically.
When a cron runs in the background or when tasks are scheduled with cron, they are known as Cron jobs
They are can execute automatically, making them useful for automating maintenance-related tasks.
Below we will look into the steps to implement this setup on CentOS8.

How to Install Cron on CentOS?

To begin, ensure that the server has non-root user with administrative privileges.
So install cron using dnf.
To do this, execute the following commands;

$ sudo dnf update
$ sudo dnf install crontabs

To start the cron daemon, run the following command:

$ sudo systemctl start crond.service

We can set cron to run whenever the server starts up by using the following command:

$ sudo systemctl enable crond.service

How to use Cron and How to Schedule Cron Jobs?

Cron jobs are saved and managed in a special file known as crontab.
Every user can have their own crontab with which they can schedule jobs. This will be is stored under /var/spool/cron/.
We can use the crontab to schedule a job by editing and adding the task written in the form of a cron expression.
A cron expression usually consists of two elements: one is the schedule and the other one is the command to run.

The schedule component of the syntax is broken down into 5 different fields, which are written in the following order:
Field Allowed Values
minute 0-59
hour 0-23
Day of the month 1-31
month 1-12 or JAN-DEC
Day of the week 0-6 or SUN-SAT


Tasks scheduled in a crontab will have the following structure:

minute hour day_of_month month day_of_week command_to_run

In the following expression, we can see a cron job set to run  the command curl http://www.bing.com every Tuesday at 6:30 PM

30 18 * * 2 curl http://www.bing.com

There are also a few special characters you can include in the schedule component of a cron expression to make scheduling easier:
1. An asterisk (*) is a wildcard variable that represents “all.” Thus, a task scheduled with * * * * * … will run every minute of every hour of every day of every month.
2. Commas (,) break up scheduling values to form a list. If we want to have a task run at the beginning and middle of every hour we can use the expression 0,30 * * * * …
3. A hyphen(-) represents a range of values in the schedule field. Instead of having 30 separate schedule tasks for a command to run for the first 30 minutes of every hour we can just schedule it as 0-29 * * * * …
4. We can use a forward slash (/) with an asterisk to express a step value. Instead of writing out eight separate cron jobs to run a command every three hours we can schedule it as 0 */3 * * * ….

[Need urgent assistance to add cron jobs on your Server? We are happy to help you!]


How to manage Crontabs ?

To edit crontab we can use the following command:

$ crontab -e

The crontab opens up in vi editor and we can add our cron jobs each line by line.
After making the changes save and close the editor.
To view the contents of crontab, without editing it can be done with the following command:

$ crontab -l

We can erase your crontab with the following command:

The following command must be given carefully as it will not ask to us confirm whether we want to erase the crontab or not.

$ crontab -r

This command will delete the user's crontab immediately. However, we can include the -i flag to get a command prompt asking us for confirmation.

$ crontab -r -i

Output

crontab: really delete bob's crontab?

When the command prompt appears, we can either enter 'y' to delete the crontab or n to cancel the deletion.

[Need urgent assistance to automate tasks with cron?  We are happy to help you!]


Conclusion

This article will guide you on the steps to set up #Cron job on #CentOS 8 to help automate #tasks. Cron is a #clock daemon, whose name originates from #Chronos, the Greek word for time. It enables users to automate the execution of #commands, scripts (a group of commands) or programs at specified time intervals.
The simplest way to validate that cron tried to run the #job is to simply check the appropriate log file; the log files however can be different from system to system. In order to determine which log file contains the cron #logs we can simply check the occurrence of the word cron in the log files within /var/log .