使用 nohup 和管道重定向到文件问题

mis*_*yes 2 shell pipe io-redirection nohup

对于以下命令:

ssh -t esolve@hostname 'sudo nohup bash -c "ls > log 2>&1 &"'
Run Code Online (Sandbox Code Playgroud)

我总是收到错误信息:

       nohup: ignoring input and appending output to `nohup.out'
Run Code Online (Sandbox Code Playgroud)

为什么会发生这种情况以及如何避免此错误信息?

此外,在下面(我用它来启用输出到屏幕和文件),

   stdbuf -o 0 command|& tee logfile
Run Code Online (Sandbox Code Playgroud)

是做什么& 用的?

Gil*_*il' 5

碰巧您正在运行的命令nohup从不写入 stdout 或 stderr,并且您没有向其发送任何输入。但是nohup无法知道这一点,因此它告诉您任何输入都将被丢弃,任何输出都将写入nohup.out.

为了避免这种情况,将nohup的 stdin、stdout 和 stderr重定向到/dev/null.

ssh -t esolve@hostname 'sudo nohup bash -c "ls > log 2>&1 &" </dev/null >/dev/null 2>/dev/null'
Run Code Online (Sandbox Code Playgroud)

至于|&,来自bash 手册

管道的格式是

[time [-p]] [!] command1 [ [| or |&] command2 …]
Run Code Online (Sandbox Code Playgroud)

管道中每个命令的输出通过管道连接到下一个命令的输入。也就是说,每个命令都会读取前一个命令的输出。此连接在命令指定的任何重定向之前执行。

如果|&使用,则标准错误通过管道command1连接到command2的标准输入;它是 的简写2>&1 |

这是一个 bash 扩展,也存在于 zsh 中。