I am using a ssh command executor in java which runs the command and gets the output in stderr, stdout and an integer exit value. I am trying run a command with timeout like,

    timeout 5s COMMAND

Is there a way to get a response in the stderr or the stdout so that I can know whether the command was timed out or not?

share|improve this question
    
What have you tried? For example, the man page for timeout says If the command times out, and --preserve-status is not set, then exit with status 124. Are you trying to distinguish that from a command that actually does exit with status 124? – Law29 Aug 18 '16 at 12:43
    
I am pretty much a noob in linux, but all I want is to know whether the command was timed out as an output in stderr or stdout – Antariksha Yelkawar Aug 18 '16 at 12:45
    
@Ipor's answer is probably what you're looking for. – Law29 Aug 18 '16 at 12:47
up vote 1 down vote accepted

From man timeout:

   If  the  command times out, and --preserve-status is not set, then exit
   with status 124.  Otherwise, exit with the status of  COMMAND.   If  no
   signal  is specified, send the TERM signal upon timeout.  The TERM sig‐
   nal kills any process that does not block or catch that signal.  It may
   be  necessary  to  use the KILL (9) signal, since this signal cannot be
   caught, in which case the exit status is 128+9 rather than 124.

So... timeout 5s command || [ $? -eq 124 ] && echo timeouted

share|improve this answer
    
That worked, thanks. though the ;fi seems unnecessary. – Antariksha Yelkawar Aug 18 '16 at 12:48
1  
You may prefer timeout 5s command ; if [ $? -eq 124 ] ; then echo timed out ; else echo did not time out ; fi (I think Ipor started with this and forgot to remove the ;fi :) ) – Law29 Aug 18 '16 at 12:50
    
Thanks, That's much more of a complete answer that I needed. Just a small doubt, this is echoed in stdout right? how do it do it in stderr? – Antariksha Yelkawar Aug 18 '16 at 13:03
    
Nevermind, I got it – Antariksha Yelkawar Aug 18 '16 at 13:15

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.