Automate Systems Tasks with crontab on CentOS 7

centos crontab

We’ll show you, how to use CentOS crontab. How to automate system tasks on CentOS 7, using CentOS crontab. The crontab software utility is a time-based job scheduler in Unix-like operating systems. Cron is driven by a crontab (cron table) file, a configuration file that specifies shell commands to run periodically on a given schedule. The crontab files are stored where the lists of jobs and other instructions to the cron daemon are kept. Users can have their own individual crontab files and often there is a system-wide crontab file (usually in /etc or a subdirectory of /etc) that only system administrators can edit.

1. Connect via SSH and update the system software

First of all, connect to your Linux VPS via SSH and update all your system software to the latest version available. You can use the following command to do that:

sudo yum update

2.  Verify if cronie package is installed

To automate the system tasks, or better known as jobs under Linux, you can use a utility called Cron. Using Cron you can run scripts automatically within a specified period of time, create a backup of your databases or other important files, monitor the services running on your server, and many other things. To use the Cron utility, you need to install the cronie package on your system. It should be already installed on your server. To confirm, issue the following command:

sudo rpm -q cronie

3. Install cronie package

If it is not installed, you can use yum to install it. Yum is a package manager which you can use to install and manage software on CentOS 7. Run the command below:

sudo yum install cronie

4. Check if  crond service is running

The cron jobs are picked by the crond service. To check whether the crond service is running on your CentOS VPS, you can use the following command:

sudo systemctl status crond.service

5. Configure cron jobs

To configure cron jobs you need to modify the /etc/crontab file. Please note that it can only be modified by the root user. To check the current configuration, you can use the following command:

sudo cat /etc/crontab

The output should be similar to the one below:

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
37 * * * * root run-parts /etc/cron.hourly
23 5 * * * root run-parts /etc/cron.daily
19 3 * * 0 root run-parts /etc/cron.weekly
23 0 6 * * root run-parts /etc/cron.monthly

As you can see the crontab file already contain an explanation about how to define your own jobs. The syntax is the following:

minute hour day month day_of_week username command

An asterisk (*) in the crontab can be used to specify all valid values, so if you like the command to be executed every day at midnight, you can add the following cron job:

0 0 * * * root /sample_command >/dev/null 2>&1

Your cron job will be run at:

2016-06-10 00:00:00
2016-06-11 00:00:00
2016-06-12 00:00:00
2016-06-13 00:00:00
2016-06-14 00:00:00
...

Specific users can create cron jobs too. The cron jobs for specific users are located in /var/spool/cron/username. When you create cron jobs for specific users you do not need to specify the username in the cron job. Therefore the syntax will be like the one below:

Need a fast and easy fix?
✔ Unlimited Managed Support
✔ Supports Your Software
✔ 2 CPU Cores
✔ 2 GB RAM
✔ 50 GB PCIe4 NVMe Disk
✔ 1854 GeekBench Score
✔ Unmetered Data Transfer
NVME 2 VPS

Now just $43 .99
/mo

GET YOUR VPS
minute hour day month day_of_week command

6.  Restart the crond service

After you make the changes restart the crond service using the command below:

sudo systemctl restart crond.service

For more information you can check the man pages:

man cron

and

man crontab

If it is difficult for you to set up correct cron jobs at the beginning, you can use a cron job calculator to generate the cron job expression. There are several good cron job calculators available on the Internet.

Read Also: Ubuntu crontab


centos crontab

Of course, you don’t have to use CentOs crontab, if you use one of our CentOS VPS hosting services, in which case you can simply ask our expert Linux admins to help you with crontab on CentOS to Automate system tasks. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post, on how to use the CentOS crontab,  please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.

Leave a Comment