Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I am running some automated tests at night and I want to catch it when a client hangs. To do this I have a forked process in a much bigger script. The forked part basically sleeps, and then goes into an infinite loop to check for hung clients, the problem is I cant stop this from killing the parent process.
A bit of explanation about the code. I am not killing the hung clients with kill but rather canceling them through a software app.
The steps I have taken so far are to save the parent pid and compare it to the PID being read in. If they match I donít stuff it into the list of things to be killed. Below is the offending code
### BEGINING OF FORK # my $kidpid; my $parentpid = $$; if (!defined($kidpid = fork())) { # fork returned undef, so failed print "cannot fork: $!"; } elsif ($kidpid == 0) { sleep(3600); my ($date, $Stime, $Ctime, $J, $Temp1, $Temp2, $bpdbjobs); my (@JOBID, @PIDS, @times, @bpdbLines); while(1) { ### parsing the input $bpdbjobs = `$BPDBJOBS | grep Active | grep -v PID`; @bpdbLines = split /\n/, $bpdbjobs; for($J = 0; $J < @bpdbLines; $J++) { $bpdbLines[$J] = $bpdbLines[$J] . "\n"; $bpdbLines[$J] =~ /^(\d+)\s/; $JOBID[$J] = $1; $bpdbLines[$J] =~ /\s(\d+)$/; $PIDS[$J] = $1; if( $PID[$J] == $parentpid) { $PIDS[$J]=undef; $JOBID[$J]=undef; $J--; } } for( $J = 0; $J < @PIDS ; $J++) { #get the time the jo +b started chomp ($times[$J] = `/bp/bin/bpps | grep $PIDS +[$J] | /usr/local/bin/perl -lane 'print \$F[4]'`); $times[$j]=parsedate($times[$j]); } ### time checks $Ctime = time; for($J = 0; $J < @PIDS; $J++) { $Temp1 = $Ctime - $times[$j]; if($Temp1 > 7200) { $Temp2 = "/usr/openv/netbackup/bin/adm +incmd/bpdbjobs -cancel $JOBID[$J]"; #cancel if job runs longer than 2 hours print "$Temp2\n"; system($Temp2); unless (open (todayHungClientsLogP, "> +>$HungClientsLog")) { print (" HungClients: unable t +o open output <$HungClientsLog>\n"); } else { # print (todayHungClientsLogP +" Running Time = $Temp1 >>"); print (todayHungClientsLogP "$ +Temp2\n"); close (todayHungClientsLogP); } } } sleep(1800); $alive = `/bin/ps -ef | grep $parentpid | grep -v grep +`; unless($alive ne "") { exit; } } } else { #do the rest of the script here.

I have tested my basic idea to make sure it works with this script.
#!/usr/local/bin/perl -w use strict; my $parentpid = $$; my $kidpid; print "This is the original process the pid = $$\n"; if (!defined ($kidpid = fork())) { #fork failed. print "Can't fork: $!"; } elsif ($kidpid ==0) { print "\tThis is the child process.. parentpid = $parentpid\n" +; print "\tThis is the child process its pid = $$\n"; } else { sleep(1); print "This is the return to the parent process\n"; }

and this is the output I get from that.
This is the original process the pid = 5292 This is the child process.. parentpid = 5292 This is the child process its pid = 5293 This is the return to the parent process

Any help you have to offer would be great.

In reply to I am having a fork problem by krujos

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (5)
    As of 2018-01-21 13:48 GMT
    Find Nodes?
      Voting Booth?
      How did you see in the new year?

      Results (228 votes). Check out past polls.