bash 注销时bash如何处理作业?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4298741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 23:00:04  来源:igfitidea点击:

How bash handles the jobs when logout?

linuxbashnohup

提问by Unni

As far as I understood from the books and bash manuals is that. When a user logs out from bash all the background jobs that is started by the user will automatically terminate, if he is not using nohup or disown. But today I tested it :

据我从书籍和 bash 手册中了解到的是。当用户从 bash 注销时,如果用户未使用 nohup 或 disown,则该用户启动的所有后台作业将自动终止。但是今天我测试了它:

  1. Logged in to my gnome desktop and accessed gnome-terminal.
  2. There are two tabs in the terminal and in one I created a new user called test and logged in as test

    su - test
    
  3. started a script.

    cat test.sh
    #!/bin/bash
    sleep 60
    printf "hello world!!"
    exit 0
    
    
    ./test.sh &
    
  4. After that I logged out of test and closed the tab

  5. In the next tab I exected ps aux as root and found that job is still running.
  1. 登录到我的 gnome 桌面并访问 gnome-terminal。
  2. 终端中有两个选项卡,其中一个我创建了一个名为 test 的新用户并以 test 身份登录

    su - test
    
  3. 启动了一个脚本。

    cat test.sh
    #!/bin/bash
    sleep 60
    printf "hello world!!"
    exit 0
    
    
    ./test.sh &
    
  4. 之后我退出了测试并关闭了选项卡

  5. 在下一个选项卡中,我以 root 身份执行 ps aux 并发现该作业仍在运行。

How this is happening ?

这是怎么回事?

回答by jilles

Whether running background jobs are terminated on exit depends on the shell. Bash normally does not do this, but can be configured to for login shells (shopt -s huponexit). In any case, access to the tty is impossible after the controlling process (such as a login shell) has terminated.

正在运行的后台作业是否在退出时终止取决于 shell。Bash 通常不会这样做,但可以配置为用于登录 shell ( shopt -s huponexit)。在任何情况下,在控制进程(例如登录 shell)终止后,都无法访问 tty。

Situations that do always cause SIGHUPinclude:

确实会导致的情况SIGHUP包括:

  • Anything in foreground when the tty is closed down.
  • Any background job that includes stopped processes when their shell terminates (SIGCONTand SIGHUP). Shells typically warn you before letting this happen.
  • tty 关闭时前景中的任何内容。
  • 任何包含在 shell 终止时停止的进程的后台作业(SIGCONTSIGHUP)。壳牌通常会在发生这种情况之前警告您。

huponexit summary:

huponexit总结:

  • On: Background jobs will be terminated with SIGHUP when shell exits

    $ shopt -s huponexit
    $ shopt huponexit
    huponexit       on
    
  • Off: Background jobs will NOT be terminated with SIGHUP when shell exits.

    $ shopt -u huponexit
    $ shopt huponexit
    huponexit       off
    
  • On:当 shell 退出时,后台作业将用 SIGHUP 终止

    $ shopt -s huponexit
    $ shopt huponexit
    huponexit       on
    
  • Off:当 shell 退出时,后台作业不会用 SIGHUP 终止。

    $ shopt -u huponexit
    $ shopt huponexit
    huponexit       off
    

回答by thejh

Only interactive shells kill jobs when you close them. Other shells (for example those you get by using su - username) don't do that. And interactive shells only kill direct subprocesses.

只有交互式 shell 在您关闭作业时才会终止作业。其他外壳(例如您通过使用 获得的外壳su - username)不会这样做。交互式 shell 只会杀死直接的子进程。