PHP

ID #125

Can I run a PHP script on cron?

Applies to: Grid System

Yes, you can run a PHP script on cron. Look here to see how to setup your own crontab.

If your PHP scripts do not have executable permissions, 644 or -rw-r--r-- for example, then as part of the command portion of the cron line, you must specify the php interpreter and pass it the filename of the PHP script (including full path to the script from your home directory) that you want executed, like so:

28 14 * * * /usr/local/bin/php -q /myscript.phtml
6 3 20 4 * /usr/local/bin/php -q /htdocs/www/x.php


The first cron line above will run myscript.phtml located in your home directory every day at 2:28PM. The second line will run the script x.php from your /htdocs/www/ directory once a year on April 20th at 3:06AM.

When you explicitly specify the php interpreter /usr/local/bin/php your scripts need not have filenames ending in one of the standard php extensions. Conversely, if your script filenames do not end in one of those PHP extensions, then you must explicitly use the php interpreter in the command portion of your cron as above.

The -q flag suppresses HTTP header output. As long as your script itself does not send anything to stdout, -q will prevent cron from sending you an email every time the script runs. For example, print and echo send to stdout. Avoid using these functions if you want to prevent cron from sending you email.

If your PHP scripts do have executable permissions like 755 or -rwxr-xr-x and they have one of the PHP filename extensions above, then you do not need to specify the php interpreter in the command portion of your cron line, like this:

5 17 * * 2 /myscript.php


The above cron would run myscript.php in your home directory every Tuesday at 5:05PM.

NOTE: If you want to execute your cron job with PHP5.2, go here.


Last update: 2011-12-21 16:37
Author: FAQ Admin
Revision: 1.5

Digg it! Share on Facebook Print this record Send FAQ to a friend Show this as PDF file
Please rate this FAQ:

Average rating: 3.57 (47 Votes)

completely useless 1 2 3 4 5 most valuable

You can comment this FAQ

Comment of Anonymous:
See this for running command line php scripts with arguments:

http://us2.php.net/features.commandline
Added at: 2004-07-15 13:49

Comment of Anonymous:
When you have a PHP script that manipulates file structure (say deleting certain files at a certain time) and that script is activated by a cron job, make sure that you explicitly specify the file system structure in your script -- that is, write out '/htdocs/www/deleteThisFile.txt' instead of $_SERVER[DOCUMENT_ROOT].'/deleteThisFile.txt'

For some reason the cron server doesn't activate the $_SERVER[DOCUMENT_ROOT] variable.
Added at: 2004-08-25 09:09

Comment of Anonymous:
Wow, this was the first tutorial I found (including all of google's first page of results) that a.)told me something other than what the ***** mean in the crontab, and b.) Told me how to execute a php script! I was SO RELIEVED!

I had spent so much time searching through piles of jargon I didn't understand and that didn't give a SINGLE example of how to run a php script via cron (even though they purported to be tutorials) and then I found this. THANK YOU!
Added at: 2004-12-23 07:22

Comment of Anonymous:
I agree with a number of the other comments that are listed. This was a very quick and consise description of how to use cron with php. Every other text that I found was useless.
Added at: 2005-09-20 15:17

Comment of Anonymous:
Here is a good crontab reference:

http://en.wikipedia.org/wiki/Crontab
Added at: 2006-03-08 18:45

Comment of Anonymous:
You can use cURL functions as a replacement for includes:

http://us2.php.net/curl
Added at: 2005-11-15 16:09

Comment of Anonymous:
Actually, there is a way to use includes. I had to do this with the includes because I had multiple levels of scripts and even needed to run the pear::Mail library. The main thing to conceptualize is that you're not using Apache's environement anymore so your paths aren't the same as if you were coming in from a browser. Nevertheless, you can set your env (path) on the fly and it will behave nicely again LOL:

// Set path since we're not using apache env
ini_set("include_path", ".:/usr/lib/php:/usr/local/lib/php:/home/path/to/pear");

// Now I can include these scripts (which in fact call scripts that call other scripts...etc.,etc.
include 'Mine_uses_pear_Mail.php';//Mail actually includes other php...so you see how hairy it can get!
include 'DB.php';

Note that I'm on a shared host, so I ran a phpinfo() script to get the php default paths; than used those + the pear path that I needed; The one dot is for current directory i.e. .:
Also note that the first php script your cron job is running needs to have:
#!/usr/local/bin/php at the VERY beginning (even before the
Added at: 2006-03-06 20:00

Comment of Anonymous:
Be aware that when a PHP script runs from cron here, these $_SERVER values will be different than what you see when you run the PHP script from your own shell:

$_SERVER['PWD'] = '/var/spool'
$_SERVER['HOME'] = 'actual full path to your homedir as told in other FAQ'
$_SERVER['TERM'] = 'dumb'

Whereas PWD when the script is run from your shell will be your present working directory where you were sitting when you invoked the script, HOME will not exist, and TERM would be 'linux'

Other SERVER vars that come from Apache's environment, like PHP_SELF and DocumentRoot will not exist when the script is run from cron or from your shell.

Added at: 2007-07-10 22:27

Comment of Anonymous:
If you really need those server variables you can use the command:
wget http://example.com/cron/script.php

this will essentially make cron call the script as a normal user would, via apache.
Added at: 2008-03-05 06:41

Comment of Anonymous:
WGET is an easy solution when you need to pass parameters in the URL. Just be sure to include quotation marks if needed:
/usr/bin/wget 'http://example.com/cron/script.php?list=1&task=2'
Added at: 2008-12-01 14:09

Comment of Anonymous:
I use an external cron management service (free) http://www.cronwatch.com
Added at: 2010-01-30 14:26

Comment of Chris:
Rather than mess with ini_set, I used an if/else statement for scripts run both by chrontab and through a browser:

if($_SERVER['HOME']){
include($_SERVER['HOME'].'/folder/');
}
else{
include($_SERVER['DOCUMENT_ROOT'].'/../folder/');
}

have chrontab run a script that will email you contents of print_r($SERVER) to see server variables available outside of Apache environment
Added at: 2010-08-24 13:05

Comment of :
Months are not zero based in cron, so 1,2,3 . . . is March 20, not April. :-) 4 would be correct for a month array in Perl or PHP.
Added at: 2010-12-09 16:33