Execute PHP via cron - No Input file specified
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 Have a file that's included in the execution that determines which config to load.... such as the following:
if (!strstr(getenv('HTTP_HOST'), ".com")) {
$config["mode"] = "local";
} else {
$config["mode"] = "live";
}
The cron is loading the LOCAL config when it should be loading the LIVE config. I've 开发者_运维问答tried using the http:// URL to the file instead of the absolute path but it didn't find the file. Do I need to change the command to use a URL within it?
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";
}
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";
}
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.
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.
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
精彩评论