3

Going by the man page, I would expect this to work:

ps ah -o pid,pgrp -G 18322

But this shows exactly the same list as without the -G arguments. I want a saner way to produce this output:

ps ah -o pid,pgrp | perl -e 'while(<STDIN>){ my @ws = split " ", $_; if ($ws[1] eq $ARGV[0]) { print $ws[0]."\n" } }' 18322

(Thanks to mst on #perl for the perl-fu)

Here's a more traditional command line version, (thanks againt to mst), but still a bit awkward. $process_group needs to be set beforehand:

ps ah -o pgrp,pid | egrep '^'$process_group' ' | awk '{print $2}'

1 Answer 1

5

Use pgrep instead:

pgrep -g 18322

From man pgrep:

   -g, --pgroup pgrp,...
          Only  match  processes in the process group IDs listed.  Process
          group 0 is translated into pgrep's or pkill's own process group.

Alternatively, you can just parse the ps output in simpler ways:

ps xh -o pgrp,pid | awk '$1==18322{print $2}'

Or just simplify your (needlessly complex) original Perl approach:

ps xh -o pgrp,pid | perl -lane 'print $F[1] if $F[0] eq 5592'

Or just grep:

ps xh -o pgrp,pid | grep -Po '\s*5592\s*\K.+'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.