1

I have an interesting situation I am trying to script. I have a program that outputs 26,000 lines after 10 seconds when it starts successfully. Otherwise I have to kill it and start it again. I tried doing something like this:

test $(./long_program | wc -l) -eq 26000 && echo "Started successfully"

but that only works if the program finishes running. Is there a clever way to watch the output stream of a command and make decisions accordingly? I'm at a loss, not quite sure even how to start searching for this. Thanks!

Rohan
  • 52,392
  • 12
  • 90
  • 87
nathancahill
  • 10,452
  • 9
  • 51
  • 91

2 Answers2

4

What about

./long_program > mylogfile &
pid=$!
sleep 10

# then test on mylogfile length and kill $pid if needed
Stephane Rouberol
  • 4,286
  • 19
  • 18
0
count=0
until [ $count -eq 26000 ]; do
    killall ./longrun
    #start in background
    ./longrun >output.$$ &
    sleep 10
    count=$(wc -l output.$$ |awk '{print $1}')   
done
echo "done"
#disown so it continues after current login quits
disown -h
Ted Shaw
  • 2,298
  • 14
  • 8