#!/usr/bin/perl -w #use Data::Dumper; use strict; my %order; set_order(); my @process_list; get_processes(); my $parent = shift || parent_of($$); print "Children of PID $parent:\n"; my @children = grep { $_->[$order{PPID}] == $parent and $_->[$order{PI +D}] != $$ } @process_list; foreach my $c (@children) { print $c->[$order{PID}], " : ", $c->[$order{CMD}], "\n"; } sub parent_of { my $pid = shift || return -1; for (my $i = 0; $i <= $#process_list; ++$i) { return $process_list[$i][$order{PPID}] if ($process_list[$i][$order{PID}] == $pid); } return -1; } sub process_info { my $pid = shift || return -1; for (my $i = 0; $i <= $#process_list; ++$i) { return $process_list[$i] if ($process_list[$i][$order{PID}] == + $pid); } return -1; } sub get_processes { local *FH; my $opts = "-ef"; if ( $^O eq "linux" ) { $opts .= " --cols 1024"; } open (FH, "ps $opts |") or die "Can't get process list"; my $discard = <FH>; while (<FH>) { chomp; s/^\s+//; my @line = split /\s+/, $_, scalar keys %order; push @process_list, \@line; } close(FH); } sub set_order { if ( $^O eq "aix" or $^O eq "irix" or $^O eq "hpux" ) { %order = ( UID => 0, PID => 1, PPID => 2, C => 3, STIMEM => 4, STIMED => 5, TTY => 6, TIME => 7, CMD => 8, ); } elsif ( $^O eq "dynixptx" or $^O eq "linux" ) { %order = ( UID => 0, PID => 1, PPID => 2, C => 3, STIME => 4, TTY => 5, TIME => 6, CMD => 7, ); } else { die "Unrecognised platform"; } }

In reply to Find children of process (unix/linux) by Tanktalus

Title:
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, details, 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, summary, 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.