redirection to file issues with nohup and pipe

5,461

It happens that the command you're running under nohup never writes to stdout or stderr, and that you aren't sending any input to it. But nohup has no way to know that, so it tells you that any input would be discarded and any output would be written to nohup.out.

To avoid this, redirect nohup's stdin, stdout and stderr to /dev/null.

ssh -t esolve@hostname 'sudo nohup bash -c "ls > log 2>&1 &" </dev/null >/dev/null 2>/dev/null'

As for |&, from the bash manual:

The format for a pipeline is

[time [-p]] [!] command1 [ [| or |&] command2 …]

The output of each command in the pipeline is connected via a pipe to the input of the next command. That is, each command reads the previous command’s output. This connection is performed before any redirections specified by the command.

If |& is used, the standard error of command1 is connected to command2’s standard input through the pipe; it is shorthand for 2>&1 |.

This is a bash extension, also present in zsh.

Share:
5,461

Related videos on Youtube

user3802606
Author by

user3802606

Updated on September 18, 2022

Comments

  • user3802606
    user3802606 over 1 year

    For the following command:

    ssh -t esolve@hostname 'sudo nohup bash -c "ls > log 2>&1 &"'
    

    I always got error information:

           nohup: ignoring input and appending output to `nohup.out'
    

    why does this happend and how to avoid this error information?

    Besides, in the following (I use it to enable output to both screen and file),

       stdbuf -o 0 command|& tee logfile
    

    what is the & used for?

  • user3802606
    user3802606 about 11 years
    it seems to me that when there are output or error information from the command, like ls in my script, the output and error are not written to nohup.out, why?
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' about 11 years
    @misteryes You've redirected ls's stdout and stderr to log.