Saturday, May 10, 2008

Bash "nohup"s Background Jobs

In the old days, if you had a background job running, and you accidentally killed your terminal window, the job was killed with it. A special command, nohup ("no hangup"), would prevent this; it detached the job from the terminal's process group.

You can still find /usr/bin/nohup, but bash now automatically nohups all background jobs. Try this from a new terminal window:
$ { sleep 20; echo DONE > /tmp/OUT; } & sleep 1; exit
The window will die, but if you look in another window (or, after 20 seconds, in /tmp/OUT), you'll see the backgrounded job continues.

I say this because I still see admonitions to nohup jobs.

1 comment:

Elliot Wilen said...

That's how bash is behaving, for sure--I just tested it. But is this possibly a bug, not a feature?

Via Wikipedia, I found this:

It seems the "correct" way to leave a job running when you log out, if you didn't nohup it to begin with, is to use disown -h.

This raises the question of how to recover the output of the job, though--whether you use disown or simply kill the terminal window. Any ideas?