Question

LINUX! In shell scripting if possible and show commands (please do not copy codes form others)...

LINUX! In shell scripting if possible and show commands (please do not copy codes form others) Thank you!

Write a simple test program to find out PIDs of ALL ancestor (i.e., parent, grandparent ) processes of the current process or a given process specified by a PID. A parent process's PID of a given process pid can be found in /proc/$pid/status

0 0
Add a comment Improve this question Transcribed image text
Answer #1

pid is given as an argument, second is the pid of its parent, grandparent and so on.

use strict;
use warnings;
  
use Getoption::Long;
use Pod::Usage;
  
sub _usage_and_exit {
my %option = @_;
pod2usage(
-noperldoc => 1,
-verbose => 99,
-exitval => defined $option{help} && $option{help} ? 0 : 1,
-output => defined $option{help} && $option{help} ? \*STDOUT : \*STDERR,   
);
}
  
my %option;   
my $sw = Getoptionions( \%option, 'help|h', );   
my $pid = shift;

_usage_and_exit( help => $option{help} )
if not $sw   
or defined $option{help}
or not defined $pid
or $pid !~ /^\d+$/
or $pid < 1
or @ARGV;
  
while ( $pid > 0 ) {
open my $stat_fh, "<", "/proc/$pid/stat"
or die "no such pid: $pid\n";   
my $ppid = ( split /\s+/, <$stat_fh> )[3];
close $stat_fh;
print $pid, "\n";
$pid = $ppid;
}

This scripts non-zero value if it unable to find pid given or the process is in chain.

Add a comment
Know the answer?
Add Answer to:
LINUX! In shell scripting if possible and show commands (please do not copy codes form others)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT