Running PHP scripts within the cron environment can be tricky!
A common technique for executing PHP scripts from a cron job is to use a command line utility like curl
or wget
to execute and subsequently retrieve the output of the script. Using this method, the command in your cron job would look like this:
wget http://domain.tld/script.php
However, if the script is located within your site, you don't need to take the extra step to access it using an HTTP retrieval tool such as wget
or curl
. Instead, you can execute the script directly by invoking the PHP interpreter. This method tends to be more efficient and equally effective. The command in your cron job would look like this:
/usr/local/bin/php -q /full/path/to/yourscript.php
The -q
option suppresses HTTP output. You can omit this parameter if you need the script's HTTP output.
Another alternative that we have found works well is to prepend your cron job entry with a "cd /path/to/file; " so that the cron will be in the correct location, then call the "php yourscript.php" to execute it. That would be accomplished by doing:
cd /full/path/to/script; /usr/local/bin/php -q script.php
As above, the -q
parameter can be omitted if output capturing is desired.
Please note: If you do need a copy of wget
or curl
, please submit a ticket at http://support.eicra.com with your request and a brief description of what you'll be using it for. We'll provide you with a copy of the binary in your ~/bin
directory pending approval by our server technicians.
A command line version of PHP can be found at:
/usr/local/bin/php
This can be used in cron scripts instead of using lynx, etc to pull a file from the site to perform actions via the cronjob.
If your script requires unusual options, you may have to reference an alternate php.ini file as such:
/usr/local/bin/php -c /home/username/php.ini /home/username/www/script.php
php.ini or php.ini-dist can be downloaded on the web, or as part of the php source tarball. You may edit this script to taste for running command line php scripts.
Support-Agent
Comments