Linux 里的 2>&1 究竟是什么
admin
2023-03-01 08:01:51
0

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

而定时任务经常看到如下格式,譬如我下面这个每三分钟执行一次,而且把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]#


相关内容

热门资讯

今日重大通报“微乐安庆麻将.有... 网上科普关于“微乐安庆麻将有没有挂”话题很是火热,小编也是针对微乐安庆麻将作*弊开挂的方法以及开挂对...
多家优衣库门店,被曝偷拍 近日,优衣库部分门店被指“偷拍”消费者来防盗。在社交媒体上,部分消费者反映,在优衣库因“被当小偷”引...
玩家最新攻略“红中麻将.怎么开... 有 亲,根据资深记者爆料红中麻将是可以开挂的,确实有挂(咨询软件无需打开...
最新引进“授权大厅炸/金/花.... 最新引进“授权大厅炸/金/花.辅助器?”果然有透视挂您好,授权大厅炸/金/花这个游戏其实有挂的,确实...
现货黄金首次站上4400美元/... 【大河财立方消息】 北京时间12月22日午间,现货黄金首次站上4400美元/盎司,今年以来累涨近68...
今日重大消息“同城游贵阳捉鸡麻... 家人们!今天小编来为大家解答同城游贵阳捉鸡麻将透视挂怎么安装这个问题咨询软件客服徽4282891的挂...
MiniMax通过港交所聆讯,... 2025年12月21日晚,国内大模型公司稀宇科技MiniMax(以下简称:MiniMax)在港交所公...
科创合肥,亮出新招 作 者:风铃 来 源:正和岛(ID:zhenghedao) 最近这一周,合肥在科创圈内频频刷屏。 具...
终于明白“花花生活圈.是不是有... 有 亲,根据资深记者爆料花花生活圈是可以开挂的,确实有挂(咨询软件无需打...
重磅消息“一喜棋牌.可以开挂吗... 网上科普关于“一喜棋牌有没有挂”话题很是火热,小编也是针对一喜棋牌作*弊开挂的方法以及开挂对应的知识...