Search code examples
bashawkpipeinfinite-loop

terminate linux program when lines repeat endlessly


A finance-crunching program I am dealing with, cruncher.js, has annoying bugs difficult to troubleshoot. One common problem (whose triggering input is difficult to pointpoint and therefore avoid) causes this failure scenario:

Downloading account information...
Downloading today orders...
Downloading historical quotes...
Downloading historical quotes...
Downloading historical quotes...
Downloading historical quotes...

Once the line "Downloading historical quotes..." repeats for a third time, I know it's hit an infinite loop and never exits, nor skips whatever input case it can't handle gracefully.

How can I pipe this cruncher.js program to |awk, i.e. what inline awk script would detect in its input a 3rd (or 2nd if much easier) consecutive repeated line, and terminate there?

Or maybe instead of awk, using other common Linux/shell tools?


Solution

  • here is one way

    $ yes | awk -v key='y' '{if($0==key)c++; else c=0} c==3{exit}1' 
    y
    y
    

    replace the key value with your repeated value; and yes with your stream generator.