tags:

views:

59

answers:

2

I am seeing an application always remains live even after closing the application using my Perl script below. Also, for the subsequent runs, it always says that "The process cannot access the file because it is being used by another process. iperf.exe -u -s -p 5001 successful. Output was:"

So every time I have to change the file name $file used in script or I have to kill the iperf.exe process in the Task Manager.

Could anybody please let me know the way to get rid of it?

Here is the code I am using ...

my @command_output;
eval { 
    my $file = "abc6.txt";    
    $command = "iperf.exe -u -s -p 5001";
    alarm 10;
    system("$command > $file");
    alarm 0;
close $file;
};
if ($@) {
    warn "$command timed out.\n";
} else {
   print "$command successful. Output was:\n", $file;
}
unlink $file;
A: 

Since your process didn't open $file, the close $file achieves nothing.

If the process completed in time, you would not have the problem. Therefore, you need to review why you think iperf can do its job in 10 seconds and why it thinks it can't.

Further, if the timeout occurs, you should probably aim to terminate the child process. On Unix, you might send it SIGTERM, SIGHUP and SIGKILL signals in sequence, with a short pause (1 second each, perhaps) between. The first two are polite requests to get the hell out of Dodge City; the last is the ultimate death threat. Of course, you have to know which process to send the signal too - that may be trickier to determine with 'system' and Windows than on Unix.

Jonathan Leffler
A: 

In fact I am able to kill that application process using the system() command in windows BUT...............

For each subsequent RUNS I have to run the below script twice to kill that process as in first run it doing my purpose

First Run:

Terminating on signal SIGALRM(14) --> My Purpose

but in the second run it is killing the application in Task manager giving me a message

Second Run:

iperf.exe -u -s -p 5001 successful. Output was:
SUCCESS: The process "iperf.exe" with PID 4652 has been terminated.

Is there any other way around to do it in one step, might be something wrong (misplaced the system() command)??

my @command_output;
eval {
my $file = "abc7.txt";
$command = "iperf.exe -u -s -p 5001";
alarm 10;
system("$command > $file");
alarm 0;
#close $file;
};
if ($@) {
warn "$command timed out.\n";
} else {
print "$command successful. Output was:\n", $file;
system("taskkill /F /IM iperf.exe");
}
#unlink $file;

/rocky...

You need to do the 'taskkill' if the command timed out. [Also, please use the 'code' icon (010101 above the edit box) to indent code - or add four spaces to the front of each line.]
Jonathan Leffler