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 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.
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...
Field | Allowed Value |
---|---|
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 |
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 valueOnce 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
As an e-commerce store owner, you understand the importance of having a reliable and efficient online presence. Let's look at why website uptime monitoring is so important for e-commerce. Read more today.
Read moreAs a business owner, ensuring customer satisfaction should be a top priority. One key aspect of customer satisfaction is having a reliable and efficient online presence, which is where website uptime monitoring comes in.
Read moreAs a website owner, uptime monitoring is crucial to ensure that your website is always available and functioning properly. In this article, we'll discuss how to maximize the efficiency of your website uptime monitoring strategy, including the use of tools, setting up alerts, and optimizing your website's performance.
Read moreLooking to monitor your website and domains? Join our platform and start today.