Execute PHP via cron - No Input file specified

后端 未结 6 1150
醉梦人生
醉梦人生 2021-01-14 02:13

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

相关标签:
6条回答
  • 2021-01-14 02:50

    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";
    }
    
    0 讨论(0)
  • 2021-01-14 02:53

    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";
    }
    
    0 讨论(0)
  • 2021-01-14 02:56

    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

    0 讨论(0)
  • 2021-01-14 03:06

    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.

    0 讨论(0)
  • 2021-01-14 03:11

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

    0 讨论(0)
  • 2021-01-14 03:12

    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.

    0 讨论(0)
提交回复
热议问题