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
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

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 Two-Factor Authentication [Plus Additional Security Features]

We're pleased to announce that you can now enabled Two-Factor Authentication (2FA) in your Domain Monitor account. Enable it today to take advantage of additional account security.

Read more
Setting Your Time Zone [New Feature]

Time Zones, they're a pain. Recently, here in the UK the clocks have gone forward, and for many of you, you’ve probably forgotten a few clocks or watches around the house right?

Read more
Introducing Slack Notifications [Third-party Integration]

I'm pleased to announce that we've just launched our Slack Integration as a new Notification Type, and you can enable it today!

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