APM: RE: Killing spawned processes

Bakken, Tom - Temple, TX tom.bakken at tx.usda.gov
Mon Jul 26 10:25:09 CDT 2004


I tried Bill's spawn subroutine on a system that supports fork.  And it
does allow the uptime command to run for at most 20 seconds as
advertised.  If uptime succeeds it then reads the results of uptime and
processes it as intended.  However, if uptime does run for 20 seconds,
the kill program terminates my perl script rather than the child
process.

Bill, I may have misinterpreted your instructions, but I think I'm
stumbling close to what I want

What am I doing wrong?

SNIP----
sub spawn (&) {
  my ($CodeRef) = @_;
  my $pid;
  unless ($pid = fork) {
    $CodeRef->();		# Start the child process.  Make sure it
exits too!
    exit;
  }
  $SIG{CHLD} = "IGNORE";	# Make sure we don't have any ghost
processes.  Have perl do the waitpid
  return $pid;
}
sub GetWorkstationUptime {	# Copy files to server using a list of
machine names 
  my $ListOfWorkstationsArrayRef = shift;
  my $Count = 0;
  foreach (sort @{$ListOfWorkstationsArrayRef}) {
    my ($MachineName, $Description, $DnsHostName,
           $OS, $WhenChanged, $WhenCreated) = split /\|/, $_;
####    next unless uc($MachineName) =~ /TXTEMPLE/;
    next unless uc($MachineName) =~ /TXBELLVILLD/;
    $Count++;
    unless ($MachineName =~ /$StateAbbrev/i) {
      print "$Count ALERT!!! No MachineName for this Active Directory
record.\n";
      print LOGFILE "$Count     ALERT!!! No MachineName for this Active
Directory record.\n";
      print DATAFILE
"$Count\t--------------\t-------------------\t---\t---\t---\tNo Machine
Name\n";
      next;
    }
    print "$Count: Checking last reboot on: $MachineName\n";
    my $ChildPid = spawn {
      print LOGFILE "$Count: Checking last reboot on: $MachineName\n";
      $Status = system "uptime \\\\$MachineName > /nul";
      unless ($Status == 0) {
        $Error++;
        if($OS =~ /Windows XP Professional/) {
          print "ALERT!!! Could not connect to $MachineName.
$Description Error: $?\n";
          print LOGFILE "     ALERT!!! Could not connect to
$MachineName. $Description Error: $?\n";
        }
        else {
          print "ALERT!!! Could not connect to $MachineName.
$Description Error: $?\n";
          print LOGFILE "     ALERT!!! Could not connect to
$MachineName. $Description Error: $?\n";
          print "ALERT!!! OS Not Windows XP Professional.  Detected:
$OS\n";
          print LOGFILE "     ALERT!!! OS Not Windows XP Professional.
Detected: $OS\n";
        }
        print DATAFILE
"$Count\t$MachineName\t-------------------\t---\t---\t---\tCould not
connect\n";
        next;
      }
    };				# Don't forget the semicolon here!!!!
    sleep 20;			# Give uptime 20 seconds to run
    kill TERM => $ChildPid;	# Kill after that amount of time
    my $Results =  `uptime \\\\$MachineName`;
    my ($Days, $Hours, $Minutes, $Seconds) = split /,/, $Results;
Blah blah
.
.
.
.
}				# End GetWorkstationUptime subroutine



Tom Bakken
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Tom.Bakken at tx.usda.gov.vcf
Type: text/x-vcard
Size: 460 bytes
Desc: Tom.Bakken at tx.usda.gov.vcf
Url : http://mail.pm.org/pipermail/austin/attachments/20040726/a1b6e8a6/Tom.Bakkentx.usda.gov.vcf


More information about the Austin mailing list