tags:

views:

384

answers:

6

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?

+5  A: 

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";
}
powtac
I take it php_sapi_name() === 'cli' checks to see if it is command line, meaning I need it to be !=.... I'll test it out.....
Webnet
I used[code]if (php_sapi_name() != 'cli' } else { $config["mode"] = "live";}[/code]It didn't workI need it to use live when it's run via command line
Webnet
It still loaded the local config instead of live during execution.
Webnet
A: 

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".

NawaMan
A: 

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.

Jim Lewis
+3  A: 

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";
}
inakiabt
A: 

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.

Ivan Nevostruev
this is the opposite of what is needed, the .com will cause script to behave as if in production when run from cron
Jim
"The cron is loading the LOCAL config when it should be loading the LIVE config" - my solution will fix this ("live" config will be loaded from cron)
Ivan Nevostruev
A: 

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

pcp
wget makes more sense than lynx. And this is not a lazy solution. Coding only for web access is easier to maintain than having to code for both web access and cli access.
jmucchiello
you're right. I'm used to having lynx around all the time.The wget arguments to achieve the same result:wget -q -O - http://webvuln.com > /dev/null
pcp