我在 java 中使用 ssh 命令执行器,它运行命令并获取 stderr、stdout 和整数退出值中的输出。我正在尝试运行一个带有超时的命令,例如
timeout 5s COMMAND
有没有办法在标准错误或标准输出中获得响应,以便我可以知道命令是否超时?
答案1
从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.
所以...
timeout 5s command || [ $? -eq 124 ] && echo timeouted