{"id":56765,"date":"2020-08-20T10:42:24","date_gmt":"2020-08-20T15:42:24","guid":{"rendered":"https:\/\/blog.cpanel.com\/?p=56765"},"modified":"2020-08-20T10:42:24","modified_gmt":"2020-08-20T15:42:24","slug":"how-to-configure-a-cron-job","status":"publish","type":"post","link":"https:\/\/devel.www.cpanel.net\/blog\/tips-and-tricks\/how-to-configure-a-cron-job\/","title":{"rendered":"How To Configure a Cron Job"},"content":{"rendered":"\n
Do you find yourself wasting hours on tedious and repetitive server administration tasks? If you answered yes, you\u2019re going to love this article, which explores how cron and cPanel & WHM help you automate these boring but essential aspects of web hosting.<\/p>\n\n\n\n
Cron is a time-based scheduler. It allows server administrators to run scripts known as cron jobs automatically at scheduled intervals. cPanel & WHM runs several essential server maintenance scripts in this way, and you or your users can schedule scripts too. You might, for example, configure a custom backup script to run at 7 a.m. each morning.<\/p>\n\n\n\n
We\u2019re going to look at some common uses of a cron, including how to schedule scripts, and how to add new jobs to automate server admin tasks.<\/p>\n\n\n\n
Cron can run simple commands, shell scripts, and programs written in languages such as PHP, Python, and Perl. If a task can be completed on the command line or in a script, you can automate it with the task scheduler.<\/p>\n\n\n\n
Cron uses a compact notation to represent times and days. A typical configuration line might look like this:<\/p>\n\n\n\n
30 23 25 * * myscript.sh<\/p>\n\n\n\n
The format has five white-space separated fields for time intervals followed by a command. <\/p>\n\n\n\n
The schedule is stored in a \u201ccrontab\u201d file, a list of jobs with additional directives. You can edit the file over SSH with the crontab<\/em> command, but using cPanel & WHM is faster, easier, including two interfaces for scheduling jobs: one for cPanel\u2019s maintenance scripts and one for user scripts, which we\u2019ll take a look at in the next section. <\/p>\n\n\n\n Before we get to cPanel\u2019s cron configuration interface, you should have a firm grasp of the interval notation. <\/p>\n\n\n\n The time fields represent the following, from left to right:<\/p>\n\n\n\n Fields with an asterisk indicate that the job will run at every relevant interval compatible with the other fields.<\/p>\n\n\n\n Our first example specified the time interval \u201c30 23 25 * *\u201d. This is the 30th minute of the 23rd hour of the 25th day of the month. Or, to put it another way, 11:30 in the evening on the 25th of each month. Because the Month and Day of Week fields contain asterisks, the script runs on days and months for which the other fields are valid; in this case, every day and month. <\/p>\n\n\n\n Let\u2019s look at some more complex examples. You have a monitoring script that you would like to run every hour, on the hour, between 9 a.m and 5 p.m., but only on weekdays. To achieve this, we have to introduce another piece of notation, the range.<\/p>\n\n\n\n 00 9-17 * * 1-5 myscript.sh<\/p>\n\n\n\n Ranges are values separated by a hyphen. The example breaks down as:<\/p>\n\n\n\n What if, instead of every hour between 9 a.m. and 5 p.m, you wanted the script to run at 9 a.m, 12 p.m., 3 p.m., and 6 p.m on weekdays. For intervals of this type, you need one more piece of notation, the list. <\/p>\n\n\n\n 00 9,12,15,18 * * 1-5 myscript<\/p>\n\n\n\n This example is the same as the previous one, but with a list in the Hours field instead of a range. <\/p>\n\n\n\n The basics we have outlined here should cover almost every situation, but cron has an incredibly flexible notation with additional shortcuts and directives. If you want to learn more, take a look at this article<\/a> or the man page on your CentOS server with the command \u201cman 5 crontab.\u201d<\/p>\n\n\n\n cPanel & WHM schedules several scripts<\/a>, including the upcp<\/em> update script, backup,<\/em> and cpbackup<\/em>. Server administrators can adjust when these scripts run in WHM.<\/p>\n\n\n\n Select Configure cPanel Cron Jobs<\/em> in Server Configuration<\/em> in the sidebar menu.<\/p>\n\n\n\nConfiguring cPanel Cron Jobs in WHM<\/strong><\/h3>\n\n\n\n