咯哒鬼哦
发表于:2020-11-25 08:52 阅读: 162次
这篇文章详细介绍了nohup命令重定向标准输出和错误输出,并通过代码从实战的角度进行了讲解,对大家的学习和工作有很好的参考价值,现在赶紧来跟我一起学习一下吧!
命令:command > /dev/null 2>&1 &
输出到/dev/null表示输出重定向到黑洞,即输出内容不打印到屏幕上,null是/dev下空设备文件。
> :代表重定向到哪里,例如:echo "123" > ./123.txt
1 :表示stdout标准输出,系统默认值是1,所以">/dev/null"等同于"1>/dev/null"
2 :表示stderr标准错误
& :表示等同于的意思,2>&1,表示2的输出重定向等同于1
一. 依据上面所述,下面两种输出效果一致:
[root@guangzhou study]# catprint.php<?phpecho "hello,world\";
[root@guangzhou study]# php print.php >print.log
[root@guangzhou study]# php print.php1>print1.log
[root@guangzhou study]#catprint.log
hello,world.
[root@guangzhou study]#catprint1.log
hello,world.
完整命令: command 1> 日志文件 (或者 command > 日志文件)
二. 现在尝试标准错误输出,故意修改造成print.php文件报语法错误,执行代码后打印两个日志文件均为空。
[root@guangzhou study]# catprint.php<?php //echo "hello,world.\n";
aaa "hello,world.\n";
[root@guangzhou study]# php print.php1>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]#catprint.log
[root@guangzhou study]#cat print1.log
可见标准输出不能程序的错误输出。
现在改成2使用错误输出重定向错误日志,执行程序后打印可见错误信息。
[root@guangzhou study]# php print.php 2>print2.log
[root@guangzhou study]#catprint2.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]# catprint.php<?php //echo "hello,world.\n";
aaa "hello,world.\n";
[root@guangzhou study]# php print.php> print.log 2>&1[root@guangzhou study]# catprint.php<?phpecho "hello,world.\n";
[root@guangzhou study]# php print.php> print2.log 2>&1[root@guangzhou study]#catprint.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]#catprint2.log
hello,world.
完整命令: command > 日志文件 2>&1
三. 有时程序可能要跑好一会,当前命令行窗口需要处理其他事情的情况下,可以在命令末尾加上“&”符号,下面脚本一开始休眠10秒钟:
[root@guangzhou study]# catprint.php<?phpsleep (10);echo "hello,world.\n";
[root@guangzhou study]# php print.php> print.log 2>&1 &[2] 11641#当前窗口可执行其他命令,如date命令
[root@guangzhou study]#date2020年 09月 23日 星期三10:52:38CST
[root@guangzhou study]#catprint.log
hello,world.
[2]- 完成 php print.php > print.log 2>&1
完整命令: command > 日志文件 2>&1 &
四. 上面命令末尾加“&”符号只能用在窗口为关闭的情况,如需要关闭窗口后命令继续运行的可在命令开始处加上“nohup”符号:
[root@guangzhou study]# catprint.php<?phpecho 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]#date2020年 09月 23日 星期三11:06:25CST
[root@guangzhou study]# nohup php print.php> print.log 2>&1 &[1] 14164[root@guangzhou study]#date2020年 09月 23日 星期三11:06:32 CST
第二个date执行后立即关闭当前窗口,并新开窗口打印日志,可见两次时间不足50秒:
[root@guangzhou study]# catprint.log
nohup: 忽略输入2020-09-23 03:02:59hello,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 文件中。
好了先写到这吧,希望这篇关于nohup命令重定向标准输出和错误输出的教程对大家有所启发和帮助,同时希望大家多多支持酷码派!
分类: 运维技术
29
8