I\'m using the following command to execute a PHP file via cron
php -q /home/seilings/public_html/dvd/cron/mailer.php
The problem is that I
Use this php_sapi_name() to check if the script was called on commandline:
if (php_sapi_name() === 'cli' OR !strstr(getenv('HTTP_HOST'), ".com")) {
$config["mode"] = "local";
} else {
$config["mode"] = "live";
}
If you want to use "live" on the commandline use this code:
if (php_sapi_name() === 'cli' OR strstr(getenv('HTTP_HOST'), ".com")) {
$config["mode"] = "live";
} else {
$config["mode"] = "local";
}
Another simple solution:
cron:
php -q /home/seilings/public_html/dvd/cron/mailer.php local
php:
if (!empty($argv[0])) {
$config["mode"] = "local";
} else {
$config["mode"] = "live";
}
If you're feeling lazy and do not feel like making sure all those env variables work you might want to try running with cron using:
lynx -dump http://url.to.your.script > /dev/null
Enviroment variable such as HTTP_HOST
exists only when you're running php scrints under web server. But you can add it manually in your crontab config:
## somewhere in crontab config
HTTP_HOST=somthing.com
15 * * * * /path/to/your/script > /dev/null 2>&1
This will enable your script to think that it's running on production enviroment.
When you run the php with cron it is very likely that then environment variable 'HTTP_HOST
' will not be set (or null) and when null is given to strstr
function, strstr
returns false
that is why the mode is set to "local"
.
You're probably getting a different set of environment variables when you execute your command via cron, versus the command line. You might need to write a wrapper script that sets the environment up the way you want it before running your PHP command.