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.
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.
The Cron job utility is located under the Advanced options of the home page.
Select Cron jobs and you will be taken to the area where you will begin configuring your setup.
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.
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.
If everything well smoothly, your Cron job should be firing, and a message should be sent that looks something like this.
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.
Posted by: Matt Irving on 04/16/2022
Subscribe to my blog!