Cron not passing params to PHP script

前端 未结 3 1609
轻奢々
轻奢々 2021-01-17 03:21

I have a cron job set like

php /home/novinarb/public_html/index.php --uri=\"cron/24satahr\"

but the \'uri\' param doesn\'t get to the php

相关标签:
3条回答
  • 2021-01-17 03:52

    Is the php script running at all?

    I suspect you need to provide the full path to php in your crontab line. Even though cron jobs run as you, they don't have any of your login environment set up; this means they don't have your $PATH.

    0 讨论(0)
  • 2021-01-17 03:54

    I was facing the same problem but was able to fix it after reading the manual entry for php.

    Initially I had something set like:

    /usr/bin/php -f /path/to/php/script.php -c 1426 >> /tmp/script.php.log 2>&1
    

    I was able to fix it by changing the line to:

    /usr/bin/php -f /path/to/php/script.php -- -c 1426 >> /tmp/script.php.log 2>&1
    

    As per the manual entry the syntax is:

    php [options] [ -f ] file [[--] args...]
    

    Also,

     args...        Arguments passed to script. Use '--' args when first argument starts with '-' or script is read from stdin
    

    Going by that, my cron command becomes:

    /usr/bin/php -f /path/to/php/script.php -- -c 1426 >> /tmp/script.php.log 2>&1
    

    and it works!

    0 讨论(0)
  • 2021-01-17 04:00

    A more robust method would be to accept command-line arguments in your PHP script with getopt() or $argv and making it executable. An example with $argv called script.php:

    #!/usr/bin/php
    <?php
      if (isset($argv[1])):
        echo $argv[1];
      endif;
    ?>
    

    Make it executable:

    chmod +x script.php
    

    And execute:

    ./script.php "cron/24satahr"
    

    Will output:

    cron/24satahr
    
    0 讨论(0)
提交回复
热议问题