Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute PHP via cron - No Input file specified

Tags:

php

cron

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?

like image 357
Webnet Avatar asked Dec 17 '22 04:12

Webnet


2 Answers

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";
}
like image 59
inakiabt Avatar answered Jan 03 '23 17:01

inakiabt


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";
}
like image 42
powtac Avatar answered Jan 03 '23 18:01

powtac