Home > Back-end >  What happens if called program returns the exit code same as timeout command?
What happens if called program returns the exit code same as timeout command?

Time:09-28

I'm using timeout command in the Linux shell to call a program with a specific timeout.

And from the timeout command guide, it returns exit code 124 if a timeout happens, and returns the program exit code if no timeout happens. Unfortunately, the program that I call also returns exit code 124 in some cases.

So I just want to ask if we have any way to recognize if 124 is from the timeout command or program?

code:

timeout 10s program 2>&1 | tee log.txt
if [ "${PIPESTATUS[0]}" == 124 ]; then
    # do something
fi

Thanks

CodePudding user response:

If you do i.e. a

timeout --preserve-status 1 sleep 4

you would get a status of 143, which means that the command has been terminated by SIGTERM (128 15==143). Since a command is not supposed to return values larger than 127 to communicate a status condition, this could be used in your case to disambiguate between timeout and "real" exit status.

This assumes that you are using that timeout command which comes with the GNU coreutils.

  • Related