Search code examples
bashinfinite-loop

How to check infinite loop in bash?


I have been trying to run an executable using a bash multiple times. There is a chance that this executable will fall into infinite loop, or segfaults. I know there is no try-catch in bash but we can bypass that using:

{ #try
    "myCommand" && "do what i want"
} || { #except
    "handle error"
}

But this is not capable of understanding infinite loop. How can I handle this problem?


Solution

  • You can user timeout from the gnu coreutils.

    Here a example for a timeout of 10 seconds

    timeout 10s yourscript.sh