Using Getopt::Long to drive a system command on linux
I am trying to execute a php script with arguments, using the following perl driver:
#!/opt/local/bin/perl
use strict;
use warnings;
开发者_C百科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");
}
but it seems, that something is wrong with :
system("php loadAgent_curl.php $argString")
since the ls system commands runs fine, but the php command with arguments does not
The Command Line arguments ot this perl script can be:
-a 10 -h ktest.test.net -t 5 -un admin -pw adminpassword -p "acViewer/index.html?StartDate=20090926040000&EndDate=20111220235959"
Unlike most other Perl commands, system
returns 0 on "success" and non-zero on "failure". So the typical idiom is
system $command and die ...
instead of
system $command or die ...
Update: the OP did get this part right -- system(...)==0 or die ...
is also a perfectly fine way to do error checking on the system
command.
There also might be some funky quoting in the exact command you are passing to the system
command. For a task like this, it is often best to bypass the shell and use the LIST form of system
to pass the command directly to the OS. Maybe something like:
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: $!";
(and also make sure php
is in your $PATH
[or specify the complete path to php
] and loadAgent_curl.php
is in the current directory).
You didn't mention the type of error message you're getting. Was it something like Cannot find "php"
or something else.
You might be having trouble with quotes. Here's a few recommendations:
- Use
qq()
instead of quotation marks. That'll make things a bit cleaner. - Build the command into a variable called
$command
which you can print out in case you have errors. That might help you understand where you maybe having a problem. - Set a variable called
$error
when executingsystem
, and then check that variable. It's a lot clearer than the backwards style ofand/or
stuff that you have to do on failure and success, and it's a lot easier to maintain.
Example:
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$!);
}
At least this way, you can see what command failed, and get a better idea why it might not have executed.
Try PHP - GetOptionKit:
http://c9s.blogspot.com/2011/11/php-getoptionkit.html
synopsis
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' );
精彩评论