How to get the output of timeout command without using a shell script
6,825
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
Related videos on Youtube
Author by
Antariksha Yelkawar
Updated on September 18, 2022Comments
-
Antariksha Yelkawar almost 2 years
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?
-
Law29 almost 8 yearsWhat have you tried? For example, the man page for
timeout
saysIf 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? -
Antariksha Yelkawar almost 8 yearsI 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
-
Law29 almost 8 years@Ipor's answer is probably what you're looking for.
-
-
Antariksha Yelkawar almost 8 yearsThat worked, thanks. though the ;fi seems unnecessary.
-
Law29 almost 8 yearsYou 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
:) ) -
Antariksha Yelkawar almost 8 yearsThanks, 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 almost 8 yearsNevermind, I got it