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

Hope you found this post useful...

...so please read on! I love writing articles that provide beneficial information, tips and examples to my readers. All information on my blog is provided free of charge and I encourage you to share it as you wish. There is a small favour I ask in return however - engage in comments below, provide feedback, and if you see mistakes let me know.

If you want to show additional support and help me pay for web hosting and domain name registration, donations, no matter how small, are always welcome!

Use of any information contained in this blog post/article is subject to this disclaimer.
comments powered by Disqus
Other posts you may like...