I would like to translate the following command, which works in linux Ubuntu and CentOS, to work in MacOS:

echo $(pgrep -c -P$$)

This is designed to be called from within a python script:

subprocess.check_output("echo $(pgrep -c -P$$)", shell=True)

and run new jobs within a loop when the number of processes falls below a threshold.

The issue is that the MacOS version of pgrep does not have a -c option.

Many thanks for your help!

  • 1
    Seems a bit of an XY problem. With subprocess, you could probably keep track of children directly or use ps If you really want to let any other process that’s sililar affect the loop. Without context of the logic on forking children, it’s hard to offer some alternatives.
    – bmike
    Nov 17, 2017 at 20:45

1 Answer 1


If you're looking for the count of child processes, you can use pgrep -P <pid> | wc -l since the output is on PID per line and wc -l outputs the line count.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .