detach

detach

Robbert Haarman

2010-12-11


Introduction

detach forks a new process, detaches it from the terminal, and executes a given command in it. This can be used for starting processes that don't terminate when the controlling terminal does.


Download

The following downloads are available:

Latest Release

This version supports writing pidfiles, using files for stdin, stdout and stderr, and running in the foreground.

Initial Release

This is the original minimalist version of detach. It does not support any options, the arguments are simply treated as the command to invoke. Often, that's all you need.


Installation

Compiling and installing detach is as simple as:

$ make && sudo make install

If desired, an alternate installation prefix (default is /usr/local) can be specified by setting the PREFIX environment variable:

# env PREFIX=/usr make install

See the Makefile for additional details.


Examples

Start Firefox and keep it running even if the current shell exits:

$ detach mozilla-firefox

ssh into a remote machine, and start an xterm there that will be displayed on the local machine:

$ ssh remote.host.net detach env \
	DISPLAY=local.host.net:0.0 xterm

Alternatives

Before I started writing detach, I thought that surely someone must have already done so. While at the time I wasn't able to find any command that did what I wanted, I have since discovered some commands that do the same or almost the same as detach.

setsid

Debian GNU/Linux contains a binary called setsid in the util-linux package, which does the same thing as detach 0.1.0 (i.e. it uses setsid(2) to detach a command from the terminal, but does not redirect the standard file descriptors). So if you use Debian GNU/Linux, or another distribution with the setsid command, you probably have no need for detach.

To start mozilla using setsid, you would use:

$ setsid mozilla

Neither detach nor setsid are standard Unix commands, so one cannot count on either one being available on a particular system. detach is intended to be portable, and is known to work on OpenBSD, NetBSD, GNU/Linux and Mac OS X. The same is probably true of setsid, but I have not tested this.

disown

Some shells, like bash and zsh, contain a disown built-in which performs a similar function to detach. disown, without any arguments, removes the current job from the job table, meaning that, for all intents and purposes, the shell forgets the job exists. In particular, as with detach, the jobs will not exit when the shell does. You can also specify one or more jobs after the disown command, which will remove the specified jobs, rather than the current one.

Examples:

$ gv report.pdf &
[1] 9916
$ jobs
[1]  + running    gv report.pdf
$ disown %1
$ jobs
$ exit
# Shell exits, leaving gv running

One important difference between detach and disown is that detach is a program, whereas disown is a shell built-in. This means that you can use detach without using a shell, for example, with the exec family of system calls. With disown, you would have to start a shell first, and then, in the shell, start the program you want to start, and then detach it. For example, to start and detach mozilla from a Ruby script, you could use one of the following:

# Using detach:
exec 'detach', 'mozilla'

# Using bash and disown:
exec 'bash', '-c', 'mozilla & disown'

nohup

When Slashdot found out about detach, ScriptedReplay asked why one would use detach, instead of nohup, which comes with the system. The truth is that I didn't find nohup when searching for a command that would run another command detached from the terminal. The reason is that nohup doesn't really do that:

nohup, as the name implies, makes your command ignore SIGHUP. That is, unless you set a handler for it yourself, in which case you will still receive the signal when the terminal exits. You can use normal shell redirection and backgrounding on your process (although nohup will automatically redirect standard output to nohup.out if you don't redirect it).

detach uses the setsid system call to detach the command from the terminal. This means your process simply won't get a SIGHUP when the terminal exits, so you are free to use SIGHUP for things like reloading configuration files (like many daemons do). The disadvantage is that the standard file descriptors must be closed before calling setsid. For this reason, normal redirection doesn't work with detach, so explicit support for redirection has been added. detach also contains an option for writing the pid of the detached process to a file.