The Linux timeout command and exit codes The Linux timeout command and exit codes shell shell

The Linux timeout command and exit codes


Your situation isn't very clear because you haven't included your code in the post.

timeout does exit with the exit code of the command if it finishes before the timeout value.

For example:

timeout 5 ls -l non_existent_file# outputs ERROR: ls: cannot access non_existent_file: No such file or directoryecho $?# outputs 2 (which is the exit code of ls)

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 signal 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.


See BashFAQ105 to understand the pitfalls of set -e.