我正在尝试使用以下perl驱动程序执行一个使用参数的PHP脚本:

#!/opt/local/bin/perl
use strict;
use warnings;
use Getopt::Long;
use Cwd;

my %args = ();

GetOptions(
            \%args,           "NUM_AGENTS|a=s",
            "HOST_NAME|h=s",  "TIME_STAGGER|t=s",
            "USER_NAME|un=s", "USER_PASS|pw=s",
            "TARGET_PAGE|p=s"
) or die "Unknown parameter!\n";

my $i         = 0;
my $startTime = time;

my $pwd = getcwd();

my $logdir = "$pwd/load-logs";

mkdir $logdir
  or die "Cannot mkdir $logdir: $!"
  unless -d $logdir;
chmod 0755, $logdir or die "Cannot chmod 0755 $logdir: $!";

my $startTimeTemp = $args{NUM_AGENTS} + $startTime;
my $startTime2    = $startTimeTemp + 10;

mkdir( "$logdir/$startTime2", 0777 )
  or die "Cannot mkdir $logdir/$startTime2: $!"
  unless -d "$logdir/$startTime2";

my $random_number = rand() * 10;
my $execDelay =
  ( $random_number % $args{TIME_STAGGER} ) * ( ( $random_number % 100 ) );
my $procStartTime = $startTime2 + $execDelay;

my $reqlogfile  = "$logdir/$startTime2/req.log";
my $resplogfile = "$logdir/$startTime2/resp.log";

print "NUM_AGENTS: " . "$args{NUM_AGENTS}\n";
print "HOST_NAME: " . "$args{HOST_NAME}\n";
print "procStartTime: " . "$procStartTime\n";
print "i: " . "$i\n";
print "TARGET_PAGE: " . "$args{TARGET_PAGE}\n";
print "resplogfile: " . "$resplogfile\n";
print "USER_NAME: " . "$args{USER_NAME}\n";
print "USER_PASS: " . "$args{USER_PASS}\n";
print "execDelay: " . "$execDelay\n";
print "COMMON_SID: " . "$args{COMMON_SID}\n";

while ( $args{NUM_AGENTS} > $i ) {
    $i++;
    print "count: " . "$i\n";

    my $argString =
"'$args{NUM_AGENTS}' '$args{HOST_NAME}' '$procStartTime' '$i' '$args{TARGET_PAGE}' 'resplogfile' '$reqlogfile' '$args{USER_NAME}' '$args{USER_PASS}' '$execDelay' '$args{COMMON_SID}'";

    system("php loadAgent_curl.php $argString") == 0 or die "failed to execute: $!";

    sleep 1;

    #system("ls");
}

但是看来,有些问题:

system("php loadAgent_curl.php $argString") 

由于LS系统命令运行良好,但是带有参数的PHP命令没有

该perl脚本的命令行参数可以是:

-a 10 -h ktest.test.net -t 5 -un Admin -PW AdminPassword -p“ acviewer/index.html?startDate = 20090926040000&endDate&endDate = 20111220235959”

有帮助吗?

解决方案

您没有提及您收到的错误消息类型。它像 Cannot find "php" 或者是其他东西。

您可能会遇到报价问题。这里有一些建议:

  • 利用 qq() 而不是引号。那会使事情变得更干净。
  • 将命令构建为一个称为的变量 $command 如果您有错误,则可以打印出来。这可能会帮助您了解可能出现问题的地方。
  • 设置一个称为的变量 $error 执行时 system, ,然后检查该变量。它比向后的风格清晰得多 and/or 您必须在失败和成功方面做的事情,并且要容易得多。

例子:

my $argString = qq("$args{NUM_AGENTS}" "$args{HOST_NAME}" ) 
    . qq( "$procStartTime" "$i" "$args{TARGET_PAGE}" "resplogfile" )
    . qq("$reqlogfile" "$args{USER_NAME}" "$args{USER_PASS}" )
    . qq("$execDelay" "$args{COMMON_SID}");

my $command = qq(php loadAgent_curl.php $argString);

my $error = system qq($command);
if ($error) {
    die qq(ERROR: Failed to execute "$command"\n\n$!);
}

至少这样,您可以看到哪些命令失败,并有一个更好的了解,为什么它可能不会执行。

其他提示

与大多数其他perl命令不同, system 在“成功”和“失败”上返回0。所以典型的成语是

system $command and die ...

代替

system $command or die ...

更新: :OP确实正确地完成了这一部分 - system(...)==0 or die ... 这也是一个完全不错的方法来检查错误 system 命令。


您传递给的确切命令中也可能有一些时髦的引用 system 命令。对于这样的任务,通常最好绕过外壳并使用列表形式 system 直接将命令传递到操作系统。也许类似:

my @argList = ($args{NUM_AGENTS}, $args{HOST_NAME}, $procStartTime, $i,
               $args{TARGET_PAGE}, 'resplogfile', $reqlogfile, $args{USER_NAME},
               $execDelay, $args{COMMON_SID});
system("php", "loadAgent_curl.php", @argList) and die "failed to execute: $!";

(也确保 php 在你的 $PATH 或指定完整的路径 php] 和 loadAgent_curl.php 在当前目录中)。

尝试PHP -GetOptionKit:

http://c9s.blogspot.com/2011/11/php-getoptionkit.html

概要

use GetOptionKit\GetOptionKit;

$getopt = new GetOptionKit;
$spec = $getopt->add( 'f|foo:' , 'option require value' );  # returns spec object.

$getopt->add( 'b|bar+' , 'option with multiple value' );
$getopt->add( 'z|zoo?' , 'option with optional value' );

$getopt->add( 'f|foo:=i' , 'option require value, with integer type' );
$getopt->add( 'f|foo:=s' , 'option require value, with string type' );
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top