通过引用bash: silently kill background function process和 Timeout a command in bash without unnecessary delay ,我编写了自己的脚本来为命令设置超时,并使 kill 消息静音。
但是当我的进程被终止时,我仍然收到“已终止”消息。我的代码有什么问题?
#!/bin/bash
silent_kill() {
kill $1 2>/dev/null
wait $1 2>/dev/null
}
timeout() {
limit=$1 #timeout limit
shift
command=$* #command to run
interval=1 #default interval between checks if the process is still alive
delay=1 #default delay between SIGTERM and SIGKILL
(
((t = limit))
while ((t > 0)); do
sleep $interval;
#kill -0 $$ || exit 0
((t -= interval))
done
silent_kill $$
#kill -s SIGTERM $$ && kill -0 $$ || exit 0
sleep $delay
#kill -s SIGKILL $$
) &> /dev/null &
exec $*
}
timeout 1 sleep 10
最佳答案
您的代码没有任何问题,“已终止”消息不是来自您的脚本,而是来自调用 shell(您启动脚本的那个) 来自)。
您可以通过禁用作业控制来停用:
$ set +m
$ bash <your timeout script>
关于bash - 当我的命令被超时终止时,如何使 "Terminated"消息静音?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32551713/