Command-Line
如何在不使用 shell 腳本的情況下獲取 timeout 命令的輸出
我在 java 中使用 ssh 命令執行器,它執行命令並以 stderr、stdout 和整數退出值獲取輸出。我正在嘗試執行一個超時命令,例如,
timeout 5s COMMAND
有沒有辦法在 stderr 或 stdout 中獲得響應,以便我可以知道命令是否超時?
來自
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