Link to home
Start Free TrialLog in
Avatar of tindavid
tindavid

asked on

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
Avatar of gheist
gheist
Flag of Belgium image

You need to make sufe both ends are aware of own and other dns name and reverse name
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...)
Avatar of tindavid
tindavid

ASKER

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  ......
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);
}
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
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.
Avatar of arnold
Add to your res= assignment
; echo $?

In shell, bash $? Is the status code and will be the value assigned to res.
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. $?
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.
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.
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.
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
ASKER CERTIFIED SOLUTION
Avatar of tindavid
tindavid

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Noone seems give the solution that I want. I found the solution myself.
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.