[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1240
  • Last Modified:

Using perl to fork a process and terminate it if over X second

Here is my problem, often that when I am using tnsping to test a TNS Alias, the command could take minutes to return if there is a network disruption.

Can I wrap the tnsping command a perl command using fork and alarm feature, so that if the child process is ubale to complete with X second, this perl command will return 1 otherwise return 0

let's say tns alias: SID_HOSTX

$ tnsping SID_HOSTX                  #could take minutes to return not ok
0
tindavid
Asked:
tindavid
  • 7
  • 4
  • 3
  • +1
1 Solution
 
gheistCommented:
You need to make sufe both ends are aware of own and other dns name and reverse name
0
 
rfportillaCommented:
I don't know tnsping, but I don't see why you wouldn't be able to.  Do you need an example?  

Try this:

http://perldoc.perl.org/functions/alarm.html

In the eval block, I would use backticks to execute tnsping and parse the return.  You can get finer error handling that way.  

This is synchronous, however.  If you wanted to this asynchronously (with forking), it will be a little more complicated.  Let me know if this is a requirement and I can do a quick search for an example.  (or just google.  The truth is out there...)
0
 
tindavidAuthor Commented:
Yes, I needed a wrapped script to execute a Unix command will be find:

pseudo code:

tnsping is an Oracle command to test the DB connection listener.

$ tnsping abcd

where abcd is athe alias to make a ping to the port of Listener in the remote server and that port is conifgured with Oracle Listener.

I want to make sure the 'tnsping abcd' will not hanging there foe minutes and return not successful.


So, please send me the complete command syntax to do this, I presume the code will like this

Synchronize solution will be fine, I only want to know if this tnsping can return asap, so that I can make a decision .

# test tnsping connection by using perl's alarm, fork , retrun code if executed with a limited time slot (say 2 seconds)

perl -fork ......                

if  [ $? -ne 0 ]; then  ......
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
tindavidAuthor Commented:
Dear All,

I have managed to get an easy answer as below:

my $pid = fork;
if ($pid > 0){
    eval{
        local $SIG{ALRM} = sub {kill -9, $pid; die "TIMEOUT!"};
        alarm 10;
        waitpid($pid, 0);
        alarm 0;
    };
}
elsif ($pid == 0){
    setpgrp(0,0);
    #exec(q[sleep 2]);
    exec(q[sleep 20]);
    exit(1);
}
0
 
tindavidAuthor Commented:
hi rfportilla,

let's say the above perl code is now named as TestSleep.pl with arg1 as command

#!/usr/bin/ksh

cmd1='sleep 20'
res=`perl TestSleep.pl ${cmd1} `
if [ $res -ne 0 ]; then
   echo "$cmd1 is killed due to over the allowable time of 10 sec"
fi

however, the return code of the TestSleep.pl 'sleep 20' does not return 1.

so in my shell script I need to get the return code of excution of perl script result code

Could you help to finish this part ?

Thanks
0
 
tindavidAuthor Commented:
I need to get the status code if 9 if child process is killed and 0 if child process gets executed before alam wake up.
0
 
arnoldCommented:
Add to your res= assignment
; echo $?

In shell, bash $? Is the status code and will be the value assigned to res.
0
 
rfportillaCommented:
I'm not completely clear on that. Are you saying that you need the status code, the child process, or both? As Arnold said, status code is easy. $?
0
 
tindavidAuthor Commented:
Yes, I need to status code of the command executed by TestSleep.pl (child process).

Togather with the script (TestSleep.pl), there should be another argument.


TestSleep.pl 5 "sleep 20" or TestSleep.pl 5 "Unix command"

Where the 5 threshold of how long the parent prcess to wait for the child process to complete.  If the threshold is over, the script should terminate the child process and return return code of child prcess.

You may ask why "sleep 20"?   So that the child prcess is always longer than the threshold of 5.

Instead of executing "sleep 20", I can execute a command of "ping xxx.xxx.xxx.x" to see if thi ping might runing over minutes to return. So if the commnad is this ping command , the return code should not be 0, correct ?

Thank you.
0
 
arnoldCommented:
when you run an assignment executing an external command, the status assigned will be that of ``
if you want, the output of the command, you need to echo $?
status=`run_command; echo $?`
the variable $statis will have the exit code from run_command.

You are testing the alarm functionality at this point which should be working when you only execute the perl script directly.
0
 
rfportillaCommented:
You don't need to echo $? within the command.  PERL will populate the $? with the return of the last shell command run in ``.  

I still did not get a clear answer.  Why are you running this as child processes?  Is this a requirement?  I'm only asking because it makes the task more complicated and unless you have a bunch of processes to check (>20), it is much easier to handle this synchronously.
0
 
arnoldCommented:
Here is an explanation.
within the shell script the only thing that will be assigned to res is the output from the perl script. STDERR will not be part of the data which is what the TIMEOUT output is when the alarm is triggered.
The status of the execution of the code will be available in the $? system/environment variable. until another command is executed.

i.e. if all works right, res will have TIMEOUT when your assignment redirects 2>&1.
and $? will have the exit code
0
 
tindavidAuthor Commented:
Ok, I have modified my code to get an exit code.

#!/usr/bin/perl

$tme = $ARGV[0];
$cmd = $ARGV[1];

my $retval = 0;
my $pid = fork;

if ($pid > 0){
    eval{
        local $SIG{ALRM} = sub {kill -9, $pid; $retval = 99;};
        alarm $tme;
        waitpid($pid, 0);
        exit($retval);
    };
}
elsif ($pid == 0){
    setpgrp(0,0);
    exec($cmd);
    exit($retval);
}

$  perl TestCMD.pl 5 "sleep 20"
$ echo $?
99

$  perl TestCMD.pl 5 "sleep 2"
$ echo $?
0
0
 
tindavidAuthor Commented:
Noone seems give the solution that I want. I found the solution myself.
0
 
rfportillaCommented:
I'm not quite sure what solution you were looking for then.  I thought we gave you information relevant to solving your problem.  Maybe I'm missing something.  You wanted to to get the return for a script that you executed within PERL, right?  

Anyway, sorry we couldn't be more help.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 7
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now