Setup Scheduled Tasks using CRON on CentOS / RHEL

In this guide, I’m going to talk about scheduling tasks using CRON.

These scheduled commands or tasks are known as “Cron Jobs”. Cron is generally used for running scheduled backups, monitoring disk space, deleting files (for example log files) periodically which are no longer required, running system maintenance tasks and a lot more.

– OSTechNix

Table of Contents

  1. Crontab Syntax
  2. Check Crontab Status
  3. Crontab Add, Edit & List
  4. Test Crontab
  5. Check Errors of Cron
  6. Example of Some Scheduled Jobs

Step 1 : Crontab Syntax

Let’s see the syntax of crontab:

* * * * * /path/backup.sh

The meaning of five * means:

* * * * * Command
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)

Step 2 : Check Crontab Status

To check if CRON is running or not, we can run this command:

ps ax | grep cron 

If CRON is running, you will see the response like this:

  563 ?        Ss     0:02 /usr/sbin/crond -n
26001 pts/0    R+     0:00 grep --color=auto cron

If your server doesn’t have cron, you can install by typing this command:

# CentOS/RHEL 7
sudo yum install cronie

# CentOS/RHEL 6
sudo yum install crontabs

Step 3 : Crontab Add, Edit & List

To add or edit any job in crontab, use the below command to open the crontab editor. You’ll able to see all cronjobs here:

sudo crontab -e

If you want to add/edit job for a specific user, then you need to run this:

sudo crontab -u username -e

To view the crontab list, simply run this:

crontab -l

Like add/edit for a specific user, we can see the specific user’s job too.

crontab -u username -l

Step 4 : Test Crontab

To test if crontab is running, we can follow this:

# Open cron editor
sudo crontab -e

# Then add this test job
* * * * * /bin/echo "foobar" >> /path/cron-running.txt

After a minute, under the entered path you’ll find a newly created file called “cron-running.txt“. After a minute, if you can see the newly created file, then CRON is working.

Now we are going to check if a command works through crontab or not. Open the CRON editor using sudo crontab -e and then add this job:

* * * * * /bin/foobar > /path/command-working.txt 2>&1

Save the file and again check the entered path. If you can see the file “command-working.txt“, then command works.

Step 5 : Check Errors of Cron

We can easily see the cron errors using these below commands:

# Check cron log
cat /var/log/cron

# Check errors
cat /var/log/messages
# or,
cat /var/log/cron.log

Step 6 : Example of Some Scheduled Jobs

Let’s see some examples of scheduled tasks.

1. Schedule a task to execute every minute:

* * * * *  /path/script.sh

2. Schedule a task to execute every 5 minutes:

*/5 * * * *  /path/script.sh

3. Schedule a task to execute on every Sunday at 6 PM:

0 18 * * sun /path/script.sh

4. Schedule a task to execute on every 10 minutes and send Standard Error and Standard Out to an email:

[email protected]
*/10 * * * * /path/script.sh > /dev/null 2>&1

5. Schedule a task to execute on every Sunday and Monday:

0 18 * * sun,mon /path/script.sh

6. Schedule multiple tasks to execute:

* * * * /path/script1.sh && /path/script2.sh

# Or,
* * * * * /path/script.sh; /path/scrit2.sh

7. More examples:

# Execute on system reboot
@reboot /path/script.sh

#Execute hourly:
@hourly /path/script.sh

#Execute daily:
@daily /path/script.sh

#Execute weekly:
@weekly /path/script.sh

#Execute monthly:
@monthly /path/script.sh

#Execute yearly:
@yearly /path/script.sh

We’ve seen most of the CRON’s commands. If you face any issues, write in the comment box. Thank you.


Software Engineer | Ethical Hacker & Cybersecurity...

Md Obydullah is a software engineer and full stack developer specialist at Laravel, Django, Vue.js, Node.js, Android, Linux Server, and Ethichal Hacking.