如何检查 bash 中的无限循环?
How to check infinite loop in bash?
我一直在尝试使用 bash 多次 运行 一个可执行文件。此可执行文件有可能陷入无限循环或段错误。我知道 bash 中没有 try-catch 但我们可以使用:
绕过它
{ #try
"myCommand" && "do what i want"
} || { #except
"handle error"
}
但是这个无法理解无限循环。我该如何处理这个问题?
Bash 无法告诉您 myCommand
内部发生了什么,除非该循环发送信号或修改 system/environment。你可以 运行 你的 #try 在后台 &
,然后如果它在一定时间后仍然 运行ning 就做一些事情。 $!
指的是最后一个后台任务。
查看 Bash 中的 Job Control。
myCommand && doWhatIWant &
sleep 10
ps $! &>/dev/null && kill $!
您可以从 gnu coreutils 中使用 timeout
。
这里有一个超时为 10 秒的例子
timeout 10s yourscript.sh
我一直在尝试使用 bash 多次 运行 一个可执行文件。此可执行文件有可能陷入无限循环或段错误。我知道 bash 中没有 try-catch 但我们可以使用:
绕过它{ #try
"myCommand" && "do what i want"
} || { #except
"handle error"
}
但是这个无法理解无限循环。我该如何处理这个问题?
Bash 无法告诉您 myCommand
内部发生了什么,除非该循环发送信号或修改 system/environment。你可以 运行 你的 #try 在后台 &
,然后如果它在一定时间后仍然 运行ning 就做一些事情。 $!
指的是最后一个后台任务。
查看 Bash 中的 Job Control。
myCommand && doWhatIWant &
sleep 10
ps $! &>/dev/null && kill $!
您可以从 gnu coreutils 中使用 timeout
。
这里有一个超时为 10 秒的例子
timeout 10s yourscript.sh