How To Setup a Cron Job on a CentOS 8 Server [Beginner's Tutorial]
# tutorial# how to

How To Setup a Cron Job on a CentOS 8 Server [Beginner's Tutorial]

Cron jobs are a type of scheduling system that uses time and is often found in systems such as Linux. Cron jobs run in the background and tasks run with cron.

In this tutorial post, I'll tell you how to set up a cron job on a CentOS 8 server, but the process should be identical on a regular Linux server.

Ready? Let's dive right in...

Installing Cron onto your CentOS 8 server

Almost every Linux distribution has some form of cron installed by default, but CentOS 8 doesn't.

So for a CentOS server, you're going to need to install cron using dnf.

To get started with the installation of cron jobs, run:

sudo dnf install crontabs

Running any kind of package installation on a server such as a CentOS server will ask you to confirm that you do indeed want to install the package.

So you'll be able to confirm this by pressing y and then ENTER

After this, you're going to need to ensure that you enable the cron service via systemctl so that whenever your Linux server reboots your crons can continue running.

You'll need to firstly start the cron daemon by running:

sudo systemctl start crond.service

After this, run the following so that whenever the server starts you'll be able to start scheduling tasks

sudo systemctl enable crond.service

Now, you can start scheduling cron jobs on the server.

Setting up cron jobs on your server

In order to set up a cron job on your server, you're firstly going to need to know a little about how cron jobs work.

A cron job is managed through a special file known as a crontab, each user on your server can have their own crontab set up and scheduling jobs.

In order to schedule a cron job, you're going to need to know some important syntax, I've put together a table below to make this easier for you...

FieldAllowed Value
minute0-59
hour0-23
Day of the month1-31
month1-12 or JAN-DEC
Day of the week0-6 or SUN-SAT

In order to set up a cron job, your crontab should be structured with the following:

minute hour day_of_month month day_of_week command_to_run

CentOS 8 Cron Job Example

I suspect if you're new to setting up cron jobs, the syntax, formatting and general structure probably seems quite complicated. So here's a practical example of what you could put into your crontab:

The below example will run a curl request (a simple HTTP request) to the Example domain's website every Tuesday at 5:30pm

30 17 * * 2 curl https://example.com

Special characters that you can include in your crontab

The special characters that you can include in a crontab include:

  • * - an asterisk is a wildcard variable that represents "all"
  • , - a comma break up scheduling values to form a list
  • - - a hyphen will represent a range of values in a schedule field
  • / - you can use a forward slash with an asterisk to express a step value

Managing cron jobs on a CentOS 8 server

Once you've set up a cron job, you're going to need to manage it from time to time, whether this be to modify when your cron job runs, or the command it runs.

Typically, on a Linux based server you cron simply run crontab -e, however, by default on a CentOS 8 server this would open up the crontab editor using vim.

crontab -e

Vim can be quite confusing to some people, so nano is a better choice, but you'll need to install this first.

If you'd like to view your crontab, but not edit it, you can run:

crontab -l

More posts

Introducing Notification Contacts

Notification Contacts make alerting team members incredibly easy and simple, find out more about this new feature that we've released for all of our customers!

Read more
Pretty Purchases Joins Our Website Monitoring Tool

Here at the Domain Monitor, we're delighted to announce that Pretty Purchases, a UK based E-Commerce website that specialises in selling handbags has joined our website monitoring tool, learn more and find out how you can get a 5% discount today!

Read more
The 5 Things You Need To Do When Your Website Goes Down

If you've ever received a notification from our website uptime monitoring tool, you'll know how scary it can be to find out your website is down, your stomach literally drops and you start to panic, is your website actually down? Here's what to do when your website goes down.

Read more

Get our FREE downloadable PDF revealing The Top 3 Benefits Of A Website Monitoring Tool!

Plus, more great content, product news and updates