#!/usr/bin/env perl
# Program: HP-UX-11iv3-ptree-kill.pl.txt
#
# Description: Cleanup of process tree on HP-UX servers
#
# If you obtain this script via Web, convert it to Unix format. For example:
# dos2ux HP-UX-ptree-kill.pl.txt > HP-UX-ptree-kill.pl
#
# Usage: HP-UX-ptree-kill.pl -h | -p PID
#
# -h Print help message
# -p PID Kill process tree that starts with PID
#
# Last Update: 21 April 2013
# Designed by: Dusan U. Baljevic (dusan.baljevic@ieee.org)
# Coded by: Dusan U. Baljevic (dusan.baljevic@ieee.org)
#
# Copyright 2013-2015 Dusan Baljevic
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .
# Define important environment variables
#
$ENV{'PATH'} = "/usr/bin:/usr/sbin:/sbin:/opt/perl/bin";
# Define Shell
#
$ENV{'SHELL'} = '/usr/bin/sh' if $ENV{'SHELL'} ne '';
$ENV{'IFS'} = '' if $ENV{'IFS'} ne '';
# Enforce strictness
#
if ( eval "require strict" ) {
import strict;
use strict;
no strict 'refs';
} else {
print "WARN: Perl strict not found\n";
}
sub Usage {
if ( eval "require File::Basename" ) {
import File::Basename;
$CMD = basename( "$0", ".pl" );
Prusage();
} else {
$CMD = `basename $0`;
chomp($CMD);
Prusage();
}
}
# Display usage if "-h" option is used
#
sub Prusage {
print <&1`;
( $System, $Hostname, $Maj, undef, $Hardware, undef ) =
split( /\s+/, $VH );
$Version = $Maj;
( $Major, $Minor, $Patch ) = split( /\./, $Maj );
}
if ( "$Minor$Patch" < 1131 ) {
print "ERROR: This script should be run on HP-UX 11iv3 or higher\n";
exit(1);
}
# Do not allow to run as unprivileged user
#
if ( $> != 0 ) {
print "ERROR: This script should be run with root privileges\n";
exit(1);
}
# Get the process tree from MYPPID
#
my @pstree = ();
if ( "$MYPPID" ) {
@pstree = `ptree $MYPPID`;
} else {
die "ERROR: PID not specified\n";
}
if ( ! "@pstree" ) {
die "ERROR: No process tree found for PID $MYPPID\n";
exit(1);
}
foreach my $i ( @pstree ) {
# Get rid of whitespaces at the beginning of line
#
$i =~ s/^\s+//g;
# The first column is the PID of the current line
#
($mypid, undef) = split( /\s+/, $i );
# Add the PID to the array in an orderly manner
# to preserve the process hierarchy
#
push(@array, $mypid);
}
# Now, reverse the array elements
#
my @rev = reverse(@array);
foreach my $proc (@rev) {
# Check if the process is still alive
# by sending it signal 0
#
if (kill 0 => $proc) {
print "INFO: Process $proc is alive... Attempting to kill it\n";
# Now, try to kill the process
#
print "kill(\"TERM\", $proc)\n";
# kill("TERM", $proc);
} elsif ($! == EPERM) { # changed uid
print "WARNING: Process $proc has escaped the control\n";
} elsif ($! == ESRCH) {
print "WARNING: Process $proc is deceased\n"; # or zombied
} else {
warn "WARNING: Could not check the status of process $proc: $!\n";
}
}
# Clean exit
#
exit(0);