`

/dev/null 2>&1 解释

 
阅读更多
/dev/null 2>&1 解释
crontab内容 :50 18 5-30 * * /script/myscript.sh 1> /dev/null 2>&1
其中 1> /dev/null 2>&1是什么意思??
dev/null 为系统垃圾箱
&为后台运行
但是 myscript 后面的1 和 /null后面的2 还有 &后面的1是什么意思?
1代表标准输出,2代表错误信息输出.
1>/dev/null 就是指将标准输出定向到空设备,
2>&1,的意思是将错误输出定向到和1一样的输出设备,也同样是空.
换句话说,就是不显示该程序执行过程中的任何信息
cmd >a 2>a 和 cmd >a 2>&1 为什么不同?
cmd >a 2>a :stdout和stderr都直接送往文件 a ,a文件会被打开两遍,由此导致stdout和stderr互相覆盖。
cmd >a 2>&1 :stdout直接送往文件a ,stderr是继承了FD1的管道之后,再被送往文件a 。a文件只被打开一遍,就是FD1将其打开
他们的不同点在于:
cmd >a 2>a 相当于使用了FD1、FD2两个互相竞争使用文件 a 的管道;
而cmd >a 2>&1 只使用了一个管道FD1,但已经包括了stdout和stderr。
从IO效率上来讲,cmd >a 2>&1的效率更高。

-------------------------------------

为什么要用 /dev/null 2>&1 这样的写法.这条命令的意思是将标准输出和错误输出全部重定向到/dev/null中,也就是将产生的所有信息丢弃.下面我就为大家来说一下, command > file 2>file   与command > file 2>&1 有什么不同的地方.
       首先~command > file 2>file 的意思是将命令所产生的标准输出信息,和错误的输出信息送到file 中.command   > file 2>file 这样的写法,stdout和stderr都直接送到file中, file会被打开两次,这样stdout和stderr会互相覆盖,这样写相当使用了FD1和FD2两个同时去抢占file 的管道.
       而command >file 2>&1 这条命令就将stdout直接送向file, stderr 继承了FD1管道后,再被送往file,此时,file 只被打开了一次,也只使用了一个管道FD1,它包括了stdout和stderr的内容.
       从IO效率上,前一条命令的效率要比后面一条的命令效率要低,所以在编写shell脚本的时候,较多的时候我们会用command > file 2>&1 这样的写法.
****************************************************
in UNIX
0 = stdin
1 = stdout
2 = stderr

>/dev/null 2>&1 means
redirect all the standard out and standard error messages/output/results from the programs/scripts to /dev/null ( which means they go to a bottomless pit)

*****************************************************

/dev/null 2>&1

You need to understand the theory first and then its upto you how and where you want to apply that theory. I'll try to explain above to you.

The greater-than (>) in commands like these redirect the program’s output somewhere. In this case, something is being redirected into /dev/null, and something is being redirected into &1.

Standard in, out and error:

There are three standard sources of input and output for a program. Standard input usually comes from the keyboard if it’s an interactive program, or from another program if it’s processing the other program’s output. The program usually prints to standard output, and sometimes prints to standard error. These three file descriptors (you can think of them as “data pipes”) are often called STDIN, STDOUT, and STDERR.

Sometimes they’re not named, they’re numbered! The built-in numberings for them are 0, 1, and 2, in that order. By default, if you don’t name or number one explicitly, you’re talking about STDOUT.

That means file descriptor 0 or fd0 denotes STDIN or standard input and file descriptor 1 or fd1 denotes STDOUT or standard output and file descriptor 2 or fd2 denotes STDERR or standard error.

You can see the command above is redirecting standard output into /dev/null, which is a place you can dump anything you don’t want (often called the bit-bucket), then redirecting standard error into standard output (you have to put an & in front of the destination when you do this).

The short explanation, therefore, is “all output from this command should be shoved into a black hole.” That’s one good way to make a program be really quiet!

*********************************************
When in a shell, you can do various things with the output and input to commands. You can run them through a pipe:


grep root /var/log/messages | less


This takes the output of the 'grep' commands, and uses the 'less' paginator to finally display it on the screen.

To go into further detail, this sends the STDOUT of 'grep' to less.

If you wanted to send the output to a file, you'd use a redirect:


grep root /var/log/messages > /tmp/file


This takes the STDOUT, and sticks it into '/tmp/file'. I won't go into detail about the > and >> options etc. as they are all covered in the man page.

There are 3 file descriptors for every process. STDIN, STDOUT and STDERR. These map to 0, 1 and 2 respecitvly.

Strangely enough, the above command can also be written as:


grep root /var/log/messages 1> /tmp/file


The first command makes the assumption that you want to redirect STDOUT. This one clears up that assumption.

Now we'll look at STDERR:


grep root /var/log/mseeagse > /tmp/file


This will spit out an error to your tty, even though you've redirected the output.


grep root /var/log/mseeagse 2> /tmp/file


This one will output nothing, and '/tmp/file' will contain the error. If we didn't miss-spell the messages file name, any errors that might occur would go to the file, but the output will go to the TTY.

Now we'll do the last confusing thing, moving a file descriptor to a different one. This is where the &<number> comes in:


grep root /var/log/mseeagse 2> &1


This moves STDERR so it comes out in the same place as STDOUT. It says "Move file descriptor 2 to file descriptor 1".

Thus where we get to your tail. We mix and match the STDOUT and STDERR redirections:


grep root /var/log/messages > /tmp/file 2> &1
grep root /var/log/mseeagse > /tmp/file 2> &1


With both of these commands, both the regular output (STDOUT), and any errors that occur (STDERR) will go into '/tmp/file'.

When stacking redirects like this, there is one major thing to be aware of. When you move a file descriptor, you have to make sure the descriptor you are moving too has already been redirected to where you want, otherwise you can get some... interesting results.
************************************************************
This is from memory.
Everything in Linux is a file, including I/O. There are three standard file descriptors, Standard In (STDIN, file descriptor 0), Standard Output (STDOUT, file descriptor 1) and Standard Error (STDERR, file descriptor 2). /dev/null is the null device, which is like "write only memory". > will write to the specified file (overwriting its contents) and >> will append to the specified file.

2> redirects STDERR to the specified file. >> is used to append to the end of the file.

& only means to run the process in the background if it appears at the end of the line.

2>&1 redirects STDERR to STDOUT. Since in this case, STDOUT is being redirected to /dev/null, 2>&1 causes both STDERR and STDOUT to /dev/null.
分享到:
评论

相关推荐

    详解nohup /dev/null 2&1 含义的使用

    nohup /mnt/Nand3/H2000G &gt;/dev/null 2&gt;&1 & 对 于& 1 更准确的说应该是文件描述符 1,而1 一般代表的就是STDOUT_FILENO,实际上这个操作就是一个dup2(2)调用.他标准输出到all_result ,然后复制标准输出到文件描述

    shell中1小于/dev/null 2大于&amp;1的含义

    shell中可能经常能看到:&gt;/dev/null 2&gt;&1,这里简单介绍下,方便需要的朋友

    Linux下设备内存地址SuperIO直接操作工具devmem2

    devmem2 0x2000004E b 0x87 &gt; /dev/null 2&gt;&1 sleep 0.01 devmem2 0x2000004E b 0x01 &gt; /dev/null 2&gt;&1 sleep 0.01 devmem2 0x2000004E b 0x55 &gt; /dev/null 2&gt;&1 sleep 0.01 devmem2 0x2000004E b 0xAA &gt; /dev/null ...

    iptables不错脚本

    modprobe ip_tables &gt; /dev/null 2&gt;&1 modprobe iptable_nat &gt; /dev/null 2&gt;&1 modprobe ip_nat_ftp &gt; /dev/null 2&gt;&1 modprobe ip_nat_irc &gt; /dev/null 2&gt;&1 modprobe ipt_mark &gt; /dev/null 2&gt;&1 modprobe ip_...

    如何清除linux日志

     cat /dev/null &gt; /var/log/syslog  cat /dev/null &gt; /var/adm/sylog  cat /dev/null &gt; /var/log/wtmp  cat /dev/null &gt; /var/log/maillog  cat /dev/null &gt; /var/log/messages  cat /dev/null &gt; /var/log/...

    Linux下启动JAVA

    Linux下启动JAVALinux下启动JAVALinux下启动JAVALinux下启动JAVA

    /dev/null和/dev/zero文件使用详解和误删修复方法以及服务器磁盘IO测试详解

    关于linux系统文件/dev/null和/dev/zero文件的详解和/dev/null、/dev/zero文件误删后的修复方法以及服务器磁盘IO测速详解,和一些常见/dev/null /dev/zero文件的用途用法示例说明

    详解nohup /dev/null 2&gt;&1 含义的使用

    主要介绍了详解nohup /dev/null 2&gt;&1 含义的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

    RAC安装 遇到的问题及解决方法.pdf

    第一次安装RAC遇到了很多问题,经过5天的奋战,终于成功安装了RAC。将之整理出来,分享给大家。包括安装问题及常见问题解决方法。csdn一次只能上传一个文档,另一个文档请点击我的资源查看。

    详解shell中/dev/null 2&1到底是什么

    相信大家经常能在shell脚本中发现&gt;/dev/null 2&gt;&1这样的语句。以前的我并没有去深入地理解这段命令的作用,照搬照用,直到上周我将这段命令不小心写成了2&gt;&1 &gt;/dev/null,出了一点小问题之后,我才开始去了解这段...

    中文cactiez基于snmp监控管理软件

    echo '*/5 * * * * php /var/www/html/poller.php &gt; /dev/null 2&gt;&1' &gt;&gt; /tmp/crontab2.tmp crontab /tmp/crontab2.tmp rm /tmp/crontab2.tmp for service in httpd mysqld snmpd do chkconfig --level 235 $...

    详解shell中&gt;/dev/null 2&gt;&1到底是什么

    主要介绍了shell中&gt;/dev/null 2&gt;&1到底是什么,文中介绍的很详细,需要的朋友可以参考借鉴,下面来一起看看吧。

    脚本自动添加crontab示例

    pwd)` PROGRAM=$BASEDIR/bin/daemon.sh CRONTAB_CMD=”*/1 * * * * sh $PROGRAM once &gt; /dev/null 2&gt;&1 &” (crontab -l 2&gt;/dev/null | grep -Fv $PROGRAM; echo “$CRONTAB_CMD”) | crontab – COUNT=`crontab -l...

    win10 null.sys 文件 fatal:open /dev/null or dup failed: No such file or directory

    win10安装git报错 fatal:open /dev/null or dup failed: No such file or directory错误,将该文件复制到C:\Windows\System32\drivers 替换掉原有的null.sys文件重启即可

    计算机病毒与防护:Linux任务调度.pptx

    “/dev/null 2&gt;&1”表示先将标准输出重定向到/dev/null,然后将标准错误重定向到标准输出,由于标准输出已经重定向到了/dev/null,因此标准错误也会重定向到/dev/null,这样日志输出问题就解决了。 系统级与用户级...

    shell中1小于/dev/null 2大于1的含义

    shell中可能经常能看到:&gt;/dev/null 2&gt;&1 命令的结果可以通过%&gt;的形式来定义输出 /dev/null 代表空设备文件 &gt; 代表重定向到哪里,例如:echo “123” &gt; /home/123.txt 1 表示stdout标准输出,系统默认值是1,所以”&gt;...

    fatal: open /dev/null or dup failed: No such file or directory 修复

    使用git Bash here闪退并生成mintty.exe.stackdump文件 cmd使用git 报错 fatal:open /dev/null or dup failed: No such file or directory 并弹出mitty.dump文件 使用方法见我的CSDN

    openwrt sshpass

    在一些openwrt脚本中需要ssh远程登陆,需要输入密码,给脚本编写带来难度,此文件解压后放入./package下make menuconfig 中搜utilities找到sshpass选择编译进固件,就可以使用sshpass命令

    【编程训练】陶海+嵌入式一班#解压BSP资源并加载驱动

    do dd if=/dev/zero of=/dev/null & done for i in 2; do dd if=/dev/zero of=/dev/null & done for i in 3; do dd if=/dev/zero of=/dev/null & done if [ -e /home/hik/test/memtester ]; then /home/hik/test...

    fatal: open /dev/null or dup failed: No such file or directory(解决文件)

    fatal: open /dev/null or dup failed: No such file or directory 解决文件

Global site tag (gtag.js) - Google Analytics