Igor Kromin |   Consultant. Coder. Blogger. Tinkerer. Gamer.

I had a need to run a certain PHP script on a regular basis as a part of my blog maintenance. Since GoDaddy uses the more-or-less standard cPanel which includes a Cron Job scheduling option, making this happen was fairly trivial. This is a quick guide to show how to schedule a PHP script to run automatically at regular intervals.

I am assuming that the Linux cPanel is used in this case.

The first thing that is needed is to find the name of the home directory. This is displayed in the top left on the main cPanel page. The name of this directory is the same as the cPanel login, so if your login as 'bob_the_builder', the home directory will be /home/bob_the_builder.
cron4.png


Now with the home directory name handy we can schedule the Cron Job itself. Head down to the 'Advanced' section and click on 'Cron Jobs'.
cron1.png




Under the Add New Cron Job section, select the scheduling. There is a handy drop down that gives a number of usual configurations to pick from. After picking the scheduling times, the important part is to enter the command correctly.
cron2.png


The command is specified like this:
 Cron Command
php -f /home/<account_name>/public_html/<script_name>.php


This assumes that the PHP script is in the public_html directory i.e. the root directory of the web site. Simply put in your account name and script name to make it work.

After clicking Add New Cron Job the page should display it as below...
cron3.png


So this will run the PHP script on a regular interval, every Friday in my example. This will also email the output of the job to the email address specified on the Cron Jobs page. If you don't want to get these emails, add this to the end of the command: > /dev/null 2>&1. This makes the command look like this...
 Cron Command without Email
php -f /home/<account_name>/public_html/<script_name>.php > /dev/null 2>&1


If you need a way to passing parameters to your script, check out this article - How to pass parameters to your PHP script via the command line.

-i

A quick disclaimer...

Although I put in a great effort into researching all the topics I cover, mistakes can happen. Use of any information from my blog posts should be at own risk and I do not hold any liability towards any information misuse or damages caused by following any of my posts.

All content and opinions expressed on this Blog are my own and do not represent the opinions of my employer (Oracle). Use of any information contained in this blog post/article is subject to this disclaimer.
Hi! You can search my blog here ⤵
NOTE: (2022) This Blog is no longer maintained and I will not be answering any emails or comments.

I am now focusing on Atari Gamer.