[很少遇到]How to catch curl errors in PHP,怎么捕获PHP里的Curl出现的超时发生的异常错误?
Php/Js/Shell/Go jack 2013-6-13 09:31
背景:用try catch去捕获php的curl错误,是不行的,
The PHP cURL functions do not throw exceptions, so there will never be anything to catch. You'll just have to check the return value for a boolean false.
As to why the cURL timeout does not seem to be occurring after 30 seconds, I'm not sure.
上面代码返回:
error:Could not resolve host: 12122222.com; Host not found
打开注释代码返回:
Fatal error: Uncaught exception 'Exception' with message 'Curl Has Some Wrong ,Please Check It。' in D:\wamp\www\curl_try_catch.php on line 14
Exception: Curl Has Some Wrong ,Please Check It。 in D:\wamp\www\curl_try_catch.php on line 14
---
返回一些状态码,URl如下:curl.haxx.se/libcurl/c/libcurl-errors.html
再进行捕获。
来自:
http://stackoverflow.com/questions/3987006/how-to-catch-curl-errors-in-php
You can use the curl_error() function to know if there was some error, example:
if(curl_errno($c))
{
echo 'error:' . curl_error($c);
}
===================================================
I'm trying to debug this cURL-operation. The var_dump() is returning bool(false)
How can I make it exit the try in that case?
Why not try throwing an Exception when false is returned?
Look here: php.net/manual/en/function.curl-exec.php; basically, false means the operation failed.
Found it: echo curl_error($ch); "Couldn't connect to host"
http://stackoverflow.com/questions/7069822/curl-try-catch-problem
群讨论:
回忆未来-向东-Jàck(372647***) 上午 10:49:40
PHP try catch 能抓到Curl的timeout吗
一周工作五天(243801***) 上午 10:50:58
不能
回忆未来-向东-Jàck(372647***) 上午 10:52:15
常规处理办法是?
三德子(277940***) 上午 10:52:31
curl没有抛异常 除非你去做个errorhandler
回忆未来-向东-Jàck(372647***) 上午 10:52:39
前一段时间我写过博文,但没有根治:http://www.jackxiang.com/post/6445/
errorhandler ?
有示例demo么。
三德子(277940***) 上午 10:53:02
在errorhandler中 抛errorexception
看手册 errorexception这一章
回忆未来-向东-Jàck(372647***) 上午 10:53:48
嗯,超时捕获有code值没?
三德子(277940***) 上午 10:53:57
一周工作五天(243801***) 上午 10:55:41
搞那么复杂干嘛 直接把 超时时间设置长些不就ok了
----------------------------------------------------------------------------------------------
强制用try catch作下实践如下:
访问:http://jackxiang.com/test/testTimeOut
Max Time Out...time out。
Error fetching remote http://jackxiang.com/test/testMaxTimeUrl [ status 0 ]
Error fetching remote http://jackxiang.com/test/testMaxTimeUrl [ status 0 ]
Operation timed out after 5007 milliseconds with 0 bytes received
其实上述说明,不在curl里捕获,在外面一层也是能捕捉到的,只是错误的message提示是:Error:Operation timed out after 5007 milliseconds with 0 bytes received!
________________________________________________________________________________________________
在生产中的代码,用try catch实现的Remote:get,来自KO框架,作了下简单修改(有时Url为空,不知是谁调的,Add Time:2014/03/26):
The PHP cURL functions do not throw exceptions, so there will never be anything to catch. You'll just have to check the return value for a boolean false.
As to why the cURL timeout does not seem to be occurring after 30 seconds, I'm not sure.
上面代码返回:
error:Could not resolve host: 12122222.com; Host not found
打开注释代码返回:
Fatal error: Uncaught exception 'Exception' with message 'Curl Has Some Wrong ,Please Check It。' in D:\wamp\www\curl_try_catch.php on line 14
Exception: Curl Has Some Wrong ,Please Check It。 in D:\wamp\www\curl_try_catch.php on line 14
---
返回一些状态码,URl如下:curl.haxx.se/libcurl/c/libcurl-errors.html
再进行捕获。
来自:
http://stackoverflow.com/questions/3987006/how-to-catch-curl-errors-in-php
You can use the curl_error() function to know if there was some error, example:
if(curl_errno($c))
{
echo 'error:' . curl_error($c);
}
===================================================
I'm trying to debug this cURL-operation. The var_dump() is returning bool(false)
How can I make it exit the try in that case?
Why not try throwing an Exception when false is returned?
Look here: php.net/manual/en/function.curl-exec.php; basically, false means the operation failed.
Found it: echo curl_error($ch); "Couldn't connect to host"
http://stackoverflow.com/questions/7069822/curl-try-catch-problem
群讨论:
回忆未来-向东-Jàck(372647***) 上午 10:49:40
PHP try catch 能抓到Curl的timeout吗
一周工作五天(243801***) 上午 10:50:58
不能
回忆未来-向东-Jàck(372647***) 上午 10:52:15
常规处理办法是?
三德子(277940***) 上午 10:52:31
curl没有抛异常 除非你去做个errorhandler
回忆未来-向东-Jàck(372647***) 上午 10:52:39
前一段时间我写过博文,但没有根治:http://www.jackxiang.com/post/6445/
errorhandler ?
有示例demo么。
三德子(277940***) 上午 10:53:02
在errorhandler中 抛errorexception
看手册 errorexception这一章
回忆未来-向东-Jàck(372647***) 上午 10:53:48
嗯,超时捕获有code值没?
三德子(277940***) 上午 10:53:57
一周工作五天(243801***) 上午 10:55:41
搞那么复杂干嘛 直接把 超时时间设置长些不就ok了
----------------------------------------------------------------------------------------------
强制用try catch作下实践如下:
访问:http://jackxiang.com/test/testTimeOut
Max Time Out...time out。
Error fetching remote http://jackxiang.com/test/testMaxTimeUrl [ status 0 ]
Error fetching remote http://jackxiang.com/test/testMaxTimeUrl [ status 0 ]
Operation timed out after 5007 milliseconds with 0 bytes received
其实上述说明,不在curl里捕获,在外面一层也是能捕捉到的,只是错误的message提示是:Error:Operation timed out after 5007 milliseconds with 0 bytes received!
________________________________________________________________________________________________
在生产中的代码,用try catch实现的Remote:get,来自KO框架,作了下简单修改(有时Url为空,不知是谁调的,Add Time:2014/03/26):
POST的原始数据获得方法
Php/Js/Shell/Go jack 2013-6-11 19:44
Smooth Gestures:
http://www.smoothgestures.com/thankyou/#
http://www.smoothgestures.com/thankyou/#
我已经解决了这个问题,自己探索的,这个进程就是三星自带的桌面程序,实在不想刷机的话就保持在开机状态,然后连接电脑,安装PC助手或者魔乐这种同步软件,然后进去找个替代的桌面软件(推荐go桌面或者adw laucher)安装一下,装好了按home键选择安装后的软件。方法是:先想办法找个替代桌面,然后进入设置,应用程序,管理应用程序,选择全部,找到 “主屏幕”这个程序,点进去会看到里面有数据(就是桌面的设置),选择清除数据,并强行停止。之后再从替代桌面切换会原桌面即可。也许需要重启一下。就是这样简单。。。
下载一个第三方主屏幕,ADW主屏幕,也就是ADW.Launcher 安装就搞定了。什么?用电脑下了,可是USB连不上。。。。。。刚才我说了目前用眼疾手快法可以用的手机功能:下拉界面,接打电话,接发短信,长按中键的任务管理器和最近使用的任务,不能上网啊。好了,问题的关键就在这里,大家想想:可以接短信哦,可以看短信哦,可以看手机报哦,手机报最后一页有网址的链接哦,链接是可以点的哦,这样你就可以上网了。然后你就懂了=。=、
就算没有手机报,让朋友发你个带链接的信息就ok了。
然后下载个ADW.Launcher一安装,你就可以用了。
然后又有孩纸不愿意了,还是想用原来的,当然没问题。
换主题后进设置-应用程序-管理应用程序-全部
找到主屏幕项
点强行停止
再点清除数据就ok了
退出
点home键
会弹出主题选择窗口
选主屏幕就好了
起死回生了有木有,轻松搞定了有木有。
以上,旋律对三星手机 主屏幕(进程:com.sec.android.app.twlauncher)意外停止的解决方法
希望对你们有帮助!
什么?还有去手机维修店花钱才修好的=。=、
链接:http://hi.baidu.com/zx707/item/de2b992e2eaef9c5ee10f19f
附录:
touchwiz4.5是三星为其手机定制的桌面启动器,和HTC Sense一样,都是从WM时代便有的事物。如今国外神站xda-developer论坛的两位大神Fr4gg0r和Swiftwork制作了TouchWiz 4.5的修改版,非三星Android手机也能安装。
该修改版除了在原版的基础上做了优化之外,还新增了大量个性化设置,如自定义应用程序菜单图标数量、自定义快捷方式数量、炫酷3D效果等,与知名的桌面增强软件LauncherPro相比毫不逊色。 有朋友不知道怎么新增托盘图标,请看我的第三附图。如需下载主题请去我上个帖子。
下载一个第三方主屏幕,ADW主屏幕,也就是ADW.Launcher 安装就搞定了。什么?用电脑下了,可是USB连不上。。。。。。刚才我说了目前用眼疾手快法可以用的手机功能:下拉界面,接打电话,接发短信,长按中键的任务管理器和最近使用的任务,不能上网啊。好了,问题的关键就在这里,大家想想:可以接短信哦,可以看短信哦,可以看手机报哦,手机报最后一页有网址的链接哦,链接是可以点的哦,这样你就可以上网了。然后你就懂了=。=、
就算没有手机报,让朋友发你个带链接的信息就ok了。
然后下载个ADW.Launcher一安装,你就可以用了。
然后又有孩纸不愿意了,还是想用原来的,当然没问题。
换主题后进设置-应用程序-管理应用程序-全部
找到主屏幕项
点强行停止
再点清除数据就ok了
退出
点home键
会弹出主题选择窗口
选主屏幕就好了
起死回生了有木有,轻松搞定了有木有。
以上,旋律对三星手机 主屏幕(进程:com.sec.android.app.twlauncher)意外停止的解决方法
希望对你们有帮助!
什么?还有去手机维修店花钱才修好的=。=、
链接:http://hi.baidu.com/zx707/item/de2b992e2eaef9c5ee10f19f
附录:
touchwiz4.5是三星为其手机定制的桌面启动器,和HTC Sense一样,都是从WM时代便有的事物。如今国外神站xda-developer论坛的两位大神Fr4gg0r和Swiftwork制作了TouchWiz 4.5的修改版,非三星Android手机也能安装。
该修改版除了在原版的基础上做了优化之外,还新增了大量个性化设置,如自定义应用程序菜单图标数量、自定义快捷方式数量、炫酷3D效果等,与知名的桌面增强软件LauncherPro相比毫不逊色。 有朋友不知道怎么新增托盘图标,请看我的第三附图。如需下载主题请去我上个帖子。
解决 /bin/rm: Argument list too long
Unix/LinuxC技术 jack 2013-6-9 14:39
背景:前两天对外包的PHP返回慢作了一个xdebug的统计,后来尽管加上了那个rm的crontab,但是后来发现日志依旧在,还是运维发现,上了30G了。
后来查了下crontab的日志文件,发现:
配置:
一:Crontab定时删除,1小时删除一次:
三分钟一清,试过一小时一清,都3G左右大小了:
二:应该是生成的xdebug日志文件太多了造成的(1小时一次有点长了),这个文件删除不了:
出现:Argument list too long。
三:网上找了下,摘录如下:
cache目录,谁知道输入
rm *
的命令后,大概几秒后屏幕就跳出
/bin/rm: argument list too long
条件反射动作,上百度和google查找,找到解决方法和原因,原因是一个文件夹下文件太多,使用rm删除就会出现/bin/rm: Argument list too long错误.
解决方法:http://hintcnuie.javaeye.com/blog/431354
按网址上面写的输入
ls | xargs -n 10 rm -fr ls
就解决了,这句解释为:输出所有的文件名(用空格分割) xargs就是将ls的输出,每10个为一组(以空格为分隔符),作为rm -rf的参数也就是说将所有文件名10个为一组,由rm -rf删除,这样就不会超过命令行的长度了.
另外找到用脚本的方法:
for loop in `ls 目录路径`
do
rm -f $loop
done
注意:$loop是删除的文件名,确保路径是否正确。
但这个还没有实验过。
来自:http://www.dedecms.com/knowledge/servers/linux-bsd/2012/0614/1538.html
第二种方法:
当某个文件夹下的文件太多,不能使用rm删除
# rm * -rf
-bash: /bin/rm: Argument list too long
网上找了个方法,贴上来给大家分享
find PATH -name *.mp3 -exec rm {} \;
如,要删除 /tmp/123/abc 目录下的
find /tmp/123/abc ‘*’ -exec rm {} \;
如当前目录为 /tmp/123 ,想删除 /tmp/123/abc 下的
find ./abc -name ‘*’ -exec rm {} \;
大家可以自己试试,不过要小心处理,别删错重要文件了!!!
来自:http://linux.chinaitlab.com/administer/805334.html
后来查了下crontab的日志文件,发现:
配置:
一:Crontab定时删除,1小时删除一次:
三分钟一清,试过一小时一清,都3G左右大小了:
二:应该是生成的xdebug日志文件太多了造成的(1小时一次有点长了),这个文件删除不了:
出现:Argument list too long。
三:网上找了下,摘录如下:
cache目录,谁知道输入
rm *
的命令后,大概几秒后屏幕就跳出
/bin/rm: argument list too long
条件反射动作,上百度和google查找,找到解决方法和原因,原因是一个文件夹下文件太多,使用rm删除就会出现/bin/rm: Argument list too long错误.
解决方法:http://hintcnuie.javaeye.com/blog/431354
按网址上面写的输入
ls | xargs -n 10 rm -fr ls
就解决了,这句解释为:输出所有的文件名(用空格分割) xargs就是将ls的输出,每10个为一组(以空格为分隔符),作为rm -rf的参数也就是说将所有文件名10个为一组,由rm -rf删除,这样就不会超过命令行的长度了.
另外找到用脚本的方法:
for loop in `ls 目录路径`
do
rm -f $loop
done
注意:$loop是删除的文件名,确保路径是否正确。
但这个还没有实验过。
来自:http://www.dedecms.com/knowledge/servers/linux-bsd/2012/0614/1538.html
第二种方法:
当某个文件夹下的文件太多,不能使用rm删除
# rm * -rf
-bash: /bin/rm: Argument list too long
网上找了个方法,贴上来给大家分享
find PATH -name *.mp3 -exec rm {} \;
如,要删除 /tmp/123/abc 目录下的
find /tmp/123/abc ‘*’ -exec rm {} \;
如当前目录为 /tmp/123 ,想删除 /tmp/123/abc 下的
find ./abc -name ‘*’ -exec rm {} \;
大家可以自己试试,不过要小心处理,别删错重要文件了!!!
来自:http://linux.chinaitlab.com/administer/805334.html
[实践OK]相当有用之频率测试 curl 时间 CURL一行时间参数/curl获取网络时间访问多次测试网页响应时间的shell脚本,需要Curl支持,Curl测试访问时间,DNS解析时间。
Php/Js/Shell/Go jack 2013-6-6 12:02
背景:一天突然发现访问网站特别的慢,怎么办?原因是啥,不知道,此时,祭出CURL,加上相关的数据输出,特别是对于DNS慢这种问题最难查,居然被我碰到了,time_namelookup 这个参数很重要,DNS解析时间,从请求开始到DNS解析完毕所用时间(记得关掉 Linux 的 nscd 的服务测试),当然,查dns更为直观的是用curl的-H参数(http://jackxiang.com/post/4423/),不用dns解析时的一个指标,再就是用dns解析的一个指标,一对比就出来了,该命令在windows下使用也没问题。
DNS解析时的时间:
5.011:5.011:5.020:5.020:
不用DNS相当于写死Host解析时的办法:
0.003:0.003:0.026:0.026
time_namelookup时间5.011太长了,说明DNS有问题,一查果然有问题,由于此内网DNS解析了单位办工机器,还解析线上服务器内部域名,压力太大导致,为此,新加了一台dns服务器,减轻压力,解析的速度上去了,也就好了。
相关参数解释:
time_connect 建立到服务器的 TCP 连接所用的时间
time_starttransfer 在发出请求之后,Web 服务器返回数据的第一个字节所用的时间
time_total 完成请求所用的时间
time_namelookup DNS解析时间,从请求开始到DNS解析完毕所用时间(记得关掉 Linux 的 nscd 的服务测试)
speed_download 下载速度,单位-字节每秒。(注意它是字节)
[/codes]
来个实例:
加上字节:
time函数实现时间:
加上Shell的for循环,一般主要是打印时间:
粘贴好像会报错,直接给个DownLoad:
下载文件
sh curlExecuteTime.sh -n 12 "http://localhost"
**春(**春) 11:11:56
time_connect: 0.001
time_namelookup: 0
time_total: 0.01419
**春(**春) 11:12:18
这个结果按说是没问题的吧
向东(向东) 11:12:34
没问题,你要想获取接口返回修改下这儿:curl -o /dev/null -s -w \
向东(向东) 11:12:42
它定向到null了。
向东(向东) 11:21:16
c=`curl -o /tmp/$i.txt -s -w \
这样就可以在tmp下生成1.txt 2.txt了,呵呵。
DownLoad:
下载文件
零:可以直接用shell实现,用vim列模式/行模式进行批量输入,当然也可以用shell的for循环,如出现301错误需要输出头-i,如下:
一:PHP下的Curl获取时间 :
二:Shell下的curl原理:
curl -o /dev/null -s -w '连接时间:%{time_connect},传输时间:%{time_starttransfer}:%{time_total}' "http://jackxiang.com"
POST示例(不用-o写入文件,直接看):
用法及返回结果如下:
lvyaozu@lvyaozu-desktop:~/shell$ ./requesturl.sh -n20 http://www.baidu.com
Request url: http://www.baidu.com
Request number: 20
Request Failed: 0
——Average Value——
time_connect: 0.1614
time_total: 0.1693
time_namelookup: 0.0088
————————-
请求参数:
-n 指定发送多少次请求,默认为10
返回结果为 curl -w 选项返回值,以下摘自curl官方文档:
http://curl.haxx.se/docs/manpage.html
time_connect The time, in seconds, it took from the start until the TCP connect to the remote host (or proxy) was completed.
time_total The total time, in seconds, that the full operation lasted. The time will be displayed with millisecond resolution.
time_namelookup The time, in seconds, it took from the start until the name resolving was completed.
代码如下:
我的实践如下:
sh curlExecuteTime.sh -n 12 "http://jackxiang.com/getAlbumsByUId?appkey=100&userid=21240168&sortby=hot"
Request url: hhttp://jackxiang.com/getAlbumsByUId?appkey=100&userid=21240168&sortby=hot
Request number: 12
Request Failed: 0
------Average Value------
time_connect: 0.001
time_namelookup: 0.001
time_total: 0.0760833
-------------------------
来自:http://renxiangzyq.iteye.com/blog/782773
1、开启gzip请求
curl -I http://www.sina.com.cn/ -H Accept-Encoding:gzip,defalte
2、监控网页的响应时间
curl -o /dev/null -s -w "time_connect: %{time_connect}\ntime_starttransfer: %{time_starttransfer}\ntime_total: %{time_total}\n" "http://www.kklinux.com"
3. 监控站点可用性
curl -o /dev/null -s -w %{http_code} "http://www.kklinux.com"
4、以http1.0协议请求(默认为http1.1)
curl -0 ..............
监控站点首页下载时间:
curl -o /dev/null -s -w ‘%{time_total}’ http://www.miotour.com
curl -o /dev/null -s -w ‘%{http_code}’ http://www.miotour.com
curl -o /dev/null -s -w %{http_code}:%{time_connect}:%{time_starttransfer}:%{time_total} http://www.miotour.com
结果:2.547
-s 静默输出;没有-s的话就是下面的情况,这是在脚本等情况下不需要的信息。
[ec2-user@ip-10-122-250-19 ~]$ curl -o /dev/null -w ‘%{time_total}’ http://www.miotour.com
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 67770 0 67770 0 0 19228 0 –:–:– 0:00:03 –:–:– 20705
结果:3.524
监控首页各项时间指标:
curl -o /dev/null -s -w ‘%{time_connect}:%{time_starttransfer}:%{time_total}’ http://www.miotour.com
结果: 0.244: 1.044: 2.672
时间指标解释 :
time_connect 建立到服务器的 TCP 连接所用的时间
time_starttransfer 在发出请求之后,Web 服务器返回数据的第一个字节所用的时间
time_total 完成请求所用的时间
在 发出请求之后,Web 服务器处理请求并开始发回数据所用的时间是
(time_starttransfer)1.044 - (time_connect)0.244 = 0.8 秒
客户机从服务器下载数据所用的时间是
(time_total)2.672 - (time_starttransfer)1.044 = 1.682 秒
指定特定主机IP地址访问网站
curl -x 61.135.169.105:80 http://www.baidu.com
curl -x 61.135.169.125:80 http://www.baidu.com
摘录来自:http://blog.163.com/dingding_jacky/blog/static/166912787201242440274/
DNS解析时的时间:
5.011:5.011:5.020:5.020:
不用DNS相当于写死Host解析时的办法:
0.003:0.003:0.026:0.026
time_namelookup时间5.011太长了,说明DNS有问题,一查果然有问题,由于此内网DNS解析了单位办工机器,还解析线上服务器内部域名,压力太大导致,为此,新加了一台dns服务器,减轻压力,解析的速度上去了,也就好了。
相关参数解释:
time_connect 建立到服务器的 TCP 连接所用的时间
time_starttransfer 在发出请求之后,Web 服务器返回数据的第一个字节所用的时间
time_total 完成请求所用的时间
time_namelookup DNS解析时间,从请求开始到DNS解析完毕所用时间(记得关掉 Linux 的 nscd 的服务测试)
speed_download 下载速度,单位-字节每秒。(注意它是字节)
[/codes]
来个实例:
加上字节:
time函数实现时间:
加上Shell的for循环,一般主要是打印时间:
粘贴好像会报错,直接给个DownLoad:
下载文件 sh curlExecuteTime.sh -n 12 "http://localhost"
**春(**春) 11:11:56
time_connect: 0.001
time_namelookup: 0
time_total: 0.01419
**春(**春) 11:12:18
这个结果按说是没问题的吧
向东(向东) 11:12:34
没问题,你要想获取接口返回修改下这儿:curl -o /dev/null -s -w \
向东(向东) 11:12:42
它定向到null了。
向东(向东) 11:21:16
c=`curl -o /tmp/$i.txt -s -w \
这样就可以在tmp下生成1.txt 2.txt了,呵呵。
DownLoad:
下载文件 零:可以直接用shell实现,用vim列模式/行模式进行批量输入,当然也可以用shell的for循环,如出现301错误需要输出头-i,如下:
一:PHP下的Curl获取时间 :
二:Shell下的curl原理:
curl -o /dev/null -s -w '连接时间:%{time_connect},传输时间:%{time_starttransfer}:%{time_total}' "http://jackxiang.com"
POST示例(不用-o写入文件,直接看):
用法及返回结果如下:
lvyaozu@lvyaozu-desktop:~/shell$ ./requesturl.sh -n20 http://www.baidu.com
Request url: http://www.baidu.com
Request number: 20
Request Failed: 0
——Average Value——
time_connect: 0.1614
time_total: 0.1693
time_namelookup: 0.0088
————————-
请求参数:
-n 指定发送多少次请求,默认为10
返回结果为 curl -w 选项返回值,以下摘自curl官方文档:
http://curl.haxx.se/docs/manpage.html
time_connect The time, in seconds, it took from the start until the TCP connect to the remote host (or proxy) was completed.
time_total The total time, in seconds, that the full operation lasted. The time will be displayed with millisecond resolution.
time_namelookup The time, in seconds, it took from the start until the name resolving was completed.
代码如下:
我的实践如下:
sh curlExecuteTime.sh -n 12 "http://jackxiang.com/getAlbumsByUId?appkey=100&userid=21240168&sortby=hot"
Request url: hhttp://jackxiang.com/getAlbumsByUId?appkey=100&userid=21240168&sortby=hot
Request number: 12
Request Failed: 0
------Average Value------
time_connect: 0.001
time_namelookup: 0.001
time_total: 0.0760833
-------------------------
来自:http://renxiangzyq.iteye.com/blog/782773
1、开启gzip请求
curl -I http://www.sina.com.cn/ -H Accept-Encoding:gzip,defalte
2、监控网页的响应时间
curl -o /dev/null -s -w "time_connect: %{time_connect}\ntime_starttransfer: %{time_starttransfer}\ntime_total: %{time_total}\n" "http://www.kklinux.com"
3. 监控站点可用性
curl -o /dev/null -s -w %{http_code} "http://www.kklinux.com"
4、以http1.0协议请求(默认为http1.1)
curl -0 ..............
监控站点首页下载时间:
curl -o /dev/null -s -w ‘%{time_total}’ http://www.miotour.com
curl -o /dev/null -s -w ‘%{http_code}’ http://www.miotour.com
curl -o /dev/null -s -w %{http_code}:%{time_connect}:%{time_starttransfer}:%{time_total} http://www.miotour.com
结果:2.547
-s 静默输出;没有-s的话就是下面的情况,这是在脚本等情况下不需要的信息。
[ec2-user@ip-10-122-250-19 ~]$ curl -o /dev/null -w ‘%{time_total}’ http://www.miotour.com
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 67770 0 67770 0 0 19228 0 –:–:– 0:00:03 –:–:– 20705
结果:3.524
监控首页各项时间指标:
curl -o /dev/null -s -w ‘%{time_connect}:%{time_starttransfer}:%{time_total}’ http://www.miotour.com
结果: 0.244: 1.044: 2.672
时间指标解释 :
time_connect 建立到服务器的 TCP 连接所用的时间
time_starttransfer 在发出请求之后,Web 服务器返回数据的第一个字节所用的时间
time_total 完成请求所用的时间
在 发出请求之后,Web 服务器处理请求并开始发回数据所用的时间是
(time_starttransfer)1.044 - (time_connect)0.244 = 0.8 秒
客户机从服务器下载数据所用的时间是
(time_total)2.672 - (time_starttransfer)1.044 = 1.682 秒
指定特定主机IP地址访问网站
curl -x 61.135.169.105:80 http://www.baidu.com
curl -x 61.135.169.125:80 http://www.baidu.com
摘录来自:http://blog.163.com/dingding_jacky/blog/static/166912787201242440274/
[接口超时]在Nginx里的PHP输出接口进行strace时发现的超时问题备案,有可能是Mysql Server里域名解析导致。
Php/Js/Shell/Go jack 2013-6-5 18:17
Nginx里有一个PHP的接口,发现是PHP的超时,于是经过strace时发现:
经过xdebug发现它是由于数据库访问这个函数超时引起的,有5秒到15秒之多,但是把这个SQL贴在终端里访问,发现它很快才几毫秒。
用下面的sleep代码也能形成TimeOut,如下:
我在想,是192.168.109.8访问192.168.109.7上中间有时会有波动造成的,还是因为PHP连接时超时呢?需要进一步了解啊。
有一个域名反解的配置把这个去掉:
skip-name-resolve
修改配置文件添加并需要重启:
修改my.cnf的配置文件添加并需要重启,试试:
[mysqld]
skip-name-resolve
感觉通过一个脚本访问好像好了,需要时间验证,脚本:
http://www.jackxiang.com/post/6421/
经过xdebug发现它是由于数据库访问这个函数超时引起的,有5秒到15秒之多,但是把这个SQL贴在终端里访问,发现它很快才几毫秒。
用下面的sleep代码也能形成TimeOut,如下:
我在想,是192.168.109.8访问192.168.109.7上中间有时会有波动造成的,还是因为PHP连接时超时呢?需要进一步了解啊。
有一个域名反解的配置把这个去掉:
skip-name-resolve
修改配置文件添加并需要重启:
修改my.cnf的配置文件添加并需要重启,试试:
[mysqld]
skip-name-resolve
感觉通过一个脚本访问好像好了,需要时间验证,脚本:
http://www.jackxiang.com/post/6421/
nginx keepalive-timeout ,Nginx设置Keep-Alive为close。
Php/Js/Shell/Go jack 2013-6-5 15:55
nginx里的keepalive-timeout选项
此选项说的是可使客户端到服务器端的连接持续有效,当出现对服务器的后继请求时,keepalive-timeout功能可避免建立或重新建立连接。
涉及的选项还有stub_status on,能看到waiting值也和keepalive-timeout设置有关
nginx不像apache,直接有指令keep-alive off/on;它使用的是keepalive_timeout [time],默认的时长为75,可以在http、server、location使用此指令。
在本机进行的模拟测试:
nginx.conf
keepalive_timeout 600;
curl -i Url //i 显示Http头
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 05 Jun 2013 07:57:06 GMT
Content-Type: text/html;charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
Cache-Control: no-cache
Pragma: no-cache
nginx.conf指定的VHOST中添加了规则:
location /gtj/ {
alias C:/phpApp/gtj/;
keepalive_timeout 0;
expires 5m;
}
客户端请求后,可以用httpwatch抓取返回的头部信息:
Http头:
connection close
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 05 Jun 2013 07:59:40 GMT
Content-Type: text/html;charset=utf-8
Transfer-Encoding: chunked
Connection: close
Vary: Accept-Encoding
Cache-Control: no-cache
Pragma: no-cache
阅读全文
此选项说的是可使客户端到服务器端的连接持续有效,当出现对服务器的后继请求时,keepalive-timeout功能可避免建立或重新建立连接。
涉及的选项还有stub_status on,能看到waiting值也和keepalive-timeout设置有关
nginx不像apache,直接有指令keep-alive off/on;它使用的是keepalive_timeout [time],默认的时长为75,可以在http、server、location使用此指令。
在本机进行的模拟测试:
nginx.conf
keepalive_timeout 600;
curl -i Url //i 显示Http头
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 05 Jun 2013 07:57:06 GMT
Content-Type: text/html;charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
Cache-Control: no-cache
Pragma: no-cache
nginx.conf指定的VHOST中添加了规则:
location /gtj/ {
alias C:/phpApp/gtj/;
keepalive_timeout 0;
expires 5m;
}
客户端请求后,可以用httpwatch抓取返回的头部信息:
Http头:
connection close
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 05 Jun 2013 07:59:40 GMT
Content-Type: text/html;charset=utf-8
Transfer-Encoding: chunked
Connection: close
Vary: Accept-Encoding
Cache-Control: no-cache
Pragma: no-cache
阅读全文
Linux系统密码忘记后的五种恢复方法
Unix/LinuxC技术 jack 2013-6-4 09:48
vim对齐插件——Align
Unix/LinuxC技术 jack 2013-6-1 23:26
https://github.com/tsaleh/vim-align
能满足各种方式的对齐。真心不错呢
能满足各种方式的对齐。真心不错呢




