Home > other >  Execute command while process is running and end once process ends using bash
Execute command while process is running and end once process ends using bash

Time:05-12

I am trying to create a while loop that will print "." while a shell script process is running and stop printing when the process has finished. My code continues to print "." after launch.sh has completed.

sh launch.sh & PIDIOS=$!

dot_function() {
running=$(ps aux | grep launch.sh | wc -l)
while [ $running -ge 1 ]; do
   if [ $running -lt 1 ]; then
      break
   elif [ $running -ge 1 ];
      printf "."
      sleep 1
   fi
done
} 
dot_function & PIDMIX=$

CodePudding user response:

How about using kill -0?

dotwait() {
    while kill -0 "$1" 2>/dev/null
    do
        printf .
        sleep 1
    done
    echo
}

sh launch.sh & dotwait "$!"
  • Related