0

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
  • 1
    Are you asking how to have five terminal windows always open, such that when one is closed a new one is created? – roaima Sep 9 '18 at 22:21
  • Yes, thats right – unknown Sep 9 '18 at 22:21
  • 2
    It looks like xyproblem. Why don't you use parallel? – Ipor Sircer Sep 10 '18 at 0:09
  • i use loop variable for other commands like echo or other program parallel can read only for one program one line another parallel for echo with be diffrent variable – unknown Sep 10 '18 at 11:15
1

This code segment will create five xterm windows. As you kill them off another one will be spawned.

#!/bin/bash
#
pids=()

# Eternal loop
while :
do
    if [[ ${#pids[@]} -lt 5 ]]
    then
        # Start another process
        xterm &
        pid=$!

        # Save process id
        pids+=($pid)
    else
        # We have enough so wait for (at least) one to die
        wait -n

        # Reap old process ids
        oldpids=("${pids[@]}")
        pids=()
        n=0
        while [[ $n -lt ${#oldpids[@]} ]]
        do
            pid=${oldpids[$n]}
            [[ -n "$pid" ]] && kill -0 $pid 2>/dev/null && pids+=($pid)
            n=$((n+1))
        done
    fi
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.