Cron jobs: what they are and how to set them up in cPanel

June 5, 2020

The thing that makes using VPS a simple and enjoyable experience is that there are tons of tools out there that help simplify everything: from server maintenance to optimization and more. Control panels, databases, add-ons, and other software greatly improve not only yours but your virtual system’s workflow, too. Of course, scheduling your server’s time-based job is an important aspect of this. Those that want to automate specific repetitive tasks usually implement Cron, a widely used Linux and UNIX-based OS program, and do so through a special feature for it on cPanel, which you easily gain access to just by getting a cheap cPanel license. But how exactly do they work and how can you set them up yourself?

What are cron jobs and what do they do?

As mentioned above, they start scripts that are scheduled to run at a specific time or date. It typically performs and update and can be used to run such tasks as:

  • Website updates;
  • Data visualization;
  • Sending out emails;
  • Expire or delete user accounts;
  • Perform maintenance tasks;
  • Update tables, and much more.

Although the job format might appear confusing at first, it’s actually quite easy to set up and definitely worth using as it saves up much of your time. Below, we’ll break down how cron jobs work and how to create them on cPanel, a feature accessible to those that have even a cheap cPanel license.

Cron job components and prerequisites

A proper, working cron job has to have several important components:

  1. You have to have a script or a task that needs to be executed. To put it simply, it’s ‘the command’.
  2. Then this command has to execute the job repeatedly, which can be set up in cPanel.
  3. Then the script has to have a resulting output or action. The report that this happened is sent to the user’s email address.

Of course, before you set it up in cPanel, it also requires some essential prerequisites:

  • A cPanel username and password.
  • A web hosting account that supports cPanel.
  • A domain name.

Because cron jobs and other useful features can be inaccessible for those only use the trial version, it’s generally recommended getting a cheap cPanel license to unlock all features.

How to set them up in cPanel

You can create a cron job in 4 easy steps using cPanel.

1. Log in to the cPanel account

If you’re not currently using your VPS, you might be logged out of your cPanel account. The first step is to access the control panel through your preferred method and log into the system.

2. Search for cron jobs

Click on the search bar and type in ‘Cron Jobs’. Easily access the cron job menu by clicking either on the icon or the link.

3. Update the cron email

This is useful for those that want to get notifications when their cron jobs start running. Although this is not a necessary step, it’s recommended because these emails can be really convenient to indicate when something goes wrong. If you set up a task and don’t get notified via email, you will know that something isn’t working quite right. On the other hand, if these emails start bothering you, you can stop then by updating your email without inputting anything in the ‘Email’ field.

It should be noted though, that not every executed cron job script sends an email or provides an output. This depends on your choice of PHP code.

4. Set up the cron job

Now, when everything is in place and your cheap cPanel license allows you to use all cPanel’s features, time to scroll down to the ‘Add New Cron Job’ section. Generally, it’s good practice to configure the ‘Common Settings’ before you do anything with the individual time & date elements.

In ‘Common Settings’ you can pick an option from the most commonly used timing intervals. Once you choose one, the system will configure the settings for you by:

  • Minute. This specifies the minute of the hour set for the cron job or the number of minutes between each execution of the script.
  • Hour. This specifies the hour of the day set for the cron job or the number of hours between each execution of the script.
  • Day. This specifies the day of the month set for the cron job or the number of days between each execution of the script.
  • Month. This specifies the month of the year set for the cron job or the number of months between each execution of the script.
  • Weekday. You can set a custom weekly schedule to execute the cron job.

You can enter cron job commands via the Command text box. Remember to always specify the settings for the aforementioned text boxes, including the ‘Command’ one, and be cautious when using the ‘rm’ command. Without the proper options, you can accidentally delete all your home directory data.

The last field in the ‘Add New Cron Job’ section is the ‘Command’ field. Here, you enter the path to the file with the server command. After you complete it, just click ‘Add New Cron Job’ and your script should be up and running at the scheduled time.

For those that want to execute PHP scripts, the command has to consist of two parts:

  • The path to PHP for your account.
  • The path to the file that you are executing.

If you do everything right, the following message should appear: ‘cPanel successfully added the cron job’.

Everything should work fine and have access to all the necessary features as long as you keep renewing your cheap cPanel license. If you want to make any changes to your cron jobs or delete unneeded ones, you can do so by scrolling down to ‘Current Cron Jobs’ and clicking ‘Edit’ or ‘Delete’.

Useful cron jobs

There are some useful cron job commands for those specifically using the cPanel control panel. These are:

  • UPCP. This script updates cPanel & WHM to a newer version. The default settings do execute the script at a random time between 9 PM and 6 AM at a local time. Generally, it’s recommended to change schedule it to run during off-peak hours and once a day.
  • backup. This runs the backups configured in the WHM Backup Configuration interface. It’s advised to schedule it to run during off-peak hours so that the server finishes these backups before peak traffic.
  • CPBACKUP. This runs legacy backups on your server. As with the regular ‘backup’, this should also be scheduled to run during off-peak hours for the same reasons.

Overall, setting up cron jobs is a pretty clear-cut process and doesn’t require a lot of preparation. They can easily become one of the most helpful tools at your disposal, as long as you do them right and have a cheap cPanel license for access to all these features. And while the system performs these repetitive tasks for you, you can focus on what really matters to you.