4

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?

6 Answers 6

7

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";
}
Sign up to request clarification or add additional context in comments.

Comments

5

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";
}

3 Comments

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.....
I used [code]if (php_sapi_name() != 'cli' && !strstr(getenv('HTTP_HOST'), ".com")) { $config["mode"] = "local"; } else { $config["mode"] = "live"; }[/code] It didn't work I need it to use live when it's run via command line
It still loaded the local config instead of live during execution.
0

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

Comments

0

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.

Comments

0

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.

2 Comments

this is the opposite of what is needed, the .com will cause script to behave as if in production when run from cron
"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)
0

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

2 Comments

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.
you're right. I'm used to having lynx around all the time. The wget arguments to achieve the same result: wget -q -O - webvuln.com > /dev/null

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.