How to detach a Linux process from shell (using bg/disown/nohup)

Linux will automatically a child process by sending SIGINT when parent (a shell exit) ends. To avoid this you must detach the child from the parent.

  • disown: Prevents a closing shell from sending SIGUP signals.
$ cmd & 
# or hit CTRL-Z to interrupt and bg
$ gb
[1] cmd &

# disown is supported in bash and zsh, but shells like csh, tcsh, dash do not support it
$ disown %1

see fg@cyberciti and disown@cyberciti

$ nohup cmd &

# nohupping backgrounded jobs is typically used to avoid terminating them when logging off from a remote SSH session
$ cmd </dev/null &>/dev/null &
or
$ nohup cmd > foo.out 2> foo.err < /dev/null &

see nohup@cyberciti

  • tmux@wiki/gnu screen@wiki: can also detach a process from the current shell. It allows one to reattach to the process later on.
# run cmd detached from SIGHUP and in the background
$ screen -A -m -d -S somename cmd &

from How to detach a Linux process from shell

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s