When you run jobs like the example below, they're killed when you logout:
$ ./job.sh &
However, when you execute them as in the below example, redirecting stdout to /dev/null and stderr to stdout as well as putting the job in the background, they're not killed when you log out. It's somewhat like nohup, except that the output goes to /dev/null instead of a file.
$ ./job.sh > /dev/null 2>&1 &
I noticed that some daemons work like that and then I got curious to know the reason why they aren't killed at logout.
Can someone explain me why?
EDIT 1:
As suggested by @Patrick on his comment, I tried the same with the simplest job ever and the result is the same: the job does not die when I log out.
# while true; do sleep 1; done > /dev/null 2>&1 &
[1] 4320
# logout
luis-macbook:~ luis$
luis-macbook:Downloads luis$ ssh server
luis.alen@server's password:
# jobs
#
# ps -ef | grep 4320
root 4320 1 0 01:17 ? 00:00:00 -bash
EDIT 2:
As requested by Patrick again, I did the same test without redirecting the streams and, to my surprise, the process didn't die. Now I'm completely confused... I swear I remember processes put in background dying when you logged out. Am I absolutely wrong?
# while true; do sleep 1; done &
[1] 20720
# logout
luis-macbook:~ luis$ ssh server
# jobs
#
# ps -ef | grep 20720
root 20720 1 0 23:31 ? 00:00:00 -bash
while true; do sleep 1; done) without redirection, notice it stays running. – Patrick Mar 14 '12 at 16:02exitorlogout. If you terminate the shell by closing the terminal window then it will kill the jobs. Now there are special cases where backgrounded jobs will error if they try to do things with a terminal that is no longer there. If you provide what yourjob.shis doing we might be able to identify such issues. – Patrick Mar 15 '12 at 3:25