Why jobs aren't killed at logout when executed with “> /dev/null 2>&1 &”? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election Results Why I closed the “Why is Kali so hard” questionWhy can't I read /dev/stdout with a text editor?why did a local crash kill my remote jobs?Kill job in subshell with set -mLog background jobs started in all the manually started shells?

Is it fair for a professor to grade us on the possession of past papers?

Output the ŋarâþ crîþ alphabet song without using (m)any letters

What does this icon in iOS Stardew Valley mean?

How to find out what spells would be useless to a blind NPC spellcaster?

What is the logic behind the Maharil's explanation of why we don't say שעשה ניסים on Pesach?

String `!23` is replaced with `docker` in command line

How come Sam didn't become Lord of Horn Hill?

Is it ethical to give a final exam after the professor has quit before teaching the remaining chapters of the course?

Echoing a tail command produces unexpected output?

Identifying polygons that intersect with another layer using QGIS?

What is known about the Ubaid lizard-people figurines?

Why do people hide their license plates in the EU?

How does the particle を relate to the verb 行く in the structure「A を + B に行く」?

Why is "Consequences inflicted." not a sentence?

Why are there no cargo aircraft with "flying wing" design?

Can a USB port passively 'listen only'?

When a candle burns, why does the top of wick glow if bottom of flame is hottest?

Generate an RGB colour grid

Why light coming from distant stars is not discrete?

How to tell that you are a giant?

At the end of Thor: Ragnarok why don't the Asgardians turn and head for the Bifrost as per their original plan?

What's the purpose of writing one's academic biography in the third person?

Denied boarding although I have proper visa and documentation. To whom should I make a complaint?

What exactly is a "Meth" in Altered Carbon?



Why jobs aren't killed at logout when executed with “> /dev/null 2>&1 &”?



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
2019 Community Moderator Election Results
Why I closed the “Why is Kali so hard” questionWhy can't I read /dev/stdout with a text editor?why did a local crash kill my remote jobs?Kill job in subshell with set -mLog background jobs started in all the manually started shells?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








5















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









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


















5















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









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














5












5








5


1






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









share|improve this question
















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






stdout job-control






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 15 '12 at 20:06









Stéphane Gimenez

19.8k25275




19.8k25275










asked Mar 8 '12 at 22:58









Luis Fernando AlenLuis Fernando Alen

1294




1294




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


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









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


















  • 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

















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





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





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





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






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











2 Answers
2






active

oldest

votes


















3














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


















0














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$

sergiopa@sergiopa:~/Downloads$ lsof -c find
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
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
sergiopa@sergiopa:~/Downloads$


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






share|improve this answer

























    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f33815%2fwhy-jobs-arent-killed-at-logout-when-executed-with-dev-null-21%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    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















    3














    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













    3












    3








    3







    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















    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














    share|improve this answer



    share|improve this answer








    edited Mar 15 '12 at 20:09









    Stéphane Gimenez

    19.8k25275




    19.8k25275










    answered Mar 8 '12 at 23:30









    72616b65736872616b657368

    664




    664












    • 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

















    • 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
















    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





    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






    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





    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





    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





    @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













    0














    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$

    sergiopa@sergiopa:~/Downloads$ lsof -c find
    COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
    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
    sergiopa@sergiopa:~/Downloads$


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






    share|improve this answer





























      0














      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$

      sergiopa@sergiopa:~/Downloads$ lsof -c find
      COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
      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
      sergiopa@sergiopa:~/Downloads$


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






      share|improve this answer



























        0












        0








        0







        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$

        sergiopa@sergiopa:~/Downloads$ lsof -c find
        COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
        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
        sergiopa@sergiopa:~/Downloads$


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






        share|improve this answer















        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$

        sergiopa@sergiopa:~/Downloads$ lsof -c find
        COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
        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
        sergiopa@sergiopa:~/Downloads$


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







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 11 hours ago









        Rui F Ribeiro

        42.1k1484142




        42.1k1484142










        answered Mar 15 '12 at 18:50









        SergioSergio

        11




        11



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f33815%2fwhy-jobs-arent-killed-at-logout-when-executed-with-dev-null-21%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            -job-control, stdout

            Popular posts from this blog

            Frič See also Navigation menuinternal link

            Identify plant with long narrow paired leaves and reddish stems Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?What is this plant with long sharp leaves? Is it a weed?What is this 3ft high, stalky plant, with mid sized narrow leaves?What is this young shrub with opposite ovate, crenate leaves and reddish stems?What is this plant with large broad serrated leaves?Identify this upright branching weed with long leaves and reddish stemsPlease help me identify this bulbous plant with long, broad leaves and white flowersWhat is this small annual with narrow gray/green leaves and rust colored daisy-type flowers?What is this chilli plant?Does anyone know what type of chilli plant this is?Help identify this plant

            fontconfig warning: “/etc/fonts/fonts.conf”, line 100: unknown “element blank” The 2019 Stack Overflow Developer Survey Results Are In“tar: unrecognized option --warning” during 'apt-get install'How to fix Fontconfig errorHow do I figure out which font file is chosen for a system generic font alias?Why are some apt-get-installed fonts being ignored by fc-list, xfontsel, etc?Reload settings in /etc/fonts/conf.dTaking 30 seconds longer to boot after upgrade from jessie to stretchHow to match multiple font names with a single <match> element?Adding a custom font to fontconfigRemoving fonts from fontconfig <match> resultsBroken fonts after upgrading Firefox ESR to latest Firefox