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...
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
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
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.
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...
|Day of the month|
|Day of the week|
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
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
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
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.
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:
I'm pleased to announce that we've just launched our very first PRO subscription. Learn more about it in this short read article.Read more
If you're one of the 90% of people that are worried about getting your password hacked then read this article as we deep dive into why password security is super important right now.Read more
Do you need to Ping your website or server? Get started with one of our brand new tools that will let you Ping your website.Read more