4

I am doing the following :

timeout 180 bash myscript.txt

myscript.txt is supposed to be fully executed in less than 180 seconds. IF NOT, I want emergencyscript.txt to be executed. Is it possible to do so ?

Like

timeout 180 [bash myscript.txt] [bash emergencyscript.txt]

3 Answers 3

14

The exit status of timeout is 124 if the command times out. You can test for this explicitly.

timeout 180 bash myscript.txt
exit_status=$?
if [[ $exit_status -eq 124 ]]; then
    bash emergencyscript.txt
fi

Any other value for exit_status is the result of myscript.txt running to completion.

Sign up to request clarification or add additional context in comments.

Comments

2
#!/bin/bash
if ! timeout 180s bash myscript.txt; then bash emergencyscript.txt; fi

Comments

1

maybe try checking to see if timeout failed or not?

timeout 180 bash myscript.txt
if [ $? -ne 0 ]
then
#....do stuff....
fi

That's dependent on timeout or the script giving a non-zero exit value if it takes too long though.

1 Comment

Fixed! I like your answer more, much more specific with the checking

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.