现在的位置: 首页 > 综合 > 正文

/dev/null 2>&1 详解

2018年03月30日 ⁄ 综合 ⁄ 共 1060字 ⁄ 字号 评论关闭
今天在linux上运行crontab -e,看到如下内容:
*/30 * * * * /bin/bash /usr/local/i386/comm_repos/auto/etc_syslog-ng_syslog-ng.conf_and_logrotate.sh > /dev/null 2>&1
*/5 * * * * /bin/bash /usr/local/i386/comm_repos/monitor/empty_disk.sh > /dev/null 2>&1
01 */1 * * * /bin/bash /usr/local/i386/comm_repos/monitor/sar.sh > /dev/null 2>&1
*/1 * * * * /bin/bash /usr/local/i386/comm_repos/monitor/slab.sh > /dev/null 2>&1
*/1 * * * * /bin/bash /usr/local/i386/comm_repos/monitor/monitor_slab.sh 100 > /dev/null 2>&1
*/15 * * * * /bin/bash /usr/local/i386/comm_repos/auto/system_router.sh > /dev/null 2>&1
55 7 * * * /bin/bash /usr/local/i386/comm_repos/auto/collect_server_info_schedule.sh > /dev/null 2>&1
一头雾水,不知/dev/null 2>&1表示什么意思,于是百度一下,发现 :这条命令的意思是将标准输出和错误输出全部重定向到/dev/null中,也就是将产生的所有信息丢去。
而且还在存在两种不同的写法: command > file 2>filecommand > file 2>&1 。
(1)command > file 2>file 的意思是将命令所产生的标准输出和错误输出送到file 中,即stdoutstderr都直接送到file中, file会被打开两次,这样stdoutstderr会互相覆盖,这样写相当使用了FD1和FD2管道。
(2)command >file 2>&1 这条命令就将stdout直接送向file,
stderr 继承了FD1管道后,再被送往file,此时,file 只被打开了一次,也只使用了一个管道FD1,它包括了stdoutstderr的内容.
从IO效率上,前一条命令的效率要比后面一条的命令效率要低,所以在编写shell脚本的时候,建议用command > file 2>&1 这样的写法.

抱歉!评论已关闭.