I have a Python script that spawns a few subprocesses but never more than n at a time.
I want to write a shell script to confirm that it has no more than n subprocesses at any given time, but also that generally it has n processes running.
If I have the PID of the Python program inside a shell script, how do I check the number of subprocesses that that PID currently has? E.g.
python script.py &
pid=$!
while true
do
    # do something that prints number of subprocesses of
    # the process $pid to stdout
    sleep 1
done



/proc/$$/task/$$/childrento get first level childrenSince this interface is intended to be fast and cheap it doesn't guarantee to provide precise results and some children might be skipped, especially if they've exited right after we printed their pids, so **one need to either stop or freeze processes being inspected if precise results are needed**.