0

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"; }
1
  • 1
    On timeout, the exit status of timeout command is 124; you can use it to distinguish the type of error but this is not really reliable since there's no way telling the difference if the script itself returns 124 Commented Feb 26, 2020 at 0:42

1 Answer 1

1

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"; }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.