Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to timeout with exit(0) from Bash

Tags:

bash

travis-ci

I am trying to setup a travis script where we run our application to make sure it starts up fine. If it does then we can pass the build. Testing catches errors on start up. However, it is an api server and if I run the binary and it is successful it will just run indefinitely.

I tried using the following:

timeout --preserve-status 20s <binary>

But this just returns the exit code of the binary which is 143 when killed from a timeout.

timeout 20s <binary>

This returns exit 127 when successful.

Is there a script I could use that runs the binary fails if the binary errors on startup and if sucessful starting up say after 20 seconds exits with 0 to pass the travis build?

like image 650
Mark Avatar asked May 17 '18 02:05

Mark


2 Answers

In case you want to:

  • Return Exit code 0:

    • If command completed successfully (code 0).
    • OR if command did not complete yet (code 124), but that's OK too.
  • Return Exit code 1:

    • If command had a failure before timeout reached.

Then try this:

timeout 10m some_command || ( [[ $? -eq 124 ]] && \
echo "WARNING: Timeout reached, but that's OK" )
like image 196
Noam Manos Avatar answered Oct 13 '22 00:10

Noam Manos


No need to use sleep you can change your command in the following way to force the return code at 0:

(timeout 20s <binary>; exit 0) 

Example:

(timeout 2s '/bin/sleep' 100; exit 0) #subshell creation                                                                                        
echo $?
0

vs

timeout 2s '/bin/sleep' 100
echo $?
124
like image 42
Allan Avatar answered Oct 13 '22 00:10

Allan