2010-11-28 64 views
16

据我所知,这些书和bash手册就是这样。当用户从bash注销时,如果用户不使用nohup或disown,用户启动的所有后台作业都将自动终止。但是今天我测试了它:注销时bash如何处理作业?

  1. 登录到我的gnome桌面并访问了gnome-terminal。
  2. 有终端两个选项卡和一个我创建了一个新的用户名为测试,并记录在作为测试

    su - test 
    
  3. 启动的脚本。

    cat test.sh 
    #!/bin/bash 
    sleep 60 
    printf "hello world!!" 
    exit 0 
    
    
    ./test.sh & 
    
  4. 之后,我注销测试和关闭的选项卡

  5. 在接下来的标签我exected的ps aux为根,发现作业仍在运行。

这是怎么发生的?

+0

我注意到这个问题,目前还没有一个公认的答案,并且有上[苏]类似的新问题,这可能是有趣的阅读:HTTP:// superuser.com/questions/662431/what-exactly-determines-if-a-backgrounded-job-is-killed-when-the-shell-is-exited](http://superuser.com/questions/662431/what - 完全确定 - 如果一个后台工作被杀 - 当这个shell被退出) – Hennes 2013-10-21 16:13:05

回答

2

只有交互式shell在关闭它们时才会终止作业。其他shell(例如你使用su - username得到的那些)不会这样做。而交互式shell只会杀死直接的子进程。

+0

我没有`su`的mac上的相同技巧。 – khachik 2010-11-28 20:23:11

+0

什么是交互式shell? – Unni 2010-11-28 20:35:42

12

运行后台作业在退出时是否终止取决于shell。 Bash通常不会这样做,但可以配置为登录shell(shopt -s huponexit)。无论如何,在控制过程(例如登录shell)终止之后,不可能访问tty。

情况那些总是引起SIGHUP包括:在前台

  • 凡是当tty被关闭。 (SIGCONTSIGHUP)。在发生这种情况之前,壳通常会提醒你。

huponexit摘要:

$ shopt -s huponexit; shopt | grep huponexit 
huponexit  on 
# Background jobs will be terminated with SIGHUP when shell exits 

$ shopt -u huponexit; shopt | grep huponexit 
huponexit  off 
# Background jobs will NOT be terminated with SIGHUP when shell exits