小心grep 的buffer -- Linux -- IT技术博客大学习 -- 共学习 共进步!
source link: https://blogread.cn/it/article/4827?f=hot1
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
小心grep 的buffer
在统计mysql查询次数时,有如下命令:
while :; do sleep 1; mysql -u user -ppass -h phpor.net -P 3306 -e 'show status like "Questions";'; done |grep Ques | awk '{print $2 - old; old = $2; }'
几秒钟后都看不到结果,怀疑是awk的buffer造成的,于是改写为:
while :; do sleep 1; mysql -u user -ppass -h phpor.net -P 3306 -e 'show status like "Questions";'; done |grep Ques | awk '{print $2 - old; old = $2; fflush();}'
依然不行,于是,把该命令推到后台,使用strace跟踪了一下awk,发现awk没有读到内容,很自然地,跟踪grep吧,发现grep read到内容了,但是总是不write,开始怀疑我的 Ques写错了,仔细比对,也没有问题呀,突然,屏幕上打印出了很多信息,于是,我恍然大悟,应该是grep的buffer搞的鬼,于是man了一下grep,果然有这方面的选项,再次修改如下:
while :; do sleep 1; mysql -u user -ppass -h phpor.net -P 3306 -e 'show status like "Questions";'; done |grep --line-buffered Ques | awk '{print $2 - old; old = $2; fflush();}'
终于OK了
Recommend
-
2
VIM 设置缩进 -- Linux -- IT技术博客大学习 -- 共学习 共进步! 您现在的位置:首页 -->
-
4
您现在的位置:首页 --> MySQL --> Mysql长连接 Mysql长连接 浏览:2650次...
-
2
wget 的使用 浏览:4088次 出处信息 Wget 的使用 1)支持断点下传功能(2)同时支持FTP和HTTP下载方...
-
7
您现在的位置:首页 --> MySQL --> Mysql where vs having Mysql where vs having
-
3
nginx的配置文件 浏览:8265次 出处信息 记录一下,省得每次都到处找。几个关键地方都有注释,基本看得...
-
3
TCP 的那些事儿(下) 浏览:4808次 出处信息 这篇文章是下篇,所以如果你对TCP不熟悉的话,还请你先看...
-
4
您现在的位置:首页 --> 互联网 --> 杀毒软件 免费的! 杀毒软件 免费...
-
6
HTTP幂等性概念和应用 浏览:3904次 出处信息 [ 感谢
-
4
Linux一些页的东西 浏览:1224次 出处信息 在Linux世界里,分为Page cache,Buffer cache两个层面...
-
6
VIM常用指令 浏览:2964次 出处信息 用了好几年VIM 也从来没怎么认真的用过 ...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK