..

不为人知的echo黑魔法

今天干了件大事:把测试环境的机器搞死机了。事情的发展顺序是这样的:有人向我反馈jenkins编译速度特别慢,我打开jenkins一看,好多任务都没有执行完,打开jenkins的过程是非常缓慢,想到早上仿真环境网络的问题,我下意识的认为是今天的网络太慢了。我登录baochai试一下,发现登录服务竟然需要10秒钟之久,ls回显也很慢。这个时候在钉钉上联系运维的同学,我说baochai的网络特别慢,运维的同学去机房找原因,半小时未果,突然钉钉里有测试的同学发了一条消息,说baochai的cpu占用非常高,这个时候运维的同学在群里发了一张截图,说是有条bash命令占用了83G的内存,服务器濒临司机。于是赶紧kill了这个进程。一切恢复平静。大家都在讨论那条命令是在干什么,他们当时没有看就Kill了。后来我想起来了,我上午在服务器上执行了一条echo命令,当时卡死了,ctrl+c也无法退出,于是我关闭了当前的iterm2会话窗口,没有当一回事,我没有想到我退出登录以后echo经常竟然没有自动退出,并且造成了这么大的影响。

echo [{"id":188,"cat":"java","user":0,"name":"websocket-thrift-service","code":"websocket-thrift-service","jdk":"1.8","create_time":"2018-02-02 09:52:23","update_time":"2018-03-30 18:03:39"},{"id":117,"cat":"go","user":0,"name":"日志系统agent","code":"plat-log-agent","jdk":"","create_time":"2017-09-28 19:22:32","update_time":"2018-03-21 18:02:37"},{"id":204,"cat":"go","user":0,"name":"jaeger-agent","code":"jaeger-agent","jdk":"","create_time":"2018-03-27 14:01:21","update_time":"2018-03-27 14:01:21"},{"id":120,"cat":"tomcat","user":0,"name":"deploy_tmcat_test","code":"deploy_tomcat_test","jdk":"1.7","create_time":"2017-09-29 16:16:16","update_time":"2017-09-29 20:19:18"},{"id":102,"cat":"java","user":0,"name":"transferhook-service","code":"transferhook-service","jdk":"1.7","create_time":"2017-09-28 19:22:31","update_time":"2018-02-08 09:58:36"},{"id":183,"cat":"go","user":0,"name":"plat-log-agent-new","code":"plat-log-agent-new","jdk":"","create_time":"2018-01-30 19:08:50","update_time":"2018-01-30 19:56:20"},{"id":66,"cat":"java","user":0,"name":"carbooking_queue","code":"carbooking_queue","jdk":"1.7","create_time":"2017-09-28 19:22:30","update_time":"2017-09-29 09:35:52"}]

在我看来这是条在普通不过的echo命令了,我的本意是重定向这条json到underscore,格式化这条json。没想到会造成服务器司机。这里的原因是没有给字符串加上引号,造成了自动的排列组合。他的原理如下:

'->$ echo [{1,2,3},{a,b,c}]                                            
[1,a] [2,a] [3,a] [1,b] [2,b] [3,b] [1,c] [2,c] [3,c]