nathancahill
nathancahill

Reputation: 10850

Start a command, count lines of output after 10 seconds, then either restart it or let it run

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!

Upvotes: 1

Views: 480

Answers (2)

Ted Shaw
Ted Shaw

Reputation: 2306

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

Upvotes: 0

Stephane Rouberol
Stephane Rouberol

Reputation: 4384

What about

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

# then test on mylogfile length and kill $pid if needed

Upvotes: 4

Related Questions