In Bash, the
disown command issued by itself will remove backgrounded (via
&) processes from the active job table, and mark them to not receive a SIGHUP on logout.
You can also pass one or more jobs to disown, like
disown 1 3. The
disown -h flag is useful if you want to keep jobs in the table, but still not SIGHUP on logout.
You can view the job table by issuing the
jobs command. After a successful background, it will show
+ command &. After disowning a job, it should no longer display in the job table, and no longer be killed on logout. You can still view the process via
top, and other process-viewing utilities.
After a job has been disowned, you can wait for it to terminate naturally or send a signal via
kill to the PID to stop it.
Because Bash is just removing the job from the list of running jobs to terminate and the file handles to your terminal's stdout and stderr are still open, you will continue to receive output from the job until your terminal device is closed (when you log out).
# we start a command in the background
$ cat /dev/urandom > test &
# we see our command is still running
+ Running cat /dev/urandom > test &
# we disown the backgrounded job
$ disown 1
# notice it is no longer in the job table
I usually only use
disown if I run a potentially long-running command like a
cp and afterwards decide I need to log out without terminating it. If you know you're going to run a command and log out, you can capture the output by piping or
teeing it to a file, running it with
nohup, or running it in
screen (which allows you to retake ownership of the command/terminate afterwards).
# capture stdout and stderr to separate logs
cat /dev/urandom >stdout.log 2>stderr.log
# capture stdout and stderr to the same log, and display to stdout as well
cat /dev/urandom 2>&1 | tee output.log
# run a command under nohup (doesn't require a disown or job control support)
nohup cat /dev/urandom </dev/null