在给定的时间 bash 后终止进程?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7851889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Kill process after a given time bash?
提问by LF4
I have a script that tries to make a DB connection using another program and the timeout(2.5min) of the program is to long. I want to add this functionality to the script.
我有一个脚本尝试使用另一个程序建立数据库连接,并且程序的超时(2.5 分钟)很长。我想将此功能添加到脚本中。
If it takes longer then 5 seconds to connect, kill the process
Else kill the sleep/kill process.
如果连接时间超过 5 秒,则终止进程
否则终止睡眠/终止进程。
The issue I'm having is how bash reports when a process is killed, that's because the processes are in the same shell just the background. Is there a better way to do this or how can I silence the shell for the kill commands?
我遇到的问题是当一个进程被杀死时 bash 如何报告,那是因为这些进程在同一个 shell 中,只是后台。有没有更好的方法来做到这一点,或者我怎样才能让 shell 停止执行 kill 命令?
DB_CONNECTION_PROGRAM > $CONNECTFILE &
pid=$!
(sleep 5; kill $pid) &
sleep_pid=$!
wait $pid
# If the DB failed to connect after 5 seconds and was killed
status=$? #Kill returns 128+n (fatal error)
if [ $status -gt 128 ]; then
no_connection="ERROR: Timeout while trying to connect to $dbserver"
else # If it connected kill the sleep and any errors collect
kill $sleep_pid
no_connection=`sed -n '/^ERROR:/,$p' $CONNECTFILE`
fi
采纳答案by TacticalCoder
I don't know if it's identical but I did fix a similar issue a few years ago. However I'm a programmer, not a Unix-like sysadmin so take the following with a grain of salt because my Bash-fu may not be that strong...
我不知道它是否相同,但几年前我确实解决了类似的问题。但是,我是一名程序员,而不是类似 Unix 的系统管理员,因此请保留以下内容,因为我的 Bash-fu 可能没有那么强...
Basically I did fork, fork and fork : )
基本上我做了叉子,叉子和叉子:)
Out of memoryAfter founding back my old code (which I amazingly still use daily) because my memory wasn't good enough, in Bash it worked a bit like this:
内存不足在重新创建我的旧代码(令人惊讶的是我仍然每天使用)之后,因为我的记忆力不够好,在 Bash 中它的工作方式有点像这样:
commandThatMayHang.sh 2 > /dev/null 2>&1 & (notice that last '&', we're forking)
MAYBE_HUNG_PID=$!
sleepAndMaybeKill.sh $MAYBE_HUNG_PID 2 > /dev/null 2>&1 & (we're forking again)
SLEEP_AND_MAYBE_KILL_PID=$!
wait $MAYBE_HUNG_PID > /dev/null 2>&1
if [ $? -eq 0 ]
# commandThatMayHand.sh did not hang, fine, no need to monitor it anymore
kill -9 $SLEEP_AND_MAYBE_KILL 2> /dev/null 2>&1
fi
where sleepAndMaybeKill.shsleeps the amount of time you want and then kills commandThatMayHand.sh.
其中sleepAndMaybeKill.sh睡眠您想要的时间,然后杀死commandThatMayHand.sh。
So basically the two scenario are:
所以基本上这两种情况是:
your command exits fine (before your 5 seconds timeout or whatever) and so the waitstop as soon as your command exits fine (and kills the "killer" because it's not needed anymore
the command locks up, the killer ends up killing the command
你的命令退出正常(在你的 5 秒超时之前或其他什么之前),所以一旦你的命令退出正常,等待就会停止(并杀死“杀手”,因为它不再需要了
命令锁定,杀手最终杀死命令
In any case you're guaranteed to either succeed as soon as the command is done or to fail after the timeout.
在任何情况下,您都可以保证在命令完成后立即成功或在超时后失败。
回答by Itay Perl
There's a GNU coreutils utility called timeout: http://www.gnu.org/s/coreutils/manual/html_node/timeout-invocation.html
有一个名为 timeout 的 GNU coreutils 实用程序:http: //www.gnu.org/s/coreutils/manual/html_node/timeout-invocation.html
If you have it on your platform, you could do:
如果你的平台上有它,你可以这样做:
timeout 5 CONNECT_TO_DB
if [ $? -eq 124 ]; then
# Timeout occurred
else
# No hang
fi
回答by rubo77
You can set a timeout after 2 hours and restart your javaScriptThatStalls
100 times this way in a loop
您可以在 2 小时后设置超时并以javaScriptThatStalls
这种方式循环重新启动100 次
seq 100|xargs -II timeout $((2 * 60 * 60)) javaScriptThatStalls
回答by Kevin
Do you mean you don't want the error message printed if the process isn't still running? Then you could just redirect stderr
: kill $pid 2>/dev/null
.
你的意思是如果进程没有继续运行,你不希望打印错误消息?然后你可以重定向stderr
: kill $pid 2>/dev/null
。
You could also check whether the process is still running:
您还可以检查进程是否仍在运行:
if ps -p $pid >/dev/null; then kill $pid; fi
回答by waseq
I found this bash script timeout.shby Anthony Thyssen (his web). Looks good.
我找到 了 Anthony Thyssen(他的网站)的这个 bash 脚本 timeout.sh。看起来挺好的。