Skip to content

Instantly share code, notes, and snippets.

@dvdvck
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dvdvck/66d80be2faed118525cc to your computer and use it in GitHub Desktop.
Save dvdvck/66d80be2faed118525cc to your computer and use it in GitHub Desktop.
subprocess basic control job
#!/bin/bash
#Lanza subprocesos en segundo plano, manteniendo un umbral de almenos
#4 procesos en paralelo.
todo=15
echo "`date +%M:%S` Starting PARENT $$ BASHPID: $BASHPID"
while [ $todo -gt 0 ]; do
rand=$[$RANDOM % 10]
#simulando que terminan antes entrar a background
#rand=0
echo "`date +%M:%S` ALLOCATE job number $todo after $rand seconds"
(sleep $rand; echo "`date +%M:%S` DEALLOCATE number job $todo in BASHPID: $BASHPID") &
children=(`jobs -p`)
echo "`date +%M:%S` Number of children running: ${#children[@]}"
if [ ${#children[@]} -gt 4 ]; then
echo "`date +%M:%S` Waiting for an available slot ..."
wait -n ${children[@]}
fi
todo=$[$todo - 1]
done
echo "`date +%M:%S` waiting for the rest of children ${children[@]}"
wait
echo "`date +%M:%S` done :D"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment