MacOS – ‘pgrep -c’ in MacOS terminal

bashmacospythonunix

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!

Best Answer

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.