Skip to content

Instantly share code, notes, and snippets.

@WeileiZeng
Last active July 12, 2023 01:36
Show Gist options
  • Save WeileiZeng/5ad42d32efeb625cd58996ba5ae0c7b0 to your computer and use it in GitHub Desktop.
Save WeileiZeng/5ad42d32efeb625cd58996ba5ae0c7b0 to your computer and use it in GitHub Desktop.
# control numble of processes running in parallel
(( num_cores = 64 ))
(( max_process = num_cores - 16 )) #max number of process allowed in parallel
(( total_process = 64 ))
(( started_process = 0 ))
#where job_name is the name of the job you want to control. The name should be unique such that no other job will be included in the search
while (( started_process < total_process ))
do
if (( `pgrep -c job_name` < max_process )); then
./job_name.sh arg1 arg2 &
(( started_process = started_process + 1 ))
echo "current number of jobs: `pgrep -c job_name`"
sleep 1
fi
done
### In fact, there is a native Linux tool named `parallel`
# sudo apt install parallel
# parallel echo ::: {1..10}
# parallel echo ::: {1..5} ::: a b c
# read more on https://www.techrepublic.com/article/how-to-run-commands-simultaneously-with-gnu-parallel/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment