注销时bash如何处理作业?

Unn*_*nni 17 linux bash nohup

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

  1. 登录到我的gnome桌面并访问gnome-terminal.
  2. 终端中有两个选项卡,其中一个我创建了一个名为test的新用户并以test身份登录

    su - test
    
    Run Code Online (Sandbox Code Playgroud)
  3. 开始写一个剧本.

    cat test.sh
    #!/bin/bash
    sleep 60
    printf "hello world!!"
    exit 0
    
    
    ./test.sh &
    
    Run Code Online (Sandbox Code Playgroud)
  4. 之后我退出测试并关闭了标签

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

这是怎么回事?

jil*_*les 14

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

始终导致的情况SIGHUP包括:

  • 当tty关闭时,前景中的任何东西.
  • 任何后台作业,包括在shell终止时停止的进程(SIGCONTSIGHUP).在发生这种情况之前,贝壳通常会警告你.

huponexit摘要:

$ shopt -s huponexit
$ shopt huponexit
huponexit       on
Run Code Online (Sandbox Code Playgroud)

  • 所以你说,如果没有设置huponexit选项,那么bash不会杀死用户的后台作业? (2认同)

the*_*ejh 3

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