How to set up Crontab on CentOS 7 Linux

Published: March 21, 2018 (Updated: Mar 21, 2018)

Enjoying this content? Subscribe to the Channel!

The Ultimate Guide to Setting Up Cron Jobs on CentOS 7 (Works for Most Linux Distros!)


Welcome back to Darren’s Tech Tutorials!

If you’re running a Linux server, you need a way to automate routine tasks—like backups, maintenance scripts, or scheduled reporting. That’s where Cron comes in. Cron is the fundamental task scheduler built into nearly every Linux distribution, and mastering it is essential for any system administrator or power user.

In this comprehensive guide, we’re taking the core steps from our latest video and translating them into an easy-to-follow, actionable blog post. While our focus is specifically on setting up Cron jobs on CentOS 7, rest assured that these instructions and commands are universally applicable across most Linux environments, including Ubuntu, Fedora, and Debian.

Let’s dive in and learn how to schedule your first automated task!

## What is Cron and Why Should You Use It?

Simply put, Cron is the utility that lets users schedule commands or scripts to run automatically at a specified date and time. These scheduled commands are often called “cron jobs.”

Think of Cron as your personal, tireless assistant who wakes up exactly when you tell them to and runs the tedious, repetitive tasks you assign.

We manage these scheduled jobs using the crontab utility—the table of configuration instructions Cron reads.

## Prerequisites: Getting Our Test Script Ready

Before we jump into scheduling, we need a simple script that we can use to verify that Cron is actually running tasks correctly. This script should generate a recognizable output (like writing a message to a log file).

For your convenience, the cron test script and all examples used in the tutorial are available here: Cron Test Script and Examples

For demonstration purposes, let’s assume we create a simple shell script named test_cron.sh that logs the current time and date to a file in the /tmp directory.

Step 1: Create the Script File

Navigate to a suitable directory (we’ll use a home folder location for this example) and create your script:

mkdir ~/cron_scripts
cd ~/cron_scripts
nano test_cron.sh

Step 2: Add Script Content

Paste the following lines into the test_cron.sh file:

#!/bin/bash
echo "Cron job successfully ran at $(date)" >> /tmp/cron_output.log

Save and exit the editor (Ctrl+X, then Y, then Enter in Nano).

Step 3: Make the Script Executable

Cron requires the script to have executable permissions to run it:

chmod +x test_cron.sh

## How to Enter the Crontab Editor

Now that our test script is ready, the next step is to open the Cron configuration file associated with your user account. We do this using the crontab -e command.

Step 4: Open the Crontab

Execute the following command in your terminal:

crontab -e

This command opens a text editor (usually Vi or Nano) showing the contents of your personal crontab file. If this is your first time, the file will likely be empty except for some commented-out instructions explaining the syntax.

## Step 5: Scheduling Your First Cron Job

The critical part of setting up a cron job is understanding the syntax. Every cron entry requires five timing fields followed by the command to execute.

Understanding Cron Syntax

The fields are arranged in the following order:

Field Description Range
1 Minute 0-59
2 Hour 0-23
3 Day of Month 1-31
4 Month 1-12
5 Day of Week 0-7 (0 and 7 are Sunday)
Command The full path to the script or command to run

We want to test our setup immediately, so we are going to schedule the script to run every single minute of every day.

The “Every Minute” Cron Entry

Add the following line to the end of your crontab file (making sure to use the full path to your script):

* * * * * /home/your_username/cron_scripts/test_cron.sh

Note: Replace /home/your_username/ with the actual path where you saved your script. Always use the absolute path in cron jobs to prevent errors.

Step 6: Save and Verify

Once you save and exit the crontab file, you should see a confirmation message:

crontab: installing new crontab

This means your new schedule has been registered!

## Verification and Advanced Tools

Your cron job is now running and should execute every 60 seconds.

Check the Output

Wait about 60 to 90 seconds, and then check the log file we defined in our script:

cat /tmp/cron_output.log

If everything worked, you will see output similar to this, with new lines being added every minute:

Cron job successfully ran at Thu Dec 14 09:30:00 UTC 2023
Cron job successfully ran at Thu Dec 14 09:31:00 UTC 2023

To see a list of all jobs currently scheduled for your user, run:

crontab -l

Advanced Scheduling Tool: crontab.guru

While setting up simple jobs is easy, Cron syntax can get tricky when dealing with complex schedules (like “run every third Tuesday at 3:15 AM”).

A resource that is invaluable for testing and validating complex Cron entries is crontab.guru.

Visit crontab.guru

Simply type in your five fields, and the site instantly translates the syntax into plain English, ensuring you never accidentally schedule a disruptive task at the wrong time!

## Conclusion: You’ve Mastered Linux Scheduling!

Congratulations! You have successfully set up, scheduled, and verified your first automated task using Cron on CentOS 7. You now have the power to automate critical maintenance, security, and backup tasks, freeing up your valuable time.

This foundation is crucial for managing robust, efficient Linux systems. If you found this guide helpful or if you have any advanced Cron tips, please drop a comment below!

And if you enjoyed learning how to automate your server, make sure to Like this post and Subscribe to Darren’s Tech Tutorials on YouTube for more clear, accessible technology guides! Happy scheduling!