bash 中的&符号能否一直保持后台运行的疑问 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
yadam

bash 中的&符号能否一直保持后台运行的疑问

  •  
  •   yadam Jan 26, 2015 4827 views
    This topic created in 4113 days ago, the information mentioned may be changed or developed.

    问题是: &字符能否保持ssh的session结束之后 一直 运行?
    一直对搜这种特殊字符无能, Google了关键字 ssh Ampersand 换了几个描述也没找到想要的答案.搜到的基本全是怎么样nohup和&结合的描述


    软件没有写成守护进程,但是还想后台运行, 一直以来用的是screen, 最近使用 "./app & " 这种方式启动,发现软件会退出, 所以想排除这种使用方式的问题.

    Supplement 1    Jan 27, 2015
    多谢各位指导, 综合几位的答案如下, 请各位看看有没有错误或者漏的地方:
    判断SSH退出后子进程是否会被关闭有以下几种情况:

    1. 如果打开了huponexit,所有的子进程在SSH退出后全都会接到SIGHUP信号,除非程序自己处理
    SIGHUP信号并且不退出,否则程序退出
    2. huponexit打开时,可以使用nohup或者disown使进程不退出.原理是nohup捕获了SIGHUP信号,disown是告诉SHELL不给程序发送SIGHUP信号
    3. huponexit没有打开时,如果SSH正常logout,后台子进程不会自己退出
    4. huponexit没有打开时,如果SSH非正常退出(比如断网,强行关闭SSH软件),所有子进程都会被kill
    5. 正确的使用screen也可以防止子进程在SSH断开时退出

    查看huponexit是否打开的方法
    shopt | grep huponexit

    &是干什么用的?
    man bash可以看到如下描述

    If a command is terminated by the control operator &, the shell executes the command in the background in a subshell. The shell does not wait for the command to finish, and the return status is 0.

    为什么nohup有时要和&一起使用?
    man nohup可以看到下面这句

    'nohup' does not automatically put the command it runs in the background; you must do that explicitly, by ending the command line with an `&'.

    参考

    http://hacktux.com/bash/ampersand
    http://stackoverflow.com/questions/15595374/whats-the-difference-between-nohup-and-ampersand
    http://stackoverflow.com/questions/4298741/how-bash-handles-the-jobs-when-logout
    http://superuser.com/questions/662431/what-exactly-determines-if-a-backgrounded-job-is-killed-when-the-shell-is-exited

    @wzxjohn @66450146 @kslr @rrfeng @meta @Tink @whuhacker @atupal @ryd994 @lululau
    11 replies    2015-01-26 20:33:30 +08:00
    wzxjohn
        1
    wzxjohn  
       Jan 26, 2015 via iPhone   1
    nohup 一起用确实可以,不过你有 screen 为何不用。。。一直都用 screen 的路过。。。
    66450146
        2
    66450146  
       Jan 26, 2015   1
    还是用 screen / tmux 吧……
    kslr
        3
    kslr  
       Jan 26, 2015   2
    & 大致意思是利用SHELL的技巧,新建不属于当前终端的进程,这样终端关闭的信号就不会影响到。
    这里有一篇文章:
    http://www.ibm.com/developerworks/cn/linux/l-cn-nohup/
    rrfeng
        4
    rrfeng  
       Jan 26, 2015   1
    bash 默认设置 shopt huponexit 是 off 的。
    所以 cmd & 可以一直后台运行,exit ssh 也没有影响。

    但是,强行断开 ssh 连接,也就是非常规的 exit 比如网络断了之类的,还是会终止。

    使用 nohup 执行,则异常断开也可以保持后台运行。

    还有 setsid 和 disown 方式。
    meta
        5
    meta  
       Jan 26, 2015   1
    如果你的程序不会向标准输出和错误输出写任何东西就可以一直运行,否则的话因为你已经把这两个输出关闭掉了,一般会出错退出。
    你可以在程序内部处理,把这两个输出重定向,或者在shell中重定向。比如 > xxx.log 2>&1。如果用nohup的话,默认会重定向到nohup.out
    Tink
        6
    Tink  
    PRO
       Jan 26, 2015 via iPhone   1
    nohup command &
    rrfeng
        7
    rrfeng  
       Jan 26, 2015   1
    对,还有一个就是有些程序会判断 stdout/stderr 是否可用。否则会自行关闭。
    方法是加上 &> /tmp/log 或者 /dev/null 重定向掉
    whuhacker
        8
    whuhacker  
       Jan 26, 2015   1
    pstree 查看一下进程树,就知道 ssh 进程下的子进程,在你断开 ssh 连接后都会被杀掉
    而 screen / tmux 会把进程都挪出 ssh 的魔爪……
    atupal
        9
    atupal  
       Jan 26, 2015   1
    我一般都
    $ (./xxx.sh 1>stdout.log 2>error.log & )

    然后就是在 后台运行且 挂到 init 进程下,

    要看输出就
    $ tail -f stdout.log

    要看 错误就
    $ tail -f error.log
    ryd994
        10
    ryd994  
       Jan 26, 2015 via Android   1
    其实还是screen好用啊
    lululau
        11
    lululau  
       Jan 26, 2015   2
    @meta 大多数人知道 SIGHUP 可能会使程序终止,而不知道往一个已经关闭了的文件写入数据也可能会使进程终止。hohup 其实有两个作用,一是将进程的SIGHUP handler 设为 IGNORE,二是重定向标准输出和标准错误。
    About     Help     Advertise     Blog     API     FAQ     Solana     2529 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 50ms UTC 00:03 PVG 08:03 LAX 17:03 JFK 20:03
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86