
On Thu, Jun 27, 2013 at 04:34:33PM +1000, Chris Samuel wrote:
Is there a way to easily collect all child (and grandchild, etc) processes of a particular process?
I know I can do ps --ppid $PID to get the immediate children, and I can use pstree -p $PID to see all the descendants in graphical form, but I want to get an easy to parse list.
the simplest way I can think of is to use pstree's '-A' option to get ASCII rather than VT100 line-drawing characters and then pipe the output into sed to transform non-numeric characters into spaces. optionally pipe into xargs to get all the PIDs on one line and get rid of any extra spaces. e.g. $ pstree -A -p 13648 | sed -e 's/[^0-9]\+/ /g' | xargs 13648 14378 14379 14380 14381 14382 14383 14384 14385 14386 14387 31743 31744 31749 14388 14389 14390 14391 14392 14393 14394 14395 14396 14397 14398 14399 14400 14401 16089 22087 605 606 607 you now have a list of PIDs - you can use 'ps -p' or lsof or whatever to get whatever further information you need about them. BTW, PID 13648 on my system at the moment is a bash shell with lots of sub-processes (mostly several backgrounded mutt processes, including the mutt and vim processes I am writing this reply with) WARNING: this simple sed script will work in most cases but is overly simplistic - if the process name contains any digits, they will be printed too (e.g. for "gdm3", "3" would be printed). a better solution would extract only the pid numbers within the parentheses from pstree's output. hmmm...something as simple as deleting any digits immediately preceded by a non-digit-or-open-parenthesis would probably do the trick. something like: sed -e 's/[^()0-9][0-9]\+//' $ pstree -A -p 13648 | sed -e 's/[^(0-9][0-9]\+//' -e 's/[^0-9]\+/ /g' | xargs However, IFF you are certain that the process names of all child processes contain no digits, then it's safe to use the simple version. craig -- craig sanders <cas@taz.net.au> BOFH excuse #2: solar flares