How To Set Up A Cron Job

automate tasks with cron job

What is a Cron job?

A Cron job is a Linux command that executes a script a pre-defined interval. Within the command you’ll need to specify the location of the script that is to be executed. If you are using a hosting platform like CPanel or BlueHost, there is a UI tool that makes creating and scheduling Cron job super easy.

Why do I need a Cron job?

Cron jobs are excellent tools for automation. Mundane tasks like archiving old records or sending email reminders can all be handled with a script and Cron job.

Get started by writing your script. In our example, we’ll be writing a PHP script that sends an e-mail reminder to customers who have missed a payment.

Here is the script.

Test your script a few times to ensure it is working prior to mapping it to a Cron job. If all looks well, proceed to the next step.

How do I set a Cron job up in Cpanel?

The Cron job utility is located under the Advanced options of the home page.

cpanel cron job

Select Cron jobs and you will be taken to the area where you will begin configuring your setup.

how to set up email notifications in cpanel

While testing, it’s a good idea to add an email that will update if your Cron job runs and produces an output. To get started, decide where you will be placing your script file. Be sure to name it something like “cron-email-reminder-script.php” so you don’t lose track of it. CPanel provides a sample script that works just fine to get started.

/usr/local/bin/php /home/< username >/public_html/path/to/cron/script

Now that you have your command ready, move on to set the frequency. When your app is in production, the interval needs will vary, but for testing purposes an interval of one minute should get you a faster test result.

how to set up email notifications in cpanel

Just remember to disable it after testing so you don’t bog down server resources. Next, place your command in the command section and select Add New Cron Job.

linux cron commands

If everything well smoothly, your Cron job should be firing, and a message should be sent that looks something like this.

cron job not working

What if I receive a "Could not open input file:" message?

If you receive a message stating that the Cron job was not able to open the input file, it likely means there was a type in the command. It could also mean that you path was incorrect. Double check the location on your script and try again.

Another possible cause could be the synax of the command itself. You need to have a space in between "php" and the path to your script.

Cron jobs are extremely powerful automation tools. With only a few lines of code, you can reduce human error by letting the application do the boring stuff for you.

Matt Irving is the CEO of Super Easy Tech, LLC.
Matt is the CEO of Super Easy Tech and creator of Super Easy CRM. He is a passionate software engineer, tech blogger, and gamer. Feel free to connect on any of the platforms listed below.

Posted by: Matt Irving on 04/16/2022