当超时时间太长时,为什么超时不会中断`git status`? [英] Why does timeout not interrupt `git status` when it takes too long?
问题描述
在大型仓库中运行超时0.01s git状态
,其中 git status
大约需要一秒钟,这与 git status
没有什么不同.即,超时似乎并不会在花费太长时间时中断Git.为什么会这样?
Running timeout 0.01s git status
on a large repo where git status
takes about a second doesn't do anything different from git status
. I.e., timeout doesn't seem to interrupt Git when it takes too long. Why would this be?
推荐答案
By default, the timeout command simply sends a SIGTERM signal whenever the timeout expires. This signal can be caught and ignored by processes, which would cause the process not to terminate. Git is probably ignoring the SIGTERM.
从超时的联机帮助页:
如果未指定信号,则在超时时发送TERM信号.这TERM信号会杀死任何不会阻止或捕获该进程的进程信号.可能有必要使用KILL(9)信号(...)
If no signal is specified, send the TERM signal upon timeout. The TERM signal kills any process that does not block or catch that signal. It may be necessary to use the KILL (9) signal (...)
如果您确实需要超时,则可以使用 -k
开关发送SIGKILL.使用时需要您自担风险.这可能会损坏您的存储库,也可能不会损坏您的存储库:
If you really need it to timeout, you can use the -k
switch to send a SIGKILL instead. Use at your own risk. This may or may not corrupt you repo:
timeout -k 0.02s 0.01s git status
这将首先在0.01s之后发送SIGTERM,然后在0.02s之后发送SIGKILL.
This will first send a SIGTERM after 0.01s and then a SIGKILL after 0.02s of the first signal.
请注意,您需要传递两个持续时间(一个用于SIGTERM,一个用于SIGKILL).我建议您使用更长的持续时间(例如1秒),否则git可能将无法执行任何有用的操作,具体取决于您回购的大小.
Note that you need to pass two durations (one for SIGTERM and one for SIGKILL). I suggest you use a longer duration (e.g. 1s), otherwise git will probably not be able to do anything useful, depending on the size of your repo.
这篇关于当超时时间太长时,为什么超时不会中断`git status`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!