y.selivonchyk
y.selivonchyk

Reputation: 9900

How to handle non-timeout exception of a script being run with timeout?

I am running a command. In case of failure part after || would be executed. It would also be executed in case of timeout.

$ timeout 5 script.sh || { echo "Would execute if timeout or script failure"; }

How can I handle timeout error and script error separately?

$ timeout 5 script.sh ??? { echo "Timeout is fine, but this is printed only if script.sh returns an error"; }

Upvotes: 0

Views: 285

Answers (1)

Mark
Mark

Reputation: 4453

Oguz is correct. You need to check the exit code of the timeout command. If you can review the script.sh to make sure that it doesn't return an exit code of 124, you can help reduce the risk identified by Oguz.

So, I believe the code would look like this:

$ timeout 5 script.sh || [ $? -eq 124 ] || { echo "Timeout is fine, but this is printed only if script.sh returns an error"; }

Upvotes: 1

Related Questions