如何从Bash退出(0)进行超时 [英] How to timeout with exit(0) from Bash

查看:75
本文介绍了如何从Bash退出(0)进行超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to setup a travis script where we run our application to make sure it starts up fine. If it does then we can pass the build. Testing catches errors on start up. However, it is an api server and if I run the binary and it is successful it will just run indefinitely.

我尝试使用以下内容:

超时--preserve-status 20s< binary>

但是这只会返回二进制文件的退出代码,当退出超时时,退出代码为143.

But this just returns the exit code of the binary which is 143 when killed from a timeout.

超时20秒< binary>

成功时将返回出口127.

This returns exit 127 when successful.

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

Is there a script I could use that runs the binary fails if the binary errors on startup and if sucessful starting up say after 20 seconds exits with 0 to pass the travis build?

推荐答案

无需使用 sleep ,您可以通过以下方式更改命令,以将返回码强制设置为 0 :

No need to use sleep you can change your command in the following way to force the return code at 0:

(timeout 20s <binary>; exit 0) 

示例:

(timeout 2s '/bin/sleep' 100; exit 0) #subshell creation                                                                                        
echo $?
0

vs

timeout 2s '/bin/sleep' 100
echo $?
124

这篇关于如何从Bash退出(0)进行超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆