Separating a running job from the shell

There’s a little-known, but oh so useful, bash builtin called disown – We’ve all started a job that ends up taking way longer than anticipated, now you really wish you had backgrounded it to begin with, or had used Screen.

From the manpage:

disown [-ar] [-h] [jobspec ...]

Without options, each jobspec is removed from the table of active jobs. If the -h option is given, each jobspec is not removed from the table, but is marked so that SIGHUP is not sent to the job if the shell receives a SIGHUP. If no jobspec is present, and neither the -a nor the -r option is supplied, the current job is used. If no jobspec is supplied, the -a option means to remove or mark all jobs; the -r option without a jobspec argument restricts operation to running jobs. The return value is 0 unless a jobspec does not specify a valid job.

It’s important to understand that disown does’t work on pids or processes, but jobs.
Here’s an example

$ rm -rf millionsoffiles
CTRL-Z
$ bg
[1]+ rm -rf millionsoffiles &
$ jobs
[1]+  Running                 rm -rf millionsoffiles &
$ disown %1

Keep in mind, you need to be using the Bash shell for this to work.

This entry was posted in UNIX and tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>