Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I never know when's the best time to use nohup and the & at the end of the command.

share|improve this question

3 Answers 3

up vote 19 down vote accepted

Nohup makes a program ignore the HUP signal, allowing it to run after current terminal is closed / user logged out. Nohup does not send program to background.
& at the end of command is related to shell job control, allowing user to continue work in current shell session.
Usually nohup and & are combined to launch program which runs after logout of user and allows to continue work at current shell session.

share|improve this answer

Typically I use & when I want to run in the background a command which will not take long to run, or one where I won't really care if the program is terminated if I am unexpectedly logged out of a session.

xcalc &
rdesktop 1.2.3.4 & 
./this_only_takes_a_few_seconds_but_i_want_my_shell_back.py &

When working over a VPN or a dodgy remote connection, I tend to use nohup for running anything which will take a while to run. When running large data imports on a remote host, for example, you might want to use nohup to ensure that getting disconnected won't have you start over when you reconnect.

nohup ./do_data_load.py mydatafile.txt &

It's also used when a developer doesn't properly daemonize a service, so you have to use nohup to ensure it isn't killed when you log out.

nohup sillyd &

If you happen to forget to use nohup when running something like the last two examples, you can use the bash or zsh builtin disown to the same effect.

$ sillyd &
[1] 12345            # Whoops!
$ disown %1

It's pretty rare to use nohup without &, since you can't really reattach to the nohup'd session if you are logged out. This pretty much eliminates its usefulness for interactive commands. In the case where you have an interactive command you want to persist over sessions or avoid complicated disconnects, you can use screen. The firepower of a fully armed and operational screen session is beyond the scope of this post, so you should check it out in more detail.

share|improve this answer
    
I find tmux easier to configure and use than screen. Also, BSD licenced. –  Kristaps May 11 '11 at 9:17
1  
You can also use byobu which adds some nice features to screen. It's available in most apt repos and in the EPEL repos for Red Hat. –  James May 11 '11 at 19:16

nohup: logout current user,the process will not stop

&: with this symbol,there will be no output in current terminal, the process running background

share|improve this answer
4  
there still we be output, as & does not remove standart output and error streams . you have to use stream redirection, like this (in bash) command > /dev/null 2>1 –  Kristaps May 11 '11 at 8:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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