php-cron脚本未从推荐行读取参数


php cron script not reading arguments from commend line

我有两个文件。

一个是一个简单的文本文件,它有我的scron脚本的所有真实路径链接,带有参数

另一个文件是我的cron脚本本身。

我的contab文本文件就是这样的:

#!/bin/sh 
/usr/bin/php -f /home/path/reports/report.php arg1
/usr/bin/php -f /home/path/reports/report.php arg2

cron脚本读取crontab文件中的参数,并根据它的参数运行

report.php--

php $args = $argv[1];
 $count = 0;
switch($args){
   case 'arg1':
     code and create certain file .... 
   exit;
   case 'arg2':
         code and create certain file  ... 
   exit;
    }   // <--- this script runs perfectly if I run script manually through putty commend line, meaning it will run exactly what I want depending on what $argv[1]  I put in manual commend line,  BUT doesn't run automatically from crontab script

这个文件没有运行,也不知道为什么,当我通过推荐行手动运行report.php时,它会运行,它可以工作。

我注意到的一件事是将report.php更改为:

report.php--

$args = $argv[1];
 $count = 0;
switch($args){
   case ($args ='arg1'): // <- just putting the equal sign makes it work
     code and create certain file .... 
   exit;
   case ($args = 'arg2'):
         code and create certain file  ... 
   exit;
    } // <-- this script was a test to see if it had anything to do with the equal sign, surprisingly script actually worked but only for first case no what matter what argv[1] I had, this is not what I am looking for. 

问题是它只适用于第一种情况,无论我在crobtab的文本文件中放入什么参数,它总是运行第一种情况。这可能是因为我说$args="arg1",所以它总是将其视为arg1。

所以我试着通过这样做来实现它:

report.php--

$args = $argv[1];
 $count = 0;
switch($args){
   case ($args =='arg1'): // <- == does not work at all....
     code and create certain file .... 
   exit;
   case ($args == 'arg2'):
         code and create certain file  ... 
   exit;
    } // <--- this script runs perfectly if I run script manually through putty commend line, but not automatically from crontab script

并且它什么都不运行,它根本不接受我的论点,只是要注意的是,如果我在推荐行手动运行,这个带有comparison"=="的report.php文件运行得很好。

发生了什么事?当我使用"=="从crontab文件中查找参数时,为什么cron脚本没有正确读取我的参数。

至于$argv->"注意:当register_argc_argv被禁用时,这个变量不可用。"。我建议切换到$_SERVER[‘argv']和$_SERVER[‘argc'](是的,你没看错),而不是$argv/$argc的东西。

至于这个

case ($args ='arg1'): // <- just putting the equal sign makes it work

伙计,你显然不明白你在做什么,($args='arg1')和($args='arg1])之间有什么区别!

--【以下注释代码】----

将其保存为test.php:

<?php
echo $_SERVER['argv'][1] ."'n";

并进行测试。

$ php test.php abd
abd

第一个之所以有效,是因为如果使用=而不是==,则在if语句中设置变量。

试试var_dump($argv),看看是否有任何东西被分配给了这个变量。

@arxanas在评论中提到,您的开关是AFU。请阅读此处的文档。case语句已经完成了相当于$x == [value]的操作,所以您只需要使用case [value]:

switch($args) {
    case ($args =='arg1'):
        .
        .
        .
        break;
    .
    .
    .
}

应该是

switch($args) {
    case 'arg1':
        .
        .
        .
        break;
    .
    .
    .
}