zoukankan      html  css  js  c++  java
  • 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.
    ";
    aaa "hello,world.
    ";
    [root@guangzhou study]# php print.php 1> print1.log
    PHP Parse error:  syntax error, unexpected '"hello,world.
    "' (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.
    "' (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.
    "' (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.
    ";
    aaa "hello,world.
    ";
    [root@guangzhou study]# php print.php > print.log 2>&1[root@guangzhou study]# cat print.php
    <?php
    echo "hello,world.
    ";
    [root@guangzhou study]# php print.php > print2.log 2>&1
    [root@guangzhou study]# cat print.log
    PHP Parse error:  syntax error, unexpected '"hello,world.
    "' (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.
    ";
    [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') . "
    ";
    sleep(50);
    echo "hello,world.
    ";
    echo date('Y-m-d H:i:s') . "
    ";
    [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 文件中。 

  • 相关阅读:
    ntpdate
    动态查看日志
    eclipse proxy
    远程调试
    pe and elf
    03scikit-learn非监督学习
    15管家婆小项目
    02scikit-learn模型训练
    01scikit-learn数据集下载
    scikit-learn中文api
  • 原文地址:https://www.cnblogs.com/wscsq789/p/13717128.html
Copyright © 2011-2022 走看看