为什么“ssh localhost nohup echo hello”不起作用?

为什么“ssh localhost nohup echo hello”不起作用?

我认为这概括了这一点:

# /usr/bin/nohup echo hello
/usr/bin/nohup: ignoring input and appending output to `nohup.out'

# ssh localhost /usr/bin/nohup echo hello
hello

发生了什么事?

答案1

显然,

最新版本的 ssh 开始对非 TTY STDIO 使用管道

所以

bash 脚本无法判断是否ssh正在通过管道传输非 tty 命令。

因为只有当它检测到/连接到终端nohup时才会采取行动(并且当输出进入管道时不执行任何操作),并且的启动方式看起来像是正在通过管道传输,因此您会得到您所看到的行为。stdoutstderrssh


作为穆鲁 指出,您可以使用参数强制ssh分配 a :tty-t

$ ssh -t localhost "/usr/bin/nohup /bin/echo foo"
/usr/bin/nohup: ignoring input and appending output to ‘nohup.out’

答案2

man nohup(POSIX)

   If  the  standard output is a terminal, all output written by the named
   utility to its standard output shall be appended to the end of the file
   nohup.out  in  the current directory. ...

   If the standard error is a terminal, all output written  by  the  named
   utility  to  its  standard  error  shall be redirected to the same file
   descriptor as the standard output.

强调:如果标准输出是终端

简单的ssh nohup echo foo标准输出不是 TTY,除非您告诉 SSH 创建一个:

ssh -t localhost nohup echo foo

man ssh

-T   Disable pseudo-tty allocation.
-t   Force pseudo-tty allocation.  This can be used to execute
     arbitrary screen-based programs on a remote machine, which can be
     very useful, e.g. when implementing menu services.  Multiple -t
     options force tty allocation, even if ssh has no local tty.

另请参阅:

相关内容