资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Linux里的2>&1究竟是什么-创新互联

经常有这样的需求,写一个python文件,之后把这个python做成定时任务形式,按要求定时crontab执行!

创新互联公司是一家专注于网站建设、成都网站设计与策划设计,江城网站建设哪家好?创新互联公司做网站,专注于网站建设十年,网设计领域的专业建站公司;建站业务涵盖:江城等地区。江城做网站价格咨询:18980820575

而定时任务经常看到如下格式,譬如我下面这个每三分钟执行一次,而且把python的输出信息每次记录到log日志里面!常用的是

[root@nessus allpython2019]# crontab -e */3 * * * * /usr/bin/python /root/allpython2019/1-FtpSwDownloadcfg20191101.py >> /root/allpython2019/run.log 2>&1 [root@nessus allpython2019]#

那么这个2>&1 究竟是什么呢?

其实网上很多博文都解释了,如下:

run.log 2>&1 含义参考https://blog.csdn.net/liupeifeng3514/article/details/79711694

解答:command > a  2>&1 【command >> a  2>&1】这条命令,可以理解为执行 command 产生的标准输入重定向到文件 a 中,标准错误也重定向到文件 a 中!(a可以是文件run.log或者/dev/null 空设备文件)【如果不这样写 可能只有标准输出打印到log文件去了  而stderr并没有被重定向到log中,stderr被打印到了屏幕上】

下面是我的测试:

(1)测试不加入2>&1 stderr并没有被重定向到log中,stderr被打印到了屏幕上

(2)测试加入2>&1 stderr也被重定向到log中了

(3)测试常用定时crontab执行时候command > a  2>&1 和command >> a  2>&1这2条命令区别,即一个覆盖!一个是追加!我常用追加!

几个基本符号及其含义: /dev/null 表示空设备文件; 0 表示stdin标准输入; 1 表示stdout标准输出; 2 表示stderr标准错误。 例如我写了下面这个测试程序【这里我们弄了两条命令,其中t指令并不存在,执行会报错,会输出到stderr。date能正常执行,执行会输出当前时间,会输出到stdout。】 [root@nessus allpython2019]# vim test.sh #!/bin/sh t date ~ ~ ~ ~ "test.sh" [新] 3L, 17C 已写入                                                          [root@nessus allpython2019]# chmod +x test.sh [root@nessus allpython2019]# ./test.sh > res1.log ./test.sh:行2: t: 未找到命令              #--------可以看到不加入2>&1 stderr并没有被重定向到log中,stderr被打印到了屏幕上 [root@nessus allpython2019]#  [root@nessus allpython2019]# cat res1.log  2019年 11月 01日 星期五 10:01:47 CST [root@nessus allpython2019]#  [root@nessus allpython2019]#  [root@nessus allpython2019]# ./test.sh > res2.log 2>&1 #--------可以看到加入2>&1 stderr也被重定向到log中了 [root@nessus allpython2019]#  [root@nessus allpython2019]#  [root@nessus allpython2019]# cat res2.log  ./test.sh:行2: t: 未找到命令 2019年 11月 01日 星期五 10:03:08 CST [root@nessus allpython2019]#  [root@nessus allpython2019]#  [root@nessus allpython2019]# ./test.sh > res2.log 2>&1  #---------------下面演示我们常用的>和>>区别,就是一个覆盖!一个是追加! [root@nessus allpython2019]# cat res2.log  ./test.sh:行2: t: 未找到命令 2019年 11月 01日 星期五 10:03:24 CST [root@nessus allpython2019]#  [root@nessus allpython2019]#  [root@nessus allpython2019]# ./test.sh >> res2.log 2>&1 [root@nessus allpython2019]# cat res2.log  ./test.sh:行2: t: 未找到命令 2019年 11月 01日 星期五 10:03:24 CST ./test.sh:行2: t: 未找到命令 2019年 11月 01日 星期五 10:03:40 CST [root@nessus allpython2019]# ./test.sh >> res2.log 2>&1 [root@nessus allpython2019]# cat res2.log  ./test.sh:行2: t: 未找到命令 2019年 11月 01日 星期五 10:03:24 CST ./test.sh:行2: t: 未找到命令 2019年 11月 01日 星期五 10:03:40 CST ./test.sh:行2: t: 未找到命令 2019年 11月 01日 星期五 10:03:44 CST [root@nessus allpython2019]#  [root@nessus allpython2019]#  [root@nessus allpython2019]#

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


分享名称:Linux里的2>&1究竟是什么-创新互联
当前路径:http://cdkjz.cn/article/jghsg.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220