45

If I do

nohup cmd1 | cmd2 &

is that the same as

nohup "cmd1 | cmd2" &

?

I would like that I nohup everything, as cmd1 will listen on port 8023.

5 Answers 5

78

No, you need to add the nohup to the commands separately.

Something like this is recommended:

nohup sh -c "cmd1 | cmd2" &

Or alternatively:

nohup $SHELL <<EOF &
cmd1 | cmd2
EOF
6
  • can someone please explain what is $SHELL and why we need it? Commented May 15, 2017 at 18:09
  • 2
    The $SHELL contains your currently running shell, on most Linux systems this will be /bin/bash but it could be /usr/bin/zsh or something else. The advantage of using the $SHELL variable is that this way the command will be executed using your regular shell settings.
    – Wolph
    Commented May 15, 2017 at 22:04
  • @RoVo: the EOF depends on the earlier EOF and is the standard for multiline input in bash-like shells. If you changed the <<EOF to something else the EOF at the end needs to change too. Which shell (and version) are you using?
    – Wolph
    Commented Jul 27, 2017 at 21:53
  • I did not change the first EOF. EOF is working, but not EOF &. Just opens a new line. I use GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu). The first option works for me though, just needed bash -c for having bash builtins.
    – pLumo
    Commented Jul 28, 2017 at 7:32
  • 2
    @RoVo I think I may have placed the & at the wrong location with the original answer, try the updated answer :)
    – Wolph
    Commented Jul 31, 2017 at 2:40
17

As an alternative to nohup, I recommend

( cmd1 | cmd2 ) > logfile < /dev/null 2>&1 &

By rerouting stdin, stdout, and sterr from the terminal, this achieves much the same effect as nohup with a syntax that I, at least, prefer.

4
  • 12
    That doesn't protect the command from the HUP signal like nohup does. You'd need (trap "" HUP; cmd1 | cmd2 ) </dev/null > logfile 2>&1 & to do that. Commented Nov 13, 2014 at 15:37
  • 4
    True, but as written, it does prevent the closing terminal from sending the HUP to the process in the first place.
    – Politank-Z
    Commented Nov 13, 2014 at 15:45
  • Unfortunately, a process created in this way will still be killed when you close the terminal.
    – scribu
    Commented Aug 27, 2019 at 10:30
  • 1
    @scribu In my experience, it does not. I have used this method to continue processes after the terminal closes many times without fail.
    – Politank-Z
    Commented Aug 27, 2019 at 20:16
3

You always can create a script file and run it with nohup:

echo "cmd1 | cmd2" > nohupScript.sh
nohup nohupScript.sh &
2

You could start your pipe in a screen session. Keystroke Ctrl-a and then d will detach the screen session from your terminal. You can then safely exit your terminal; the pipe will continue to run. Use screen -r to reconnect to the session again.

-2
nohup cmd1 | cmd2 &

No, I have checked just now this is the as follows

nohup: ignoring input and redirecting stderr to stdout
1
  • @JasmineLognnes link contain same error message which posted along with my answer. Commented Nov 13, 2014 at 17:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.