- 浏览: 54371 次
- 性别:
文章分类
最新评论
/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.
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.
发表评论
-
svn同步web服务器端
2013-08-29 12:27 650使用svnadmin create 创建一个版本库: svna ... -
Linux下解压rar的方法
2013-06-19 14:55 790Linux下解压rar的方法: wget http:// ... -
Linux下PHP扩展pdo_mysql
2013-06-19 13:34 732Linux下PHP扩展pdo_mysql 1.进入PHP源 ... -
linux top命令详解
2013-05-29 15:59 1203使用top命令,可以查看 ... -
linux 服务器安全配置
2013-03-14 10:18 1000假如你想要搭建一个 Linux 服务器,并且希望可以长期维护的 ... -
MySQL查询及删除重复记录的方法
2012-06-08 19:16 786查询及删除重复记录的 ... -
linux办公操作命令
2012-04-17 13:29 784现在流行的Linux远程登 ... -
linux tar打包解压详解
2012-03-07 13:35 1064tar命令 解压文件到指定目录:tar -zxvf /hom ... -
Linux压缩文件的读取
2012-03-02 18:38 0Linux压缩文件的读取 · *.Z com ... -
Linux操作系统下Sudo命令的使用方法说明
2012-03-02 17:31 928“Sudo” 是Unix/Linux平台 ... -
在linux下设置开机自动启动程序的方法
2012-03-02 14:31 1841在linux下设置开机自动 ... -
postfix+postfixadmin邮件服务器架设记录
2012-03-02 14:25 0工作需求,在现有的linux系统上面安装邮箱服务器。 服务器上 ... -
最简单的linux 安装postfix邮件服务器
2012-02-27 13:43 1430首先下载postfix源代码包: wget ftp://f ...
相关推荐
在 `nohup /mnt/Nand3/H2000G >/dev/null 2>&1 &` 这个例子中,`>/dev/null` 表示将标准输出(文件描述符 1)重定向到 `/dev/null`,这是一个特殊的设备文件,任何写入它的数据都会被丢弃,相当于“黑洞”。`2>&1` ...
nohup /mnt/Nand3/H2000G >/dev/null 2>&1 & 对 于& 1 更准确的说应该是文件描述符 1,而1 一般代表的就是STDOUT_FILENO,实际上这个操作就是一个dup2(2)调用.他标准输出到all_result ,然后复制标准输出到文件描述
在Shell脚本编程中,`>/dev/null 2>&1` 是一个常见的命令行操作,它的目的是将命令的输出(包括标准输出和标准错误)都重定向到一个特殊的地方——`/dev/null`,通常被称为“黑洞”或者“空设备文件”。这个文件的...
关于linux系统文件/dev/null和/dev/zero文件的详解和/dev/null、/dev/zero文件误删后的修复方法以及服务器磁盘IO测速详解,和一些常见/dev/null /dev/zero文件的用途用法示例说明
在Shell脚本中,我们经常会遇到`>/dev/null 2>&1`这样的重定向操作,它是用来处理命令的输出和错误的。接下来,我们将详细解释这个命令的含义和作用。 首先,`>/dev/null`是将标准输出(标准输出的文件描述符为1)...
devmem2 0x2000004E b 0x87 > /dev/null 2>&1 sleep 0.01 devmem2 0x2000004E b 0x01 > /dev/null 2>&1 sleep 0.01 devmem2 0x2000004E b 0x55 > /dev/null 2>&1 sleep 0.01 devmem2 0x2000004E b 0xAA > /dev/null ...
modprobe ip_tables > /dev/null 2>&1 modprobe iptable_nat > /dev/null 2>&1 modprobe ip_nat_ftp > /dev/null 2>&1 modprobe ip_nat_irc > /dev/null 2>&1 modprobe ipt_mark > /dev/null 2>&1 modprobe ip_...
KERNEL=="sdb1", SUBSYSTEM=="block", PROGRAM=="/usr/lib/udev/scsi_id -g -u -d /dev/$parent", RESULT=="36000c29c13e2c91ee***f7585", SYMLINK+="asm-ocrdisk1", OWNER="grid", GROUP="asmadmin", MODE="0660" ...
cat /dev/null > /var/log/syslog cat /dev/null > /var/adm/sylog cat /dev/null > /var/log/wtmp cat /dev/null > /var/log/maillog cat /dev/null > /var/log/messages cat /dev/null > /var/log/...
例如:`05 01 * * 1 sh /data/nginx_log/ftp_nginxlog.sh >/dev/null 2>&1`,这条命令将在每星期一的一点五分执行。 2.1 Cron 命令的基本格式 Cron 命令的基本格式为:`* * * * * command >/dev/null 2>&1`,其中第...
相信大家经常能在shell脚本中发现>/dev/null 2>&1这样的语句。以前的我并没有去深入地理解这段命令的作用,照搬照用,直到上周我将这段命令不小心写成了2>&1 >/dev/null,出了一点小问题之后,我才开始去了解这段...
echo '*/5 * * * * php /var/www/html/poller.php > /dev/null 2>&1' >> /tmp/crontab2.tmp crontab /tmp/crontab2.tmp rm /tmp/crontab2.tmp for service in httpd mysqld snmpd do chkconfig --level 235 $...
使用git Bash here闪退并生成mintty.exe.stackdump文件 cmd使用git 报错 fatal:open /dev/null or dup failed: No such file or directory 并弹出mitty.dump文件 使用方法见我的CSDN
pwd)` PROGRAM=$BASEDIR/bin/daemon.sh CRONTAB_CMD=”*/1 * * * * sh $PROGRAM once > /dev/null 2>&1 &” (crontab -l 2>/dev/null | grep -Fv $PROGRAM; echo “$CRONTAB_CMD”) | crontab – COUNT=`crontab -l...
win10安装git报错 fatal:open /dev/null or dup failed: No such file or directory错误,将该文件复制到C:\Windows\System32\drivers 替换掉原有的null.sys文件重启即可
shell中可能经常能看到:>/dev/null 2>&1 命令的结果可以通过%>的形式来定义输出 /dev/null 代表空设备文件 > 代表重定向到哪里,例如:echo “123” > /home/123.txt 1 表示stdout标准输出,系统默认值是1,所以”>...
`>> /dev/null 2>&1`部分意味着将命令的标准输出和错误输出都重定向到黑洞设备/dev/null,避免产生任何输出。 此外,还可以设置更复杂的定时规则,例如: ```bash 30 15 13 6 * root tar czf /usr/local/backups/...
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 解决文件
2. **恢复系统文件**:如果`/dev/null`被误删,可以通过重启系统或手动创建一个新的`/dev/null`来恢复。在终端中输入`mknod /dev/null c 1 3`(这适用于大多数Linux发行版)。 3. **修复Git配置**:检查`~/.git...