What are the differences between
$ nohup foo
and
$ foo &
and
$ foo &
$ disown
Let's first look at what happens if a program is started from an interactive shell (connected to a terminal) without &
(and without any redirection). So let's assume you've just typed foo
:
foo
is created.SIGHUP
, it also sends a SIGHUP
to the process (which normally causes the process to terminate).Now, let's look what happens if you put the process in the background, that is, type foo &
:
foo
is created.jobs
and can be accessed using %n
(where n
is the job number).fg
, in which case it continues as if you would not have used &
on it (and if it was stopped due to trying to read from standard input, it now can proceed to read from the terminal).SIGHUP
, it also sends a SIGHUP
to the process. Depending on the shell and possibly on options set for the shell, when terminating the shell it will also send a SIGHUP
to the process.Now disown
removes the job from the shell's job list, so all the subpoints above don't apply any more (including the process being sent a SIGHUP
by the shell). However note that it still is connected to the terminal, so if the terminal is destroyed (which can happen if it was a pty, like those created by xterm
or ssh
, and the controlling program is terminated, by closing the xterm or terminating the SSH connection), the program will fail as soon as it tries to read from standard input or write to standard output.
What nohup
does, on the other hand, is to effectively separate the process from the terminal:
EOF
).nohup.out
, so the program won't fail for writing to standard output if the terminal fails, so whatever the process writes is not lost.SIGHUP
(thus the name).Note that nohup
does not remove the process from the shell's job control and also doesn't put it in the background (but since a foreground nohup
job is more or less useless, you'd generally put it into the background using &
). For example, unlike with disown
, the shell will still tell you when the nohup job has completed (unless the shell is terminated before, of course).
So to summarize:
&
puts the job in the background, that is, makes it block on attempting to read input, and makes the shell not wait for its completion.disown
removes the process from the shell's job control, but it still leaves it connected to the terminal. One of the results is that the shell won't send it a SIGHUP
. Obviously, it can only be applied to background jobs, because you cannot enter it when a foreground job is running.nohup
disconnects the process from the terminal, redirects its output to nohup.out
and shields it from SIGHUP
. One of the effects (the naming one) is that the process won't receive any sent SIGHUP
. It is completely independent from job control and could in principle be used also for foreground jobs (although that's not very useful).nohup
alone doesn't save a google-chrome process from being closed, when the terminal from which it was started is closed?
nohup
disconnects from the controlling terminal is wrong. nohup
closes some standard I/O streams and opens them elsewhere. It does not change session, attempt to affect the session's connection to a controlling terminal, or deal in process groups.
Using &
causes the program to run in the background, so you'll get a new shell prompt instead of blocking until the program ends. nohup
and disown
are largely unrelated; they suppress SIGHUP (hangup) signals so the program isn't automatically killed when the controlling terminal is closed. nohup
does this when the job first begins. If you don't nohup
a job when it begins, you can use disown
to modify a running job; with no arguments it modifies the current job, which is the one that was just backgrounded
nohup
and disown
both can be said to suppress SIGHUP
, but in different ways. nohup
makes the program ignore the signal initially (the program may change this). nohup
also tries to arrange for the program not to have a controlling terminal, so that it won't be sent SIGHUP
by the kernel when the terminal is closed. disown
is purely internal to the shell; it causes the shell not to send SIGHUP
when it terminates.
Commented
Nov 9, 2010 at 18:26
disown
removing the job from the jobs list. If you don't specify an option, it does remove it from the jobs list. However, if you specify the -h
option, each jobspec is not removed from the table. Instead, it makes it so that SIGHUP
is not sent to the job if the shell receives a SIGHUP
.
Commented
Nov 13, 2013 at 6:54
&
does not give you a terminal, it detaches stdin
from the process and causes it to run in the background, but both stdout
and stderr
is still attached to the current tty. This means that you may get text from different programs mixed up together, which can be quite annoying if you do gimp &
and get lots of GTK+ errors while trying to use that tty for something else.
Here is my experience trying to run soffice in the background, following a non-terminating command (e.g. tail
). For this example I will use sleep 100
.
In all the cases below I execute like this:
./scriptfile
<Ctl-C>
#!/bin/bash
/opt/libreoffice4.4/program/soffice -invisible -nofirststartwizard &
sleep 100
I see soffice logs / by pressing Ctrl-C soffice stops
#!/bin/bash
nohup /opt/libreoffice4.4/program/soffice -invisible -nofirststartwizard &
sleep 100
I don't see soffice logs / by pressing Ctrl-C soffice stops
#!/bin/bash
/opt/libreoffice4.4/program/soffice -invisible -nofirststartwizard & disown
sleep 100
I see soffice logs / by pressing Ctrl-C soffice stops
#!/bin/bash
setsid /opt/libreoffice4.4/program/soffice -invisible -nofirststartwizard &
sleep 100
I see soffice logs / by pressing Ctrl-C soffice DOES NOT STOP
To save space:
nohup setsid ..
: does not show logs / soffice DOES NOT STOP on Ctrl-C
nohup
with & disown
at the end : does not show logs / soffice stops on Ctrl-C
nohup ⟨command⟩ & disown
the created process does not stop on Ctrl+C
.
soffice
? soffice
command seems to have something different. So I considered adding it here as a rule exception. e.g. when using: nohup .. &
, pressing Ctrl-c
normally does not cause the command to stop, but with soffice
it does. I wait until someone steps on this and explains why this happens with soffice :)
Commented
Jun 28, 2018 at 13:29
nohup soffice &
and pressed Ctrl+C
. Nothing happened, as expected.
Short answer:
&
when you want your command to run in the background, so you can run the next one without waiting for it to finishnohup
if you want your command to ignore the SIGHUP
signal, so when you close the terminal or log out from ssh session the process keeps runningdisown
if you forgot to run the command with nohup
and want to log out without killing the process (it will disown all processes in the background). To put a process in the background and disown it:
bg
to put stopped process to the backgrounddisown
to make process ignore terminal terminationSee also the daemonize
(1) utility, which handles all of the chores about running a "true background" process. As of its docs:
daemonize runs a command as a Unix daemon. As defined in W. Richard Stevens' 1990 book, Unix Network Programming (Addison-Wesley, 1990), a daemon is “a process that executes `in the background' (i.e., without an associated terminal or login shell) either waiting for some event to occur, or waiting to perform some specified task on a periodic basis.” Upon startup, a typical daemon program will:
Most programs that are designed to be run as daemons do that work for themselves. However, you'll occasionally run across one that does not. When you must run a daemon program that does not properly make itself into a true Unix daemon, you can use daemonize to force it to run as a true daemon.
It superseeds all of the &
/nohup
/disown
mess.
& puts the job in the background, i.e. makes it block input from the shell, and makes the shell not wait for its completion.
nohup and disown both can be said to suppress stdin from the terminal and SIGHUP
, but in different ways. nohup
works when the job is initialized, disown
works after the fact.
nohup also redirects stderr
to stdout
and stdout
to $HOME/nohup.out
. It does not block other signals being sent indirectly from the original shell.
disown does not redirect stdout
or stderr
. It simply removes the process from the shells job table, so no signals will be indirectly sent to the process from the original shell.
SIGTERM
, SIGKILL
, SIGINT
, SIGQUIT
, etc. from the parent process. This may result in a process exiting when you expect it to not to.
foo &!
which should be equal to disowning it right from the start.set -m
to enable job control. See unix.stackexchange.com/questions/196603/…