Why do unix background processes sometimes die when I exit my shell? Why do unix background processes sometimes die when I exit my shell? unix unix

Why do unix background processes sometimes die when I exit my shell?


The difference here is indeed the intervening process.When you close the terminal window, a HUP signal (related to "nohup" as an0nymo0usc0ward mentioned) is sent to the processes running in it. The default action on receiving HUP is to die - from the signal(3) manpage,

 No    Name         Default Action       Description 1     SIGHUP       terminate process    terminal line hangup

In your first example, the sleep process directly receives this HUP signal and dies because it isn't set to do anything else. (Some processes catch HUP and use it to perform some action, e.g. reread some configuration files)

In the second example, the shell process running your shell script has already died, so the sleep process never gets the signal. In UNIX, every process must have a parent process due to the internals of how the wait(2) family of calls works and indeed processes in general. So when the parent process dies, the kernel gives it to init (pid 1, as you note) as a foster child.Orphan process (on wikipedia) has some more information available about it, also see Zombie process for some additional technical details.


Already running process?

^z
bg
disown %<jobid>

New process/script (on local machine's console)?

nohup script.sh &

New process/script (on remote machine's console)?

Depending on your need,
there are two options [ there will be more ;-) ]

ssh remotehost 'nohup /path/to/script.sh </dev/null > nohup.out 2>&1 &'

OR

use 'screen'