#!/usr/bin/perl #-----------------------------------------------------------------------------# # # # PROGRAM: ancestors.pl # # # # PURPOSE: Track the ancestry/lineage of a given PID. # # Useful for understandings the parent/grandparent processes # # of a given PID, which can help in understanding and # # debugging process problems. # # # # NOTES: This program was written for Sun's Solaris operating system, # # and may not properly read the 'ps -ef' output for other # # versions of Unix. # # # #-----------------------------------------------------------------------------# # COPYRIGHT: # # # # This example is from Developer's Daily (http://www.DevDaily.com). # # Copyright (c) 1998 DevDaily Interactive, Inc. # # This example is provided WITHOUT ANY WARRANTY either expressed or implied. # # You may study, use, modify, and distribute it for non-commercial purposes. # # For any commercial use, contact our editor (editor@DevDaily.com). # #-----------------------------------------------------------------------------# #---------------# # getPsefData # #---------------# sub getPsefData { open(PSEF_PIPE,"ps -ef|"); $i=0; while () { chomp; @psefField = split(' ', $_, 8); $pid[$i] = $psefField[1]; $uid{$pid[$i]} = $psefField[0]; $ppid{$pid[$i]} = $psefField[2]; ($min,$sec) = split(/:/,$psefField[6]); $time{$pid[$i]} = $min * 60 + $sec; $ucmd{$pid[$i]} = $psefField[7]; $i++; } close(PSEF_PIPE); } #------------------------- Main ---------------------------# #------------------------------------------------------------# # Step 1: Get the ps -ef data, and put it in the desired # # arrays and hashes. # #------------------------------------------------------------# &getPsefData; #--------------------------------------------------# # Step 2: Prompt the user for the PID to trace. # # (It's assumed that the user knows # # what PID they want to trace, so they # # are not prompted with a list of PIDs. # #--------------------------------------------------# print "\n\nSpecify the PID whose lineage should be traced: "; chop($pid=); printf("\n\n\t\t%5d \t%s\n", $pid, $ucmd{$pid}); #---------------------------------------------------# # Step 3: Print the lineage of the desired PID. # # Reset $pid each time through the loop. # #---------------------------------------------------# $i=0; while ($pid != 0) { $parent[$i] = $ppid{$pid}; printf("\t\t%5d \t%s\n", $parent[$i], $ucmd{$pid}); $pid = $parent[$i]; $i++; } print "\n";