- Bare Metal
- Bare Metal Cloud
- Big Data Benchmarks
- Big Data Experts Interviews
- Big Data Technologies
- Big Data Use Cases
- Big Data Week
- Data Lake as a Service
- Dedicated Servers
- Disaster Recovery
- Industry Standards
- Online Retail
- People of Bigstep
- Performance for Big Data Apps
- Press Corner
- Tech Trends
- What is Big Data
How to Configure Cron Jobs in cPanel
Cron is a service found on all Linux distributions that allows users to schedule commands or scripts that are executed at a specified time or interval. These scheduled tasks are known as cron jobs.
Cron jobs are extremely useful and can serve a variety of purposes. For example, you can start or stop services, you can modify files or databases, or you can send notifications by email. Continue reading this blog post to find out how to setup, list, and check cron jobs in cPanel.
Anatomy of a Cron Job
The cron job syntax might seem confusing at first, but it is actually quite simple. It consists of six fields separated by spaces: the first five are used to define time intervals, while the last is the actual command to be executed. When the asterisk symbol is used in a field, it means the cron will run in all intervals of that specific field.
For example, a cron job that executes a script every hour would look like this:
0 * * * * script.sh
Let’s detail what the first five fields mean, from left to right:
- 1st one is Minutes (0-59)
- 2nd one is Hours (0-23, following the 24-hour format)
- 3rd one is Day of the Month (0-31)
- 4th one is Month (1-12)
- 5th one is Day of the Week (0-7, Sundays can be both 0 or 7). You can also use shortcuts for the days of the week (for example Mon for Monday)
The syntax also supports ranges, lists or steps. Let’s see how that works, in a few examples.
This cron uses a range (1-5) that will run the script every minute of every hour, only during weekdays:
* * * * 1-5 script.sh
Lists are separated by a coma. Let’s say we want our script to run every day, but only at 9:30 (9:30 AM), 10:30 (10:30 AM) and 15:30 (15:30 PM):
30 9,10,15 * * * script.sh
If we want a cron to be executed every 5 minutes, we can use a step for that purpose (by using a slash):
* /5 * * * * script.sh
We will not go into further details, but keep in mind that cron syntax is very flexible, and other shortcuts are also supported.
How to Setup Cron Jobs
Although cron jobs can be configured from the command line, it is easier to do it from the cPanel interface, especially if you have no experience in system administration. The menu is found in the Advanced section:
The top part of the interface allows you to configure an email address where the output of cron commands will be sent, if needed. Just enter the address in the Email field, then click on Update Email, or follow the instructions to redirect the output to /dev/null if you don’t want the email to be sent.
Scroll down in order to add a new cron job and you will find a very helpful interface that makes setting the time intervals a trivial task. Configure the fields with the desired values and then click on Add New Cron Job:
You will notice that cPanel includes drop-down menus with pre-defined settings for the most common tasks.
The general Common Settings drop-down menu on top can be used to execute commands twice per hour, for instance:
There is a similar menu for each field as well, here is the one for the Month field:
Once added, the cron will run automatically at the designed intervals, until you remove it.
How to List Cron Jobs
In order to list the current jobs, scroll to the bottom of the Cron Jobs interface:
You can use the buttons in the Actions column to Delete or Edit an existing cron job.
How to Check Cron Jobs
As explained above, the easiest method is to set up email notifications that will send an email with the output every time a cron job runs.
More advanced troubleshooting is also possible from the command line, if you have root access, by investigating the cron log file found at /var/log/cron.
If you don’t have shell access or administrator privileges, you can ask your hosting provider to investigate any cron issues.
Tips & Tricks for Cron Jobs
The minimum interval to run a cron job is one minute. What if you need to run a script every 30 seconds, for example? While this is not possible by default, you can use two separate crons and the Linux sleep command as a workaround:
* * * * * script.sh * * * * * sleep 30; script.sh
While the first cron will execute your script every minute, the second one will wait 30 seconds and execute it again.
Another very important aspect to keep in mind is that cron jobs use their own PATH variable that can be different from the system PATH settings. As a result, some commands that work when executed in the terminal might not be found when included in a cron.
The safest way around this is to always use full paths in your crons, for both normal commands and scripts. The cPanel Cron Jobs interface provides full path examples for php scripts:
Cron is a simple but very powerful tool that allows Linux users to automate repetitive tasks. The interface provided by cPanel makes cron job management trivial even for people with no experience in system administration.
As usual, please contact us for any feedback or questions, our sysadmins are ready to help you with any issues related to cron jobs.
About the Author
Dragos Baldescu is a Level 2 Technical Support Engineer at Bigstep, passionate about Linux and testing out new technologies and solutions.