如何从 Bash 退出(0)超时

Mar*_*ark 10 bash travis-ci

我正在尝试设置一个 travis 脚本,在其中运行我们的应用程序以确保它正常启动。如果是,那么我们可以通过构建。测试在启动时捕获错误。但是,它是一个 api 服务器,如果我运行二进制文件并且它成功,它将无限期地运行。

我尝试使用以下方法:

timeout --preserve-status 20s <binary>

但这只是返回二进制文件的退出代码,从超时终止时为 143。

timeout 20s <binary>

成功时返回退出 127。

如果启动时出现二进制错误,并且如果成功启动说 20 秒后退出 0 以通过 travis 构建,是否有我可以使用的脚本运行二进制文件失败?

All*_*lan 8

无需使用sleep您可以通过以下方式更改您的命令以强制返回代码0

(timeout 20s <binary>; exit 0) 
Run Code Online (Sandbox Code Playgroud)

例子:

(timeout 2s '/bin/sleep' 100; exit 0) #subshell creation                                                                                        
echo $?
0
Run Code Online (Sandbox Code Playgroud)

对比

timeout 2s '/bin/sleep' 100
echo $?
124
Run Code Online (Sandbox Code Playgroud)


Noa*_*nos 8

In case you want to:

  • Return Exit code 0:

    • If command completed successfully (code 0).
    • OR if command did not complete yet (code 124), but that's OK too.
  • Return Exit code 1:

    • If command had a failure before timeout reached.

Then try this:

timeout 10m some_command || ( [[ $? -eq 124 ]] && \
echo "WARNING: Timeout reached, but that's OK" )
Run Code Online (Sandbox Code Playgroud)