Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does nohup work across a pipe?

Tags:

linux

bash

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.

like image 589
Jasmine Lognnes Avatar asked Nov 13 '14 15:11

Jasmine Lognnes


People also ask

Where does nohup output to?

Since there isn't a terminal to associate with it, nohup logs everything to an output file, nohup. out . By default, that file is located in whichever directory you started the command in. nohup.

What is the difference between nohup and &?

nohup catches the hangup signal (see man 7 signal ) while the ampersand doesn't (except the shell is confgured that way or doesn't send SIGHUP at all). Normally, when running a command using & and exiting the shell afterwards, the shell will terminate the sub-command with the hangup signal ( kill -SIGHUP <pid> ).

What is nohup used for?

Nohup, short for no hang up is a command in Linux systems that keep processes running even after exiting the shell or terminal. Nohup prevents the processes or jobs from receiving the SIGHUP (Signal Hang UP) signal. This is a signal that is sent to a process upon closing or exiting the terminal.

Does nohup run in background?

The nohup command can also be used to run programs in the background after logging off. To run a nohup command in the background, add an & (ampersand) to the end of the command.


4 Answers

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 
like image 58
Wolph Avatar answered Sep 20 '22 16:09

Wolph


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.

like image 33
Politank-Z Avatar answered Sep 20 '22 16:09

Politank-Z


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

echo "cmd1 | cmd2" > nohupScript.sh
nohup nohupScript.sh &
like image 37
pyOwner Avatar answered Sep 21 '22 16:09

pyOwner


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.

like image 33
Onnonymous Avatar answered Sep 22 '22 16:09

Onnonymous