Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I'm using the timeout function on debian to wait 5 seconds for my script. Works great but the problem I have is that I need a return value. Like 1 for timeout and 0 for no timeout How am I going to do this?

Have a look at my code:

timeout 5 /some/local/script/connect_script -x 'status' > output.txt
# here i need the return of timeout

As you see my connect_script -x 'status' returns the status as a string and print it to the screen (probably you can't see this) Background of this issue is that if the server (for connect_script) is freeze the script does nothing. That's why I need the timeout around that. And when it timeouts I want to restart the server. I can do that, but I have no idea how I can see if its timeout or not...

share|improve this question

2 Answers 2

up vote 3 down vote accepted

If timeout times out, it exits with status 124; you can check this to determine whether the script timed out or not.

share|improve this answer
    
Yeah thanks. I miss this in the manual. @Christopher post an answer that works for me. I don't know the $? syntax. So both of your answers are right ;) –  Zero May 22 at 14:55

According the manual (man timeout):

Synopsis timeout [OPTION] NUMBER[SUFFIX] COMMAND [ARG]...

[...] If the command times out, then exit with status 124. Otherwise, exit with the status of COMMAND

Combine this with the knowledge that the exit status or return value is stored in the variable, $?, and we have...

timeout 5 /some/local/script/connect_script -x 'status' > output.txt
RETVAL=$?

Then, you can do more processing based on the value of $RETVAL, which will be 124 if it times out, or some other value based on the exit status of connect_script.

share|improve this answer
    
Thank you very much! Thats great –  Zero May 22 at 14:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.