Finding number of child processes of a particular process
ps -eo ppid= | grep -Fwc $pid
If your grep
does not support -w
:
ps -eo ppid= | tr -d '[:blank:]' | grep -Fxc $pid
or
ps -eo ppid= | awk '$1==ppid {++i} END {print i+0}' ppid=$pid
or (clobbering the positional parameters)
set $(ps -eo ppid=); echo $#
Note that this is not atomic, so the count may be wrong if some processes die and others get spawned in the short span of time it takes to gather the data.
Try ps --ppid ${pid} --no-headers | wc --lines
.