Linux 里的 2>&1 究竟是什么
发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,经常有这样的需求,写一个python文件,之后把这个python做成定时任务形式,按要求定时crontab执行!而定时任务经常看到如下格式,譬如我下面这个每三分钟执行一次,而且把python的输出信息
千家信息网最后更新 2026年11月01日Linux 里的 2>&1 究竟是什么
经常有这样的需求,写一个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/shtdate~~~~"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]#
命令
星期
文件
标准
输出
测试
常用
屏幕
任务
含义
错误
空设
输入
信息
只有
就是
形式
指令
日志
时候
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
百单网互联网科技有限公司怎么样
中关村互联网科技集团
互联网扶贫科技
青岛互联网科技公司
狮子座互联网科技培训
山东吉时兔互联网科技有限公司
互联网实体化科技化
互联网科技与管理
南平创想时代互联网科技有限公司
花神(南京)互联网科技有限公司
和信科技工业互联网
青海科技产业互联网发展
晋城互联网科技
徐州市互联网科技有限公司
互联网时代的科技公司
黑云互联网科技有限公司
互联网科技快讯热点
福州德业互联网科技
优雇互联网科技有限公司
恒生科技互联网指数十大成分股
互联网黑科技
奇士达互联网科技
互联网科技缩写
互联网科技视频素材
互联网科技有限公司 翻译
江苏南京365互联网科技
中国互联网十大科技股排名
科技互联网海报
互联网科技股为什么大涨
现代互联网科技公司