Comprehensive guide on working with Cron Jobs in cPanel
  John Mwaniki /   24 Aug 2021

Comprehensive guide on working with Cron Jobs in cPanel

Have you been receiving happy birthday SMS from your mobile network service providers and banks every year?

Have you been receiving SMS or email reminders to pay (weekly, monthly or annual) subscription fees for some services you have subscribed to, just before the renewal date is due?

Ever received email newsletters from websites/blogs you have subscribed for, at regular intervals?

Or perhaps received your bank statement (of the previous month) in your email at the beginning of every new month? Like 12:00 Am of every date 1, and you are like... Damn, these guys are so accurate... how do they even do it?

All the above scenarios are automated tasks that execute automatically without human intervention.

As a developer, you could be wondering, "How can achieve scheduling and automating tasks based on time in my app or website?".

In this article, we will look into detail how you can schedule scripts in your website or app to execute and perform various tasks automatically without human intervention using a UNIX feature called Cron Jobs.

What is Cron Jobs?

Cron is a UNIX-like operating system (eg. Linux) utility/service that runs in the background and executes commands (jobs) on your server automatically at a specified time and date, or repeatedly at a regular interval.

These scheduled commands or tasks are known as “Cron Jobs”.

Jobs and their schedules are defined in a configuration file called a crontab. This file is located in a crontab directory, /etc/cron.*/ and can only be edited by system administrators.

Cron jobs are great and best on computers that run 24/7 eg web servers.

Though cron jobs are mostly used by system administrators (in performing tasks such as scheduled backups, automated system maintenance and disk space monitoring), they are also incredibly applicable to web and app developers in their apps.

Application of Cron Jobs in web/app development

Cron jobs are very useful in programming as they allow you to schedule tasks that your system executes automatically.

Below are a number of possible use cases where cron jobs can be incredibly useful:

  • Sending emails or SMS reminders for attending a meeting or an event some hours or days before the actual time.
  • Sending out invoices to customers when a recurring billing is due.
  • Scheduling email newsletters to be sent at a later date and time or at regular intervals.
  • Deactivating and/or deleting expired user accounts.
  • Sending out monthly or annual financial statements.
  • Sending out birthday wishes and season greetings(eg. Christmas, Ramadan, etc) to registered members.
  • Syncing of data between different applications.
  • Erasing cached data files in a certain interval.

Setting up Cron Jobs in your hosting account cPanel

cPanel has a feature that allows you to automate/schedule your tasks with cron Jobs.

How it works

Step 1: Write the code you want to be executed at regular time intervals and upload it on any directory of your choice in your web server.

Step 2: Click on “Cron jobs” under the Advanced section of your cPanel homepage.

Setting up cron jobs in cpanel

Step 3: Scroll down to “Add New Cron Job”.

Select “Common settings” and set to your preferred time interval to execute your code. You can set it to a minimum of 1 minute to have it run 60 times every hour.

Note: Some hosting administrators reset the cronjob time interval if you set it too tight such as one minute as they claim it overloads the server. I recommend you set it to at least 15 mins intervals unless it is absolutely necessary to put it lower.

Then under the command textbox, enter this command with the URL to your code.

Creating a cron job in cpanel

Command breakdown:

wget -O /dev/null https://www.webdevsplanet.com/cronscript.php

The last part (https://www.webdevsplanet.com/cronscript.php) is the URL to the file with the script to be executed.

wget is a Linux command-line utility that helps to download files from web servers using HTTP, HTTPS, and FTP protocols.

This command (wget https://www.webdevsplanet.com/cronscript.php) would still work the same way as the above command. However, every time the command is executed an output file will be created in the home directory of the file manager. With time the output files will be so many and occupy much of the storage space.

-O /dev/null prevents the creation of any output file when the command is run.

You can set up as many cron jobs as you like. Each of your jobs can be executed up to 60 times an hour.

You will be able to see all the created cron jobs under the "Current Cron Jobs" table on the Cron Jobs page as below. To edit or delete a cron job, just click on the respective option on the record in the Actions column.

Current cron jobs in cpanel

Conclusion

Whether you are a system administrator or a web developer, cron jobs are very crucial and can save you a big deal.

With the help of cron jobs, you can schedule tasks to run automatically and this can prevent you from missing the action on time.

In this article, we have covered in detail what cron jobs are, where they are most applicable in your web applications, and how you can set them up by yourself in your hosting account cPanel.