bash 为什么 Cygwin 执行 shell 命令很慢?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6811435/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 20:47:33  来源:igfitidea点击:

Why does Cygwin execute shell commands very slowly?

bashcygwin

提问by Lcsky

Just tests a very simple command like:

只需测试一个非常简单的命令,例如:

while true; do bash -c "echo hello"; done

You will find how much slow the bash in Cygwin is. Does anybody know why?

您会发现 Cygwin 中的 bash 有多慢。有人知道为什么吗?

It's a fresh install of cygwin 1.7 on win7.

这是在 win7 上全新安装的 cygwin 1.7。

thanks to Jared's testing idea, I modified the command to this(adds bash -c):

感谢 Jared 的测试想法,我将命令修改为此(添加 bash -c):

time for i in {1..10} ; do bash -c "echo Hello" ; done
Hello
...
real 0m7.711s //it's the problem
user 0m0.091s
sys 0m0.476s

采纳答案by Lcsky

Finally, I found the source - a service named "QQPCMgr RTP Service" running on my office computer, it's the real time protection service of "QQ PC Manager".

终于找到了源头——在我的办公电脑上运行着一个名为“QQPCMgr RTP Service”的服务,它是“QQ电脑管家”的实时保护服务。

By disabling it, the time of the script in the question falls back to:

通过禁用它,问题中脚本的时间回落到:

real    0m0.943s
user    0m0.105s
sys     0m0.231s

I have told the developers of QQPCMgr about this, hope they will find the reason.

我已经把这件事告诉了QQPCMgr的开发者,希望他们能找到原因。

This still much slower than Linux, but gets the same "real time" of other cygwin computers.

这仍然比 Linux 慢得多,但获得与其他 cygwin 计算机相同的“实时”。

Thank you all!

谢谢你们!

回答by dotz

How about excluding Cygwin paths from your antivirus software ?

从防病毒软件中排除 Cygwin 路径怎么样?

回答by metatechbe

Below are 3 possible causes which I encountered. Of course, other problems not mentioned can also cause slow shell commands in Cygwin.

以下是我遇到的 3 个可能的原因。当然,其他没有提到的问题也会导致 Cygwin 中的 shell 命令变慢。

  • If you have the option "Automatically detect settings" in "LAN Settings", Windows will use the WPADprotocol to discover the local HTTP proxy. First it will send a DHCP "Inform" request with option 252, then it will try a DNS lookup on "wpad". These 2 operations can take a few seconds to time-out.

  • If the shell accesses some paths like /cygdrive/..., a NetBIOS name query will be executed, which can also take some time to time out.

  • If the shell accesses some paths like //mypath/..., a NetBIOS name query will be executed, which can also take some time to time out.

  • 如果在“LAN 设置”中有“自动检测设置”选项,Windows 将使用WPAD协议来发现本地 HTTP 代理。首先它会发送一个带有选项 252 的 DHCP“通知”请求,然后它会尝试在“wpad”上进行 DNS 查找。这 2 个操作可能需要几秒钟才能超时。

  • 如果 shell 访问某些路径,例如/cygdrive/...,将执行 NetBIOS 名称查询,这也可能需要一些时间来超时。

  • 如果 shell 访问某些路径,例如//mypath/...,将执行 NetBIOS 名称查询,这也可能需要一些时间来超时。

Solutions :

解决方案:

  • Disable "Automatically detect settings" in "LAN Settings" in the Windows "Internet Options" control panel.

  • Add the following entry in %SystemRoot%\system32\drivers\etc\hosts:

  • 在 Windows“Internet 选项”控制面板的“局域网设置”中禁用“自动检测设置”。

  • 在 中添加以下条目%SystemRoot%\system32\drivers\etc\hosts

127.0.0.1 localhost cygdrive wpad

127.0.0.1 localhost cygdrive wpad

  • Make sure to avoid double slashes at the beginning of all paths.
  • 确保避免在所有路径的开头使用双斜杠。

回答by Jeremy J Starcher

Check your path. Referring a non-existant path or a very slow network share can cause the symptoms you are describing.

检查你的路径。引用不存在的路径或非常慢的网络共享可能会导致您所描述的症状。

回答by Desmond Campbell

This is how this problem manifested for me. For the first say 20 commands entered at the cygwin prompt it is fast, then it abruptly becomes painfully slow.

这就是这个问题对我的表现方式。对于第一个在 cygwin 提示符下输入的 20 个命令,它很快,然后突然变得非常缓慢。

I checked that every item in my path was a valid directory.

我检查了我路径中的每个项目都是一个有效的目录。

I tried an authentication fix suggested elsewhere - https://superuser.com/questions/877051/cygwin-from-windows-very-slow-laggy/1247517. It didn't work for me.

我尝试了其他地方建议的身份验证修复 - https://superuser.com/questions/877051/cygwin-from-windows-very-slow-laggy/1247517。它对我不起作用。

I replaced my exisitng virus scanner (System Center Endpoint Protection) with Sophos. That fixed the problem for me. Hope this helps.

我用 Sophos 替换了我现有的病毒扫描程序(System Center Endpoint Protection)。那为我解决了问题。希望这可以帮助。

回答by Jared

Let's put some numbers to it. I ran the following:

让我们给它一些数字。我运行了以下内容:

time for i in {1..1000} ; do echo "Hello" ; done

The result I get from a standard Cygwin bash window is:

我从标准 Cygwin bash 窗口得到的结果是:

...
Hello
Hello

real    0m0.584s
user    0m0.031s
sys     0m0.000s

And from a xterm bash window on the same system I get:

从同一系统上的 xterm bash 窗口我得到:

...
Hello
Hello

real    0m0.037s
user    0m0.016s
sys     0m0.000s

I think this pretty much answers the question for you. The problem is you're going through a "Windows" "cmd" like window, which is inherently slow. Cygwin itself isn't the problem, it's the display trying to keep up that is slowing things down (for this test).

我认为这几乎可以回答您的问题。问题是您正在通过“Windows”“cmd”之类的窗口,这本身就很慢。Cygwin 本身不是问题,是显示器试图跟上,导致速度变慢(对于此测试)。

回答by Teresa e Junior

I voted up on James McLeod, because starting a bash process takes some time, but it doesn't mean it will run commands slower than in UNIX.

我对 James McLeod 投了赞成票,因为启动 bash 进程需要一些时间,但这并不意味着它运行命令的速度会比在 UNIX 中慢。

Invoking bash -cfrom within a bash script is near to senseless, and Makefiles can call a lot of bash subprocesses, unless you append ; \at the end of the commands.

bash -c从 bash 脚本中调用几乎毫无意义,并且 Makefile 可以调用大量 bash 子进程,除非您; \在命令末尾附加。

For example, if a Makefile has the following:

例如,如果 Makefile 具有以下内容:

echo Hello World
echo Good Bye

It will call two bash processes. To make it faster and call just one bash process:

它将调用两个 bash 进程。为了使其更快并仅调用一个 bash 进程:

echo Hello World; \
echo Good Bye

Debian has adopted dash instead of bash as the main shell, because starting many init scripts using bash will make the system take much longer to boot (each script call its own bash process).

Debian 采用 dash 而不是 bash 作为主 shell,因为使用 bash 启动许多 init 脚本会使系统启动时间更长(每个脚本调用自己的 bash 进程)。

回答by James McLeod

Invoking bash from a shell script can't be fast. Is it faster to just use

从 shell 脚本调用 bash 不能很快。只是使用会更快吗

while true; echo hello; done

?

?

回答by James McLeod

I was able to fix this problem by uninstalling bash-completion and switching from Kaspersky to Windows Security Essentials. (I diagnosed the Kaspersky interference using Process Explorer while running the "echo Hello" benchmark.) Brought my benchmark time from 7 seconds to 0.2.

通过卸载 bash-completion 并从 Kaspersky 切换到 Windows Security Essentials,我能够解决这个问题。(我在运行“echo Hello”基准测试时使用 Process Explorer 诊断出卡巴斯基干扰。)将基准测试时间从 7 秒缩短到 0.2 秒。

回答by Futur'Fusionneur

On Comodo Internet Security Premium 10, i added "C:\Users\\Documents\MobaXterm\slash\bin" to the list of trusted files. (Settings -> File rating -> File list). Local terminal is fast again now.

在 Comodo Internet Security Premium 10 上,我将“C:\Users\\Documents\MobaXterm\slash\bin”添加到受信任文件列表中。(设置 -> 文件评级 -> 文件列表)。现在本地终端又快了。