nohup命令重定向標准輸出和錯誤輸出


命令:command > /dev/null  2>&1 &

輸出到/dev/null表示輸出重定向到黑洞,即輸出內容不打印到屏幕上,null是/dev下空設備文件。

 :代表重定向到哪里,例如:echo "123" > ./123.txt
 :表示stdout標准輸出,系統默認值是1,所以">/dev/null"等同於"1>/dev/null"
 :表示stderr標准錯誤
 :表示等同於的意思,2>&1,表示2的輸出重定向等同於1

 

一. 依據上面所述,下面兩種輸出效果一致:

 

[root@guangzhou study]# cat print.php <?php echo "hello,world\";
[root@guangzhou study]# php print.php > print.log [root@guangzhou study]# php print.php 1> print1.log [root@guangzhou study]# cat print.log hello,world. [root@guangzhou study]# cat print1.log hello,world.

 

 完整命令: command 1> 日志文件 (或者 command > 日志文件)

 

二. 現在嘗試標准錯誤輸出,故意修改造成print.php文件報語法錯誤,執行代碼后打印兩個日志文件均為空。

 

[root@guangzhou study]# cat print.php <?php //echo "hello,world.\n";
aaa "hello,world.\n"; [root@guangzhou study]# php print.php 1> print1.log PHP Parse error: syntax error, unexpected '"hello,world.\n"' (T_CONSTANT_ENCAPSED_STRING) in /opt/www/study/print.php on line 3 [root@guangzhou study]# php print.php > print.log PHP Parse error: syntax error, unexpected '"hello,world.\n"' (T_CONSTANT_ENCAPSED_STRING) in /opt/www/study/print.php on line 3 [root@guangzhou study]# cat print.log [root@guangzhou study]# cat print1.log 

 

可見標准輸出不能程序的錯誤輸出。

現在改成2使用錯誤輸出重定向錯誤日志,執行程序后打印可見錯誤信息。

 

[root@guangzhou study]# php print.php 2> print2.log [root@guangzhou study]# cat print2.log PHP Parse error: syntax error, unexpected '"hello,world.\n"' (T_CONSTANT_ENCAPSED_STRING) in /opt/www/study/print.php on line 3

 

現在我們知道標准輸出和錯誤輸出各自使用場景。(注意: 重定向符號“>”前的數字1/2中間必須在一起,中間不能有空格,不然重定向失敗。)

完整命令: command  2> 日志文件

 

另外可以將錯誤輸出重定向到標准輸出的日志文件中:

 

[root@guangzhou study]# cat print.php <?php //echo "hello,world.\n";
aaa "hello,world.\n"; [root@guangzhou study]# php print.php > print.log 2>&1[root@guangzhou study]# cat print.php <?php echo "hello,world.\n"; [root@guangzhou study]# php print.php > print2.log 2>&1 [root@guangzhou study]# cat print.log PHP Parse error: syntax error, unexpected '"hello,world.\n"' (T_CONSTANT_ENCAPSED_STRING) in /opt/www/study/print.php on line 3 [root@guangzhou study]# cat print2.log hello,world.

 

 

 

完整命令: command > 日志文件 2>&1

 

三. 有時程序可能要跑好一會,當前命令行窗口需要處理其他事情的情況下,可以在命令末尾加上“&”符號,下面腳本一開始休眠10秒鍾:

 

[root@guangzhou study]# cat print.php <?php sleep(10); echo "hello,world.\n"; [root@guangzhou study]# php print.php > print.log 2>&1 & [2] 11641 #當前窗口可執行其他命令,如date命令 [root@guangzhou study]# date 2020年 09月 23日 星期三 10:52:38 CST [root@guangzhou study]# cat print.log hello,world. [2]-  完成                  php print.php > print.log 2>&1

 

 完整命令: command > 日志文件 2>&1 &

 

四. 上面命令末尾加“&”符號只能用在窗口為關閉的情況,如需要關閉窗口后命令繼續運行的可在命令開始處加上“nohup”符號:

 

[root@guangzhou study]# cat print.php <?php echo date('Y-m-d H:i:s') . "\n"; sleep(50); echo "hello,world.\n"; echo date('Y-m-d H:i:s') . "\n"; [root@guangzhou study]# date 2020年 09月 23日 星期三 11:06:25 CST [root@guangzhou study]# nohup php print.php > print.log 2>&1 & [1] 14164 [root@guangzhou study]# date 2020年 09月 23日 星期三 11:06:32 CST

 

第二個date執行后立即關閉當前窗口,並新開窗口打印日志,可見兩次時間不足50秒:

[root@guangzhou study]# cat print.log nohup: 忽略輸入 2020-09-23 03:02:59 hello,world. 2020-09-23 03:03:12

這里有一點忘記說明,關閉窗口前需要執行exit,直接關閉窗口會導致nohup命令無法掛起。

我們重新跑一次 cat.php,   date,   nohup php print.php > print.log 2>&1 &,  date, 再加上exit命令,關閉當前窗口並新開窗口,打印print.log文件可以發現時間間隔正好是50秒。

完整命令: nohup command > 日志文件 2>&1 &

 

ps: nohup命令是由 Command參數和任何相關的 Arg參數指定的命令,忽略所有掛斷SIGHUP信號。
如果不將 nohup 命令的輸出重定向,輸出將附加到當前目錄的 nohup.out 文件中。如果當前目錄的 nohup.out 文件不可寫,輸出重定向到 $HOME/nohup.out 文件中。 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM