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?

  • 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, 2016 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 Aug 18, 2016 at 12:45
  • @Ipor's answer is probably what you're looking for.
    – Law29
    Aug 18, 2016 at 12:47

1 Answer 1


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

  • 1
    That worked, thanks. though the ;fi seems unnecessary. Aug 18, 2016 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, 2016 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? Aug 18, 2016 at 13:03
  • Nevermind, I got it Aug 18, 2016 at 13:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .