Automating system tasks using cron on CentOS 7

In this tutorial we are going to show you how to automate system tasks on a Linux VPS kernel, let’s take CentOS 7 as the operating system.

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

sudo yum update

To automate system tasks, or better known as jobs under Linux, you can use a utility called Cron… With Cron, you can run scripts automatically over a period of time, back up databases or other important files, monitor services running on your server, and more. To use the Cron utility, you need to install the package cronie on your system. It should already be installed on your server. To confirm, enter the following command:

sudo rpm -q cronie

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

sudo yum install cronie

The cron jobs were selected by the crond service. In order to check if the crond service is running on your CentOS VPS, you can use the following command:

sudo systemctl status crond.service

To configure cron at the workplace, you need to change the file /etc/crontab… Please note that it can only be changed by the superuser. 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 contains explanations on how to define your own working files. The syntax is as follows:

minute hour day month day_of_week username command

Star

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

in crontab can be used to define all valid values, so if like the command will be executed every day at midnight, you can add the following task:

2016-10-15 00:00:00
2016-10-16 00:00:00
2016-10-17 00:00:00
2016-10-18 00:00:00
2016-10-19 00:00:00
...

Your cron will run on the following days:

/var/spool/cron/usernameSpecific users can create tasks too. Cron jobs for specific users are located in

minute hour day month day_of_week command

… When creating tasks for specific users, you do not need to specify the username in cron. Therefore, the syntax will be as in the picture below:

sudo systemctl restart crond.service

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

man cron

For more information, you can check the man page:

man crontab

and

Automating system tasks using cron on CentOS 7 PS

… If you liked this post, please share it with your friends on social media using the buttons on the left side of the post, or just leave a comment below. Thank you in advance.

Sidebar