Parallel tasks in Linux with limited number of processes

In fact, the authorship is not mine (link to the full article). I will save it as a ready snippet:

#!/bin/bash

RANDOM=10
JOBS_COUNTER=0
MAX_CHILDREN=10
MY_PID=$$

for i in {1..100}
do
    echo Cycle counter: $i
    JOBS_COUNTER=$((`ps ax -Ao ppid | grep $MY_PID | wc -l`))
    while [ $JOBS_COUNTER -ge $MAX_CHILDREN ]
    do
        JOBS_COUNTER=$((`ps ax -Ao ppid | grep $MY_PID | wc -l`))
        echo Jobs counter: $JOBS_COUNTER
        sleep 1
    done
    sleep $(($RANDOM % 30)) &
done
echo Finishing children ...

where MY_PID=$$ is getting the PID of your own process. If we run child scripts or if we need to control another independent process, the line for calculating the number of processes can be changed as follows:

MY_PID="cyber-bot-loader.sh"
...
JOBS_COUNTER=$((`ps -ela | grep $MY_PID | wc -l`)) # ищем не дескриптору, а по имени процесса

About: Morozov Dmitry

My specialisation is software engineer. I am 35 years old and have been working in the IT field for over 15 years. I have accumulated experience in programming, project management, training and administration.