/dev/null 2>&1詳解

來源:互聯網
上載者:User


/dev/null 2>&1詳解 今天一個朋友突然在自己的維護的Linux中, /var/spool/cron/root 中看到了以下的內容:  www.2cto.com  30 19 * * * /usr/bin/**dcon.sh > /dev/null 2>&159 23 * * 1-7 /home/s**-log/squid-log.renew > /dev/null 2>&150 1 * * 1-7 /usr/local/src/**log.sh > /dev/null 2>&120 2 * * 1-7 /home/sq**-log/**log > /dev/null 2>&130 2 * * 1-7 /home/sq**-log/**log.0130 22 * * * /bin/**sync > /dev/null 2>&100 8 * * 1-7 /home/**-log/rmcore > /dev/null 2>&100 16 * * 1-7 /home/**-log/rmcore > /dev/null 2>&1 他問我為什麼要用 /dev/null 2>&1 這樣的寫法.這條命令的意思是將標準輸出和錯誤輸出全部重新導向到/dev/null中,也就是將產生的所有資訊丟棄.下面我就為大家來說一下, command > file 2>file  與command > file 2>&1 有什麼不同的地方.  www.2cto.com        首先~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 這樣的寫法.
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.