Automate system tasks using cron on CentOS 7
In this tutorial we are going to show you how to automate system tasks on CentOS 7. Centos crontab is an operating system command.
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
To automate the system tasks, or better known as jobs under Linux, you can use an utility called Cron. Using Cron you can run scripts automatically within a specified period of time, create 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
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
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
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 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 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:
minute hour day month day_of_week command
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:
If it is difficult for you to set up correct cron jobs at the beginning, you can use some cron job calculator to generate the cron job expression. There are several good cron job calculators available on the Internet.
Of course you don’t have to Automate system tasks using cron on CentOS 7, if you use one of our Linux VPS hosting services, in which case you can simply ask our expert Linux admins to help you Automate system tasks using cron on CentOS 7. They are available 24×7 and will take care of your request immediately.
PS. If you liked this post please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.