I think this sums it up:
# /usr/bin/nohup echo hello
/usr/bin/nohup: ignoring input and appending output to `nohup.out'
# ssh localhost /usr/bin/nohup echo hello
hello
What was happened?
recent versions of ssh started using pipes for non-TTY STDIO
so
there is no way for a bash script to tell whether a non-tty
ssh
command is being piped or not.
Since nohup
only springs into action when it detects that stdout
/stderr
are connected to a terminal (and does nothing when the output goes to a pipe), and ssh
's way of launching it looks like it's being piped, you get the behaviour you're seeing.
As muru points out, you can force ssh
to allocate a tty
by using the -t
parameter:
$ ssh -t localhost "/usr/bin/nohup /bin/echo foo"
/usr/bin/nohup: ignoring input and appending output to ‘nohup.out’
From 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.
To stress: If the standard output is a terminal.
With a simple ssh nohup echo foo
, standard output isn't a TTY unless you tell SSH to create one:
ssh -t localhost nohup echo foo
From 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.
Also see:
ssh ... nohup
, since it is your local shell that's doing the redirection and creation of that file. – muru Oct 20 '16 at 14:40nohup
creating its own output file. (nohup
saves output in a file.)nohup
neither knows nor cares about what your local shell is doing, until you tell SSH to allocate a TTY. Even then, if your local shell cannot create the file, it will not start the command, so it's notnohup
that fails. – muru Oct 20 '16 at 14:45