Bookmark and Share
Check Google Page Rank Locations of visitors to this page
Click to get Free offers

Monday, August 4, 2008

Running PHP Scripts with Cron

Lots of programmers like PHP for its ability to code and develop web applications fast. Code-debugging is a lot easier than with PERL or C. However, there is one thing a lot of developers are puzzled about, “How to run PHP Scripts with crontab?”

Cron is normally available on all Unix and Linux distributions; if you cannot access it, contact your root or server administrator. It is a daemon which allows you to schedule a program or script for a specific time of execution. If you want to learn more about cron, click here or type “man crontab” at your command prompt.

I have found myself in the need to run PHP scripts at specific times. For example, to update the content of a website, to remove expired articles, to send out e-mails on a given date and a lot more. While some may think that this is were PHP is doomed, I will show you how it’s done.
A Manual crontab?

The first solution that came to my mind was to run the script directly from my browser (e.g. http://www.mydomain.com/script.php). Since I need to run my script on a regular basis, I squashed that idea. My goodness, all the extra hassle is ridiculous.

An include?

Another possible solution is to include the script in one of the pages of the site, for example the very first: “index.php”. ()

The drawbacks to this solution are, that it works but when someone accesses the “index.php”. This could cause a lot of extra overhead produced by the script. If you get a lot of traffic, the script is executed 1000 times a day and adds a lot of usage on the database and the server.
On the other hand, if you do not get a lot of traffic, or people tend to access your site over another file, this will not work out as well. If you need to run the script on a regular intervals, this is not a solution.
Crontab!

Let’s suppose you either know what cron is or have read about it using the link above. We want to run our script once a minute. So where do we go from here? Here is how you can accomplish this task.
Your PHP setup

You will need to find out the answer to the following question, “Is my PHP installed as CGI or as an Apache module?”. To find out do the following: Create a new file, name it info.php (just an example), and put in the following code, “”. Upload to your webserver and go to it with your browser.

Now check for Server API (4th item from the top), if it says “CGI”, you have PHP compiled as CGI, if it reads “Apache”, you have it running as an Apache module.
Compiled CGI

If the answer to the question above is “CGI” then you need to add a line to your PHP script. It has to be the first line of your script and must contain your server’s PHP executable location:

#!/usr/local/bin/php -q

That looks a lot like PERL now, doesn’t it? After that let’s add the necessary command to our crontab. Edit /etc/crontab and add the following line:

* * * * * php /path/to/your/cron.php

Execute the following from the command line:

Shell> crontab crontab

Be sure your “script.php” has the necessary permissions to be executable (”chmod 755 script.php”).

Now you are all set!
Apache module

If your PHP is installed using the Apache module, the approach is a little different. First, you need access to Lynx (Lynx Browser for more information). Lynx is a small web browser, generally available on Unix and Linux.

Running your PHP script will not require you to add any additional lines. You simply have to edit your /etc/crontab file and add the following line:

* * * * * lynx -dump http://www.somedomain.com/cron.php

Please note that in general, you have to specify the entire URL (with “http://” and so on). But depending on your Lynx’s configuration, the URL might be relative; I suggest always using the absolute reference as in my example above - it always works.

Again execute the following from the command line:

Shell> crontab crontab

That all it takes to get a cron job setup using PHP. Hope you have learned something new and will use it to save overhead time on the server and on the developer.
by till, htmlcenter.com
View blog reactions

2 comments:

RobDudley said...

A good post but a couple of points I would make.

Firstly, if you have a Shebang (and are running on Linux) and the file mode is executable there is no requirement to pass the script to the php binary.

Secondly, if you are using the binary in cron, you should always pass the full path. e.g.

1 * * * * /usr/bin/php /path/to/script.php

as you cannot guarantee that cron will instantiate the PATH environment variable.

Finally, if using the Apache module you can also use wget -q --delete-after http://url/to/script.php and pipe the output to /dev/null ( > /dev/null 2>&1) or curl and pipe the stdout to dev null.

Rob

GT said...

Another thing - if your scrpit has any relative-reference includes
e.g.,

include('../testdir/dbconnect.inc');

then you might as well forget php under cron, because you're going to have to go into every cron'ed script and replace all relative references with absolute references.

PHP does cron (or vice versa) SO badly that I decided instead to use curl and -o /dev/null (as well as /dev/null 2>&1 to suppress cron e-mails).

It makes for an ugly and inefficient crontab, but unless you're prepared to sticky-tape a change to include-path into every script (more overhead) you're on a hiding to nothing.

Cheers


GT
France
GT's markekt Rant