19

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

3 Answers 3

24

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.

15

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.

2
  • I find tmux easier to configure and use than screen. Also, BSD licenced.
    – Kristaps
    May 11, 2011 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, 2011 at 19:16
0

nohup: logout current user,the process will not stop

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

2
  • 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, 2011 at 8:43
  • nohup does not log the current user out. As it was explained in other comments, it makes the process immune to the user being disconnected from the terminal.
    – Jasio
    Jan 4 at 12:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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