I have loop function:
while IFS='|' read -r keyword; do
xterm -e "echo -ne '\e[8;5;60t'; program -command"
done < ./filename
I need to open only 5 terminals with program -command
but when one terminal do the job and close then loop should start open again until 5 opened windows.
I try
[ $( jobs | wc -l ) -ge 5 ] && wait
But after one is done job only 4 is open and this wait to the last one until is open and start again 5 times.
Maybe some can modify this scirpt to work
while [ `ps -p ${pid1},${pid2} | wc -l` < 5 ]
do
sleep 1
done
This scirpt run every one second new terminal...
while IFS='|' read -r keyword; do
xterm -e "echo -ne '\e[8;5;60t'; program -command" &
sleep 1
done < ./filename
But i need something to check if there are opened 5x terminals if not then run loop one more time.
while IFS='|' read -r keyword; do
xterm -e "echo -ne '\e[8;5;60t'; program -command"
# Script to block loop until less than 5 terminal are open
while [ `ps -p ${pid1},${pid2} | wc -l` < 5 ]
do
echo "There is less than 5 terminals opened"
done
done < ./filename
I found something like this, how to modify this?
while [ $(ps -ef | grep KEYWORD | grep -v grep | wc -l) -gt 0 ]; do
sleep 1
done
I made script
child_count=$(($(pgrep --parent $$ | wc -l) - 1))
if [ "$child_count" -lt "5" ]; then
echo "Opened terminals: $child_count"
else [ "$child_count" -ge "5" ]
echo "Too many termianls: $child_count"; wait
fi
But still is waiting until all finished, this should check every 10 sec if one terminal is less than 5
I found solution:
check_processes(){
while true; do
child_count=$(($(pgrep --parent $$ | wc -l) - 1))
if [ "$child_count" -lt "5" ]; then
echo "Opened terminals: $child_count"
break
else [ "$child_count" -ge "5" ]
echo "Too many termianls: $child_count"; sleep 10; check_processes
fi
done
}
check_processes