Bobcares

Cron job on CentOS 8 for Automating tasks – Set up easily

by | Jan 11, 2021

Need to set up Cron job on CentOS 8?

At Bobcares, we often get requests from our customers to set up cron jobs on their CentOS 8 server as a part of our Server Management Services.

Today let’s see how our Expert Support Engineers get this done for our customers with ease.

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

Cron is a time-based job scheduling daemon usually found on operating systems such as CentOS and other Linux distributions.

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.

Let’s see the steps which our Support Techs follow for this setup on CentOS8:

Cron job on CentOS 8

We must have a non-root user with administrative privileges.

Installing Cron

We can install cron using dnf.

The steps to follow are given below:

$ 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 Cron Works 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.google.com every Tuesday at 6:30 PM

30 18 * * 2 curl http://www.google.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 assistance to add cron jobs? We are happy to help you!]

Managing 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 assistance to automate tasks with cron? We are happy to help you!]

Conclusion

To conclude we saw how to set up Cron job on CentOS 8. Also, we saw the method our Support Techs follow to set up cron jobs to automate tasks.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

var google_conversion_label = "owonCMyG5nEQ0aD71QM";

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.