How do I run my PHP script as a scheduled (cron) task? Print

Scheduling tasks allows you to automate various scripts to be run from your hosting package.

Running PHP files on your site is a commonly requested task.

This is absolutely possible, and easy, providing you know how it's done.

The first thing you need to know is how command lines are structured.

They are structured as follows:

/path/to/interpreter /path/to/file_to_be_interpreted Argument_to_be_passed_to_file_to_be_interpreted

In this example for a PHP file, you'd need something like this:

/usr/bin/php /home/sites/ task=tidy owner=1

The above file specifies that you want the PHP interpreter to interpret the cron.php file in the webspace of, giving it the arguments task=tidy and owner=1.

This is the equivalent of loading the following URL from a web browser:

Specifying the PHP version

You can specify what version of PHP you want to use to run your PHP script in your command.

We have multiple versions of PHP installed on our servers (versions 4, 5.2, 5.3, 5.4 and 5.5), all of which can be used to run a PHP script as a scheduled task.

To specify a specific one, you just need to amend the path to the interpreter in your command.

The /usr/bin/php path refers to a symbolic link, which refers to the default version of PHP on our servers (version 5.3).

Here are the paths to each version of the interpreter should you wish to specify:

  • Default PHP version: /usr/bin/php
  • PHP 4: /usr/bin/php4
  • PHP 5.2: /usr/bin/php5
  • PHP 5.3: /usr/bin/php53
  • PHP 5.4: /usr/bin/php54
  • PHP 5.5: /usr/bin/php55

Was this answer helpful?

« Back
Venatour Sports Travel

Encode have continually surpassed themselves in developing our new website. Their patience in learning and understanding what we needed from a website to provide the best service to our customers has been beneficial on so many levels. I would highly recommend them to anyone looking for intelligent solutions to their web, seo or business needs.

Lisa Cooley

Positive: Professionalism, Quality, Responsiveness, Value

Working with Neil is a dream; he's fast, efficient, responsive and full of website knowledge and advice. He won't baffle you with IT over load and will be honest about the best working solution.

Highly recommended!

Ellie Stevenson Author

Positive: Professionalism, Value

Encode and I had a very useful discussion about Wordpress and related issues at no cost to myself. He gave me some very helpful information and answered the questions I had.

I learnt quite a lot! Thank you.

Silvia Boev

Positive: Professionalism, Quality, Responsiveness, Value

I am very excited to work again with Neil. His attention to detail is second to none. I have seen his work in action in the past and he is great. I love his calm approach and his patient guidance to non-tech people like me. I would highly highly reccommend him!

Dr Fuz

Neil is the best! He has sorted out all our business emails and everything runs a great deal better now. Website issues are now a thing of the past as Neil has introduced a maintenance programme to ensure the site is always working optimally and any content changes are actioned pretty much immediately.

Highly recommended!!

H Scott-Brown

'Encode' provided my hosting and domain transfers. They were able to deal with the technicalities of the transfer in a particularly speedy manner, taking all the worry of the transfer, domain issues DNS and nameserver headaches as well as the migration. A couple of emails, and phone call meant it was done smoothly, swiftly and efficiently. All I can say is I'm really please they were recommended to me. Thank you