天天看點

linux 怎麼關閉終端後程式仍然運作,幾種退出Shell終端後程序仍然保持運作的方法...

有時候,你期望關閉Shell 終端後,運作的作業不會退出,簡單地在指令後加&是行不通的,以下總結幾種處理方法:

1.NOHUP ------  run a command immune to hangups, with output to a non-tty

eg:

[[email protected] iWas]# nohup ping laibin.com.cn >/tmp/out.log 2>&1 &

[1] 7921

[[email protected] iWas]# jobs

[1]+  Running                 nohup ping laibin.com.cn >/tmp/out.log 2>&1 &

[[email protected] iWas]#

2. DISOWN ----- Without options, each jobspec is  removed  from  the  table  of  active  jobs.

[ro[email protected] iWas]# cp -r /mnt/pspch12/iscw/iwas/u0620.46.linux/ .

[1]+  Stopped                 cp -i -r /mnt/pspch12/iscw/iwas/u0620.46.linux/ .

[[email protected] iWas]# bg 1

[1]+ cp -i -r /mnt/pspch12/iscw/iwas/u0620.46.linux/ . &

[[email protected] iWas]# jobs

[1]+  Running                 cp -i -r /mnt/pspch12/iscw/iwas/u0620.46.linux/ . &

[[email protected] iWas]# disown -ah

[[email protected] iWas]# exit

3.Screen1.create a new screen:

$ screen

or create a new screen running in background

$ screen -dmS 2.do anything you want in this screen

3.you can exit from this screen by "ctrl+a+d" or just close the client session

eg:

[[email protected] iWas]# screen -dmS Urumchi

[[email protected] iWas]# ping laibin.com.cn

PING laibin.com.cn (9.181.64.233) 56(84) bytes of data.

64 bytes from laibin.com.cn (9.181.64.233): icmp_seq=0 ttl=64 time=0.397 ms

64 bytes from laibin.com.cn (9.181.64.233): icmp_seq=1 ttl=64 time=0.311 ms

64 bytes from laibin.com.cn (9.181.64.233): icmp_seq=2 ttl=64 time=1.15 ms

4.restore your screen from any box

check the screen list in your box:

$ screen -list

select the screen you want to restore:

$ screen -r (if only one screen available now)

$ screen -r (restore a specified screen which create by "-d" param )

$ screen -r (restore the screen with specified pid)

eg:

[[email protected] ~]# screen -list

There is a screen on:

8530.Urumchi    (Detached)

1 Socket in /tmp/screens/S-root.

[[email protected] ~]# screen -r Urumchi

ms

64 bytes from laibin.com.cn (9.181.64.233): icmp_seq=15 ttl=64 time=0.186 ms

64 bytes from laibin.com.cn (9.181.64.233): icmp_seq=16 ttl=64 time=0.186 ms

64 bytes from laibin.com.cn (9.181.64.233): icmp_seq=17 ttl=64 time=0.182 ms

4. VNC: jobs which run from VNC won't be stopped even after exit VNC. How about Kill VNC session? (Not sure)