gpt4 book ai didi

Linux 庆典 : timeout a program if the program hangs in there and echo 'timeout'

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:28:10 26 4
gpt4 key购买 nike

我有一个程序可以打印出通过或失败。我想检测卡在那里的程序并回显“超时”

我写了这样一个脚本:

#!/bin/bash

echo -n 'test' && timeout 5 ./mytest | grep -q -i 'passed' && echo ', passed'|| echo ', failed'

if [ $? -eq 124 ]; then
echo 'timeout'
fi

但它会将卡在那里的程序视为“失败”并终止该程序。任何建议将不胜感激。谢谢!

最佳答案

人工超时:

NAME
timeout - run a command with a time limit

SYNOPSIS
timeout [OPTION] DURATION COMMAND [ARG]...
timeout [OPTION]

DESCRIPTION
Start COMMAND, and kill it if still running after DURATION.
...

这是一个例子:

cmd_output=`timeout 5 ./mytest`

if [ $? -eq 124 ]; then
echo 'timeout'
else
echo $cmd_output
fi

关于Linux 庆典 : timeout a program if the program hangs in there and echo 'timeout' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29270744/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com