Bash脚本在OSX上启动非常慢 [英] Bash scripts extremely slow to start on OSX
问题描述
我遇到了一个问题,我的bash脚本需要很长时间才能启动.起初我以为这是脚本本身的东西,但是快速实验证明了这一点.
I'm having a problem where my bash scripts take an extremely long time to start. At first I thought that it was something in the script itself, but a quick experiment disproved that.
这是示例脚本
#!/bin/bash
echo 'ping'
运行时
$ time ./script.sh
我知道
ping
real 0m12.018s
user 0m0.002s
sys 0m0.002s
(我什至看过它长达17秒...)令人难以置信的是,当我第二次运行它时,它会立即运行.
(I've even seen it go up to 17 seconds...) What's incredibly strange is that when I run it the second time, it goes instantly.
ping
real 0m0.004s
user 0m0.002s
sys 0m0.002s
但是当我再次编辑文件时,又回到了漫长的等待状态.
But when I edit the file again, it's back to the long wait.
$ echo 'echo test' >> gbr.sh
$ time ./gbr.sh
ping
test
real 0m13.021s
user 0m0.003s
sys 0m0.003s
这几乎就像bash正在编译我的脚本之类的东西. 有什么方法可以调试吗?我的.bash_profile
似乎也没什么用-如果我只是在.bash_profile
的第一行加上回显,我仍然会看到10+秒.
It's almost like bash is compiling my script or something. Is there any way to debug this? It also doesn't seem to be anything with my .bash_profile
- if I just put an echo on the first line of .bash_profile
I still only see that after 10+ seconds.
我已经在iTerm2和本机Terminal中尝试过-两者都存在完全相同的问题.这是在macOS Sierra-10.12.3
I've tried this in iTerm2 and in native Terminal - both have exactly the same issue. This is on macOS Sierra - 10.12.3
推荐答案
我在运行macOS Sierra 10.12.5和10.12.6的工作机上遇到了相同的问题,并且我确认@ charles-duffy是正确的.我公司的反恶意软件是问题所在.他们正在使用Carbon Black的名为Confer的守护程序来实现端点安全.
I had the same issue on my work machine, running macOS Sierra 10.12.5 and 10.12.6, and I confirmed that @charles-duffy is right. My company's anti-malware is the problem. They are using a daemon called Confer by Carbon Black for endpoint security.
运行此命令以查看问题是否停止:
Run this command to see if the problem stops:
launchctl unload /Library/LaunchDaemons/com.confer.sensor.daemon.plist
我认为Confer正在为它看到的每个新程序添加指纹,并尝试在允许其运行之前将其与黑名单进行比较.似乎一秒钟后超时(尝试在线注册指纹?),并且有一些原因使其反复尝试,从而产生更长的整数秒延迟.尽管经过指纹识别后速度恢复正常,但这会使新修改的程序缓慢启动.
I think Confer is taking a fingerprint of every new program it sees and tries to compare it to a blacklist before allowing it to run. It looks like it times out after a second (trying to register the fingerprint online?), and something is making it try over and over again, yielding longer delays of integer seconds. This makes newly modified programs launch slowly, although speeds return to normal after the fingerprint has been taken.
这是我的笔记本电脑上出现问题的方式.随着时间的流逝,有时延迟会增加,但每次都会增加整数秒.如果Confer记得以前的程序,就不会有延迟.
Here's how the problem manifested on my work laptop. As time went on, sometimes the delay increased, but always by an integer number of seconds at a time. If Confer remembers the program from before, there will be no delay.
$ bash --norc --noprofile -l # make sure rc scripts don't interfere
bash-3.2$ echo exit > exit.sh && chmod +x exit.sh
bash-3.2$ time ./exit.sh
real 0m1.004s
user 0m0.001s
sys 0m0.002s
bash-3.2$ time ./exit.sh
real 0m0.002s
user 0m0.001s
sys 0m0.001s
运行提供给bash -c
的命令没有问题:
There were no problems running commands provided to bash -c
:
bash-3.2$ time bash -c exit
real 0m0.008s
user 0m0.002s
sys 0m0.003s
awk脚本
但是问题确实影响了awk脚本:
awk scripts
But the problem did affect awk scripts:
bash-3.2$ printf '%s\n' '#!/usr/bin/env awk -f' 'BEGIN { exit 0; }' > test.awk && chmod +x test.awk
bash-3.2$ time ./test.awk
real 0m4.010s
user 0m0.002s
sys 0m0.001s
bash-3.2$ time ./test.awk
real 0m0.005s
user 0m0.002s
sys 0m0.001s
已编译的二进制文件
它甚至影响了已编译的C和Golang代码:
Compiled binaries
It even affected compiled C and Golang code:
bash-3.2$ printf '%s\n' '#include "stdio.h"' 'int main() {' 'printf("Hello, world!\n");' 'return 0;' '}' > hello.c && gcc -o hello hello.c
bash-3.2$ time ./hello
Hello, world!
real 0m4.006s
user 0m0.001s
sys 0m0.001s
bash-3.2$ time ./hello
Hello, world!
real 0m0.004s
user 0m0.001s
sys 0m0.001s
bash-3.2$ printf '%s\n' 'package main' 'import "fmt"' 'func main() {' 'fmt.Println("test")' '}' > test.go && go build -o test ./test.go
bash-3.2$ time ./test
test
real 0m4.018s
user 0m0.001s
sys 0m0.003s
bash-3.2$ time ./test
test
real 0m0.005s
user 0m0.001s
sys 0m0.002s
这篇关于Bash脚本在OSX上启动非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!