Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?


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


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
share|improve this question

migrated from serverfault.com Mar 8 '12 at 23:55

This question came from our site for system and network administrators.

In regards to your edit, I'm sorely confused now. You reported the original problem is that jobs are dying when you log out, but your edit is saying that "with the simplest job" it didnt die... – Patrick Mar 14 '12 at 12:52
I reported that jobs die when you logout without redirecting the streams and the opposite does not occur when you do it. – Luis Fernando Alen Mar 14 '12 at 15:51
So, you've shown that it doesnt die when you redirect the streams, now do the same test (while true; do sleep 1; done) without redirection, notice it stays running. – Patrick Mar 14 '12 at 16:02
Jobs wont die if you exit the shell with exit or logout. 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 your job.sh is doing we might be able to identify such issues. – Patrick Mar 15 '12 at 3:25

As for daemons doing it, that's because they want any output or error messages they might produce to be discarded no matter how you redirect a process's input and output streams, it will still be SIGHUP'd if it's attached to a session and that session is closed to leave processes running.

To leave processes running, there are few approaches:

  1. detach them from the session — daemons do this by forking a new process and then exiting the original process; now the new process has no parent and is adopted by init you can also accomplish that using the bash internal command disown

  2. use nohup to block the process from receiving the SIGHUP when the session dies; the process doesn't get SIGHUP, doesn't exit, its parent dies and init adopts it

  3. attach it to a session that won't die — use screen

Please recheck your work on statement "Jobs aren't killed at logout when executed with > /dev/null 2>&1 &"

share|improve this answer
That's not the behavior I see on my CentOS 5.7 machines. When I execute jobs with "> /dev/null 2>&1 &" and logout they're not killed and are somehow adopted by init. By the way, I'm executing single jobs and not a job like you described in approach A. – Luis Fernando Alen Mar 9 '12 at 4:40
Regarding adoption by init when you logout on Linux, in my experience it's a feature of bash, not necessarily to protect hangup signals as nohup does. – Sergio Mar 13 '12 at 23:14
Sergio, if it's a bash feature, why the jobs are killed when you logout without redirecting the streams and the opposite does not occur when you do it? That's what I don't get... Nevertheless, it might make sense. It seems that on AIX, which has the Korn shell as its default, jobs are killed when you logout, no matter how you redirect the streams. – Luis Fernando Alen Mar 14 '12 at 1:51
Processes are adopted by init when their parent dies. – Patrick Mar 14 '12 at 3:40
@LuisFernandoAlen what number-person described is correct. There's something else wrong with your test. Your job.sh is obviously doing something complex. I suggest you redo your test with a basic script like while true; do sleep 1; done. It will work perfectly fine. – Patrick Mar 14 '12 at 3:52

It's the behavior of jobs on bash as far as I know, I did a test here and it kept running with those streams opened as well:

sergiopa@sergiopa:~/Downloads$ find / -print >/dev/null 2>&1 &
[1] 14152
sergiopa@sergiopa:~/Downloads$ jobs
[1]+  Running                 find / -print > /dev/null 2>&1 &
sergiopa@sergiopa:~/Downloads$ ps -ef | grep find
sergiopa 14152 13913 10 15:47 pts/18   00:00:01 find / -print
sergiopa 14195 13913  0 15:48 pts/18   00:00:00 grep --color=auto find

sergiopa@sergiopa:~/Downloads$ lsof -c find
find    14152 sergiopa  cwd    DIR  252,1     4096     4808 /usr/src/linux-headers-2.6.35-22/drivers/rapidio
find    14152 sergiopa  rtd    DIR  252,1     4096        2 /
find    14152 sergiopa  txt    REG  252,1   141980   391686 /usr/bin/find
find    14152 sergiopa  mem    REG  252,1  1421892    26489 /lib/libc-2.12.1.so
find    14152 sergiopa  mem    REG  252,1   118084    26452 /lib/ld-2.12.1.so
find    14152 sergiopa  mem    REG  252,1   149392    26485 /lib/libm-2.12.1.so
find    14152 sergiopa  mem    REG  252,1    30684    26469 /lib/librt-2.12.1.so
find    14152 sergiopa  mem    REG  252,1   121578    26490 /lib/libpthread-2.12.1.so
find    14152 sergiopa  mem    REG  252,1  2768240   391861 /usr/lib/locale/locale-archive
find    14152 sergiopa    0u   CHR 136,18      0t0       21 /dev/pts/18 <<<<
find    14152 sergiopa    1w   CHR    1,3      0t0     4293 /dev/null   <<<<
find    14152 sergiopa    2w   CHR    1,3      0t0     4293 /dev/null   <<<<
find    14152 sergiopa    3r   DIR  252,3     4096 16777344 /home/sergiopa/Downloads
find    14152 sergiopa    4r   DIR  252,3     4096 16777344 /home/sergiopa/Downloads
find    14152 sergiopa    5r   DIR  252,1     4096     4808 /usr/src/linux-headers-2.6.35-22/drivers/rapidio

See lines with '<<<<', those are STDIN,STDOUT and STDERR. Closed the shell but find is still running.

Hope it helps!

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.