In this recipe, we will investigate the role of server automation and the convenience of running specific tasks at predefined periods by introducing you to the time-based job scheduler known as cron. Cron allows for the automation of tasks by enabling the administrator to determine a predefined schedule based on any hour, any day, or any month. It is a standard component of the CentOS operating system, and it is the purpose of this recipe to introduce you to the concept of managing recurring tasks in order to take advantage of this invaluable tool and to make CentOS work for you.
To complete this recipe, you will require a minimal installation of the CentOS 7 operating system with root privileges, and a console-based text editor of your choice. The crontab
program uses Vim for file editing. If you do not know how to work with Vim, go through the tutorial shown in the recipe Introduction to Vim in Chapter 2, Configuring the System.
The purpose of this recipe is to create a script that will write the time and date with a few words of your choice to a text file every five minutes. This may seem to be a relatively simple exercise, but the intention is to show you that, from such simplicity, cron can be used to do so much more that will make working with CentOS an absolute pleasure.
crontab -e
hello world
to a file located at /root/cron-helloworld.txt
every five minutes. To do this, add the following line:*/5 * * * * echo `date` "Hello world" >>$HOME/cron-helloworld.txt
crontab: installing new crontab
cron
job and will automatically activate it. You can view the output of the script by reviewing the file found at /root/cron-helloworld.txt
(you have to wait 5 minutes), or by monitoring the logfile found at /var/log/cron
(use tail -f /var/log/cron
and Ctrl+C
to exit).Cron is the name of a program that enables CentOS users to execute commands or scripts automatically at a specified time and date. Cron's settings are kept in a user-specific file called crontab
, and as we have seen in this recipe this file can be edited to create automated tasks as often as they are required.
So what did we learn from this experience?
The example used was very simple, but in many ways this was the purpose of this recipe. Crontab uses a daemon, crond
, which runs constantly in the background and checks once a minute to see if any of the scheduled jobs need to be executed. If a task is found, then cron will execute it. To edit an existing crontab
file or to create a new crontab
, we use the crontab -e
command. To view a list of current cron jobs, you can type crontab -l
. Alternatively, to view a list of the current jobs for another user, you can type crontab -u username -l
. Tasks or jobs are generally referred to as cron jobs, and by avoiding complication in our first script, it was the intention to show you that the nature of command construction was very simple. The formation of a cron job looks like this:
<minute> <hour> <day of the month> <month of the year> <day of the week> <command>
Entries are separated by a single or tabbed space, and the allowed values are primarily numeric (that is, 0-59
for a minute, 0-23
for an hour, 1-31
for a day of the month, 1-12
for month of the year, and 0-7
for day of the week). However, in saying this, it is also true to say that there are more specific operators ( /
, -
) and cron-specific shortcuts (that is, @yearly
, @daily
, @hourly
, and @weekly
) that do allow for additional controls. For example, where the /
operator is used to step through specified units, it can be read as every, so in our recipe the use of */5
will run the task every five minutes while the use of */1
runs the task every minute. As an addition to this, you should be aware that the use of this syntax will align all commands on the hour. So, with this in mind, the most suitable template or starting point for anyone wanting to write their first cron
job is to start with a series of five asterisks followed by the command, like this:
* * * * * /absolute/path/to/script.sh
Then, proceed to configure the minute, hour, day, month, and day-of-the-week values as desired. For example, if you want a particular PHP script to run at 8 P.M. (20:00 hrs) on every weekday (Monday-Friday), it may look like this:
0 20 * * 1-5 /full/path/to/your/php/script.php
So, with this in mind, and by completing this recipe, you can see how cron can be used to manage a database backup, run a scheduled system backup, provide support to websites by activating scripts at predefined intervals, or run various bash scripts and a whole lot more.
To delete or disable a cron job, it is simply a matter of either removing the instruction from an individual user's cron file or by placing a hash (#
) at the beginning of the line. Individual cron files can be found at /var/spool/cron/<username>
, and the use of the hash will either disable the cron job or allow you to write comments. To completely remove a crontab
file, you can also use crontab -r
. For example, if you want to remove the cron job created in the main recipe, you can log in as root and begin by typing the command, crontab -e
. At this point, you may either remove the entire line or comment it out, as shown here:
# */15 * * * * echo `date` "Hello world" >>$HOME/cron-helloworld.txt
Next, save the file. There are also some special cron directories in the filesystem for system-wide cron jobs that will, if you drop a script file in it, run it automatically at a certain time point. The folders are called cron.daily
, cron.hourly
, cron.weekly
, and cron.monthly
in the /etc
directory, and their names refer to the time point that they are run. Just remove the script from the folder if you don't want to execute it anymore. Take a look at the Monitoring important server infrastructure recipe for an example.