所有流浪的PHP CLI脚本都在Mac OS X上等待主机解析

时间:2015-10-17 16:47:01

标签: php macos ubuntu command-line-interface

在我运行Ubuntu的Homestead vagrant框上执行PHP命令时,我一直挂起。在控制台甚至启动php cli执行之前有一个显着的延迟。

从流浪盒中跑出strace -vyT -S time php artisan help。在recvfrom(3的倒数第一次通话中,所有内容都会卡住几分钟,但我不明白为什么:

open("/etc/resolv.conf", O_RDONLY|O_CLOEXEC) = 3 <0.000010>
fstat(3</run/resolvconf/resolv.conf>, {st_dev=makedev(0, 16), st_ino=7632, st_mode=S_IFREG|0644, st_nlink=1, st_uid=0, st_gid=0, st_blksize=4096, st_blocks=8, st_size=171, st_atime=2015/10/17-04:53:56, st_mtime=2015/10/17-04:53:54, st_ctime=2015/10/17-04:53:54}) = 0 <0.000007>
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fed9385c000 <0.000008>
read(3</run/resolvconf/resolv.conf>, "# Dynamic resolv.conf(5) file fo"..., 4096) = 171 <0.000010>
read(3</run/resolvconf/resolv.conf>, "", 4096) = 0 <0.000006>
close(3</run/resolvconf/resolv.conf>)   = 0 <0.000008>
munmap(0x7fed9385c000, 4096)            = 0 <0.000011>
uname({sysname="Linux", nodename="homestead", release="3.13.0-65-generic", version="#106-Ubuntu SMP Fri Oct 2 22:08:27 UTC 2015", machine="x86_64", domainname="(none)"}) = 0 <0.000006>
stat("/etc/resolv.conf", {st_dev=makedev(0, 16), st_ino=7632, st_mode=S_IFREG|0644, st_nlink=1, st_uid=0, st_gid=0, st_blksize=4096, st_blocks=8, st_size=171, st_atime=2015/10/17-04:53:56, st_mtime=2015/10/17-04:53:54, st_ctime=2015/10/17-04:53:54}) = 0 <0.000008>
open("/etc/hosts", O_RDONLY|O_CLOEXEC)  = 3 <0.000009>
fstat(3</etc/hosts>, {st_dev=makedev(8, 1), st_ino=1161, st_mode=S_IFREG|0644, st_nlink=1, st_uid=0, st_gid=0, st_blksize=4096, st_blocks=8, st_size=251, st_atime=2015/10/16-18:57:29, st_mtime=2014/10/03-01:16:42, st_ctime=2014/10/03-01:16:42}) = 0 <0.000005>
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fed9385c000 <0.000008>
read(3</etc/hosts>, "127.0.0.1 localhost\n\n# The follo"..., 4096) = 251 <0.000009>
read(3</etc/hosts>, "", 4096)           = 0 <0.000007>
close(3</etc/hosts>)                    = 0 <0.000007>
munmap(0x7fed9385c000, 4096)            = 0 <0.000010>
socket(PF_INET, SOCK_STREAM, IPPROTO_IP) = 3 <0.000012>
fcntl(3<socket:[78362]>, F_SETFL, O_RDONLY|O_NONBLOCK) = 0 <0.000006>
connect(3, {sa_family=AF_INET, sin_port=htons(9000), sin_addr=inet_addr("127.0.0.1")}, 16) = -1 EINPROGRESS (Operation now in progress) <0.000094>
select(4, [3<socket:[78362]>], [3<socket:[78362]>], [3<socket:[78362]>], {0, 200000}) = 1 (out [3], left {0, 199997}) <0.000010>
getpeername(3, {sa_family=AF_INET, sin_port=htons(9000), sin_addr=inet_addr("127.0.0.1")}, [16]) = 0 <0.000007>
fcntl(3<socket:[78362]>, F_SETFL, O_RDONLY) = 0 <0.000006>
setsockopt(3, SOL_TCP, TCP_NODELAY, "\1\0\0\0\0\0\0\0", 8) = 0 <0.000008>
write(3<socket:[78362]>, "478\0<?xml version=\"1.0\" encoding"..., 483) = 483 <0.000042>
brk(0x2f93000)                          = 0x2f93000 <0.000472>
recvfrom(3, 0x7ffc489b16d0, 128, 0, 0, 0) = ? ERESTARTSYS (To be restarted if SA_RESTART is set) <4.940291>
--- SIGWINCH {si_signo=SIGWINCH, si_code=SI_KERNEL} ---
recvfrom(3, 0x7ffc489b16d0, 128, 0, 0, 0) = ? ERESTARTSYS (To be restarted if SA_RESTART is set) <0.072574>
--- SIGWINCH {si_signo=SIGWINCH, si_code=SI_KERNEL} ---
recvfrom(3, 0x7ffc489b16d0, 128, 0, 0, 0) = ? ERESTARTSYS (To be restarted if SA_RESTART is set) <0.033758>
--- SIGWINCH {si_signo=SIGWINCH, si_code=SI_KERNEL} ---
recvfrom(3, 0x7ffc489b16d0, 128, 0, 0, 0) = ? ERESTARTSYS (To be restarted if SA_RESTART is set) <0.038904>
--- SIGWINCH {si_signo=SIGWINCH, si_code=SI_KERNEL} ---
recvfrom(3, 0x7ffc489b16d0, 128, 0, 0, 0) = ? ERESTARTSYS (To be restarted if SA_RESTART is set) <0.026003>
--- SIGWINCH {si_signo=SIGWINCH, si_code=SI_KERNEL} ---
recvfrom(3, 0x7ffc489b16d0, 128, 0, 0, 0) = ? ERESTARTSYS (To be restarted if SA_RESTART is set) <0.024057>
--- SIGWINCH {si_signo=SIGWINCH, si_code=SI_KERNEL} ---
recvfrom(3, 0x7ffc489b16d0, 128, 0, 0, 0) = ? ERESTARTSYS (To be restarted if SA_RESTART is set) <0.055221>
--- SIGWINCH {si_signo=SIGWINCH, si_code=SI_KERNEL} ---
recvfrom(3, 0x7ffc489b16d0, 128, 0, 0, 0) = ? ERESTARTSYS (To be restarted if SA_RESTART is set) <0.058240>
--- SIGWINCH {si_signo=SIGWINCH, si_code=SI_KERNEL} ---
recvfrom(3, 0x7ffc489b16d0, 128, 0, 0, 0) = ? ERESTARTSYS (To be restarted if SA_RESTART is set) <0.027569>
--- SIGWINCH {si_signo=SIGWINCH, si_code=SI_KERNEL} ---
recvfrom(3, 0x7ffc489b16d0, 128, 0, 0, 0) = ? ERESTARTSYS (To be restarted if SA_RESTART is set) <0.056877>
--- SIGWINCH {si_signo=SIGWINCH, si_code=SI_KERNEL} ---
recvfrom(3, 0x7ffc489b16d0, 128, 0, 0, 0) = ? ERESTARTSYS (To be restarted if SA_RESTART is set) <0.025934>
--- SIGWINCH {si_signo=SIGWINCH, si_code=SI_KERNEL} ---
recvfrom(3, 0x7ffc489b16d0, 128, 0, 0, 0) = ? ERESTARTSYS (To be restarted if SA_RESTART is set) <0.076699>
--- SIGWINCH {si_signo=SIGWINCH, si_code=SI_KERNEL} ---
recvfrom(3, 0x7ffc489b16d0, 128, 0, 0, 0) = ? ERESTARTSYS (To be restarted if SA_RESTART is set) <0.089092>
--- SIGWINCH {si_signo=SIGWINCH, si_code=SI_KERNEL} ---
recvfrom(3, 0x7ffc489b16d0, 128, 0, 0, 0) = ? ERESTARTSYS (To be restarted if SA_RESTART is set) <0.254680>
--- SIGWINCH {si_signo=SIGWINCH, si_code=SI_KERNEL} ---
recvfrom(3, 0x7ffc489b16d0, 128, 0, 0, 0) = ? ERESTARTSYS (To be restarted if SA_RESTART is set) <0.131634>
--- SIGWINCH {si_signo=SIGWINCH, si_code=SI_KERNEL} ---
recvfrom(3, 0x7ffc489b16d0, 128, 0, 0, 0) = ? ERESTARTSYS (To be restarted if SA_RESTART is set) <0.065721>
--- SIGWINCH {si_signo=SIGWINCH, si_code=SI_KERNEL} ---
recvfrom(3, 0x7ffc489b16d0, 128, 0, 0, 0) = ? ERESTARTSYS (To be restarted if SA_RESTART is set) <0.042778>
--- SIGWINCH {si_signo=SIGWINCH, si_code=SI_KERNEL} ---
recvfrom(3, 0x7ffc489b16d0, 128, 0, 0, 0) = ? ERESTARTSYS (To be restarted if SA_RESTART is set) <0.072277>
--- SIGWINCH {si_signo=SIGWINCH, si_code=SI_KERNEL} ---
recvfrom(3, 0x7ffc489b16d0, 128, 0, 0, 0) = ? ERESTARTSYS (To be restarted if SA_RESTART is set) <0.044424>
--- SIGWINCH {si_signo=SIGWINCH, si_code=SI_KERNEL} ---
recvfrom(3, 0x7ffc489b16d0, 128, 0, 0, 0) = ? ERESTARTSYS (To be restarted if SA_RESTART is set) <0.080704>
--- SIGWINCH {si_signo=SIGWINCH, si_code=SI_KERNEL} ---
recvfrom(3, 0x7ffc489b16d0, 128, 0, 0, 0) = -1 ECONNRESET (Connection reset by peer) <113.777721>

以下是/etc/hosts的内容:

127.0.0.1 localhost

# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts
127.0.1.1 homestead homestead

/etc/resolve.conf的内容:

# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
#     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver 10.0.2.3

问题现在影响了我所拥有的每个无关的流浪者盒子,而不仅仅是Homestead盒子。对于每个CLI执行,几乎每个盒子上的每个PHP命令都会停止5-15分钟。如果必须调用一系列命令,则可能需要一个小时才能完成一个需要30秒的过程。

这是在Mac之后开始运行这些盒子升级为El Capitan。

取决于流浪者的盒子,有时这个strace行:

connect(3, {sa_family=AF_INET, sin_port=htons(9000), sin_addr=inet_addr("127.0.0.1")}, 16) = 0

替换为:

connect(3, {sa_family=AF_INET, sin_port=htons(9000), sin_addr=inet_addr("192.168.56.1")}, 16) = 0

IP 192.168.56.1似乎是VirtualBox的默认路由器。

请注意,所有流浪盒都是标准配置或在我的其他团队成员的Mac / Windows系统上无问题的配置。

Vagrant 1.7.4和VirtualBox 4.3.30。

响应请求route -n

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.0.2.2        0.0.0.0         UG    0      0        0 eth0
10.0.2.0        0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.10.0    0.0.0.0         255.255.255.0   U     0      0        0 eth1

sudo netstat -tulnp | grep 9000的结果:

tcp6       0      0 :::9000                 :::*                    LISTEN      1317/hhvm

为什么hhvm出现了,我不知道,因为该框应该使用标准的PHP解释器。

3 个答案:

答案 0 :(得分:0)

问题似乎与主机名解析有关。如果你正在尝试使用你的ip而不是localhost ip 127.0.0.1。更好地在/etc/hosts中使用,如:

yourip  hostname.example.com hostname

例如

10.0.2.20 test.example.com test

并移除与localhost相关的/etc/hosts或最后保留。系统将尝试进入第一个条目,如果您没有使用ipv6,那么您也可以删除ipv6相关条目。

修改 您的/etc/resolv.conf文件应该低于行

nameserver 127.0.0.1

还添加了vagrant默认网络,因为您的输出显示它尝试连接但是如果您不想使用,那么您可以跳过。您使用的是127.0.0.1,因此至少它应该在那里。您可以从herehere获取帮助。

答案 1 :(得分:0)

tl; dr :我责怪xdebug客户端。尝试在Ubuntu上禁用xdebug。

我的理由

我没有在strace日志中看到主机解析有任何问题。

首先检查/etc/resolv.conf,然后检查/etc/hosts并连接到127.0.0.1:9000 connect(3, {sa_family=AF_INET, sin_port=htons(9000), sin_addr=inet_addr("127.0.0.1")}, 16) = -1 EINPROGRESS (Operation now in progress) <0.000094>

端口9000是xdebug的默认设置,除非在/etc/php5/mods-available/xdebug.ini或类似内容中重新定义。

write(3<socket:[78362]>, "478\0<?xml version=\"1.0\" encoding"..., 483) = 483 <0.000042>看起来像是从xdebug扩展程序发送到客户端的消息。

它等待2分钟recvfrom(3, 0x7ffc489b16d0, 128, 0, 0, 0) = ? ERESTARTSYS,然后放弃并执行你的php脚本。

自v2.2.4以来,等待时间已减少到200毫秒:https://github.com/xdebug/xdebug/pull/90

假设禁用xdebug可以解决问题,几乎没有选项:

  • 升级xdebug
  • 将xdebug配置为仅使用xdebug.remote_autostart=off
  • 按需启动
  • 继续启用/禁用xdebug
  • 始终打开xdebug客户端并确保端口已正确映射且未挂起

答案 2 :(得分:0)

这感觉就像DNS问题。我的猜测是你的机器无法通过给定的主机名解析DNS,或者DNS服务器只是很慢。我的建议是在本地安装缓存DNS服务器,然后使用它,并将/ etc / hosts文件添加到专用网络中的任何内容,这些文件将不会从DNS提供。使用NetworkManager或您必须添加127.0.0.1作为可行(和首选)DNS服务器的网络服务,事情应该顺利进行。如果您使用的是ubuntu,那应该像sudo apt-get install bind9一样简单。

如果你想诊断问题,首先安装dnsutils,以获取dig和nslookup,并尝试查询某些内容,例如www.google.co.uk,查看需要多长时间以及哪个服务器具有权威性。然后尝试使用dig,更具体地说,直接询问服务器,直接询问SOA,看看你是否可以找出最薄弱环节的位置。

这可能是一台路由器通过DHCP将自己宣传为DNS服务,但在按下时无法正常工作。