目錄php
參考:https://www.ibm.com/developerworks/cn/linux/l-cn-nohup/index.htmlhtml
$ mate-system-monitor
//運行中,終端被佔用;使用快捷鍵 [ctrl z] 暫停運行,返回終端
job1, 'mate-system-monitor' has stopped
$ jobs //查看後臺運行程序,有這個狀態爲中止的後臺程序
Job Group CPU State Command
1 7870 0% stopped mate-system-monitor
$ bg %1 //在後臺繼續運行程序
Send job 1 「mate-system-monitor」 to background
$ jobs //查看,狀態已變爲運行了
Job Group CPU State Command
1 7870 0% running mate-system-monitor
$ fg %1 //將後臺程序返回到前臺,
Send job 1, 「mate-system-monitor」 to foreground
// [ctrl c] //終止程序linux
$ mate-system-monitor &
$ jobs -l
Job Group CPU State Command
1 10084 4% running mate-system-monitor
$ mate-system-monitor & //至關與以下3個步驟:shell
當後臺運行程序有輸出時,會擾亂當前終端的內容。可用">filename 2>&1"來更改缺省的重定向文件名。
$ mate-system-monitor > mate-system-monitor_log.txt 2>&1 &
$ jobs
Job Group CPU State Command
1 10565 22% running mate-system-monitor > mate-system-monitor_log.txt 2>&1 &
這樣終端就不會收到干擾了。
$ sslocal -c s.json > sw_log 2>&1 &
$ jobs
Job Group CPU State Command
2 10880 12% running sslocal -c s.json > sw_log 2>&1 &
1 10565 11% running mate-system-monitor > mate-system-monitor_log.txt 2>&1 &
以上都是在某個終端執行,父進程都是終端。若父進程關閉,則會關閉下面全部子進程。json
nohup (no hangup 不掛起)放在命令的前面; 標準輸出和標準錯誤缺省會被重定向到nohup.out 文件中。
通常咱們可在結尾加上"&"來將命令同時放入後臺運行,也可用">filename 2>&1"來更改缺省的重定向文件名。
$ nohup mate-system-monitor > mate-system-monitor_log.txt 2>&1 &
$ jobs
Job Group CPU State Command
1 11164 2% running nohup mate-system-monitor > mate-system-monitor_log.txt 2>&1 &
$ exit //要退出終端會提示
There are still jobs active:
PID Command
12086 nohup mate-system-monitor > mate-system-monitor_log.txt 2>&1 &
A second attempt to exit will terminate them.
Use 'disown PID' to remove jobs from the list without terminating them.
若強行關閉後,上面進程的父進程由本來的bash的pid變爲1。windows
setsid setsid(8) run a program in a new session
$ setsid mate-system-monitor > mate-system-monitor_log.txt 2>&1 &
$ jobs
jobs: There are no jobs
Job 1, 'setsid mate-system-monitor > ma…' has ended
$ jobs
jobs: There are no jobs
父進程直接就是1了,jobs也看不到了,只能經過ps等查找了。
$ ps -ef |grep mate-system-monitor
toma 12799 12546 1 08:04 pts/1 00:00:14 mate-system-monitor
toma 13758 12546 0 08:18 pts/1 00:00:00 grep --color=auto mate-system-monitorbash
若運行命令時未加nohup 或者setsid,可用disown補救,
$ mate-system-monitor > mate-system-monitor_log.txt 2>&1 &
$ jobs
Job Group CPU State Command
1 14318 9% running mate-system-monitor > mate-system-monitor_log.txt 2>&1 &
$ disown %1
$ jobs
jobs: There are no jobs
$ ps -ef |grep mate-system-monitor
toma 14318 12546 1 08:24 pts/1 00:00:25 mate-system-monitor
toma 16098 12546 0 08:51 pts/1 00:00:00 grep --color=auto mate-system-monitor
父進程變爲1,已脫離當前的終端。
以上命令實如今後臺運行,還脫離了當前終端的影響,可是沒法再從新鏈接到這個會話。
session
注:這部份內容,未測試過,徹底摘抄2008 年的原文:https://www.ibm.com/developerworks/cn/linux/l-cn-nohup/ssh
$ sudo pacman -Ss screen
extra/screen 4.6.2-1
Full-screen window manager that multiplexes a physical terminal
簡單的說,screen 提供了ANSI/VT100 的終端模擬器,使它可以在一個真實終端下運行多個全屏的僞終端。screen命令提供了分離和從新鏈接一個會話的功能。
經常使用選項:
用screen -dmS session name來創建一個處於斷開模式下的會話(並指定其會話名)。
用screen -list 來列出全部會話。
用screen -r session name來從新鏈接指定會話。
用快捷鍵CTRL-a d 來暫時斷開當前會話。測試
screen 示例
[root@pvcent107 ~]# screen -dmS Urumchi
[root@pvcent107 ~]# screen -list
There is a screen on:
12842.Urumchi (Detached)
1 Socket in /tmp/screens/S-root.
[root@pvcent107 ~]# screen -r Urumchi
當咱們用「-r」鏈接到 screen 會話後,咱們就能夠在這個僞終端裏面隨心所欲,不再用擔憂 HUP 信號會對咱們的進程形成影響,也不用給每一個命令前都加上「nohup」或者「setsid」了。這是爲何呢?讓我來看一下下面兩個例子吧。
1. 未使用 screen 時新進程的進程樹
[root@pvcent107 ~]# ping www.google.com &
[1] 9499
[root@pvcent107 ~]# pstree -H 9499
init─┬─Xvnc
├─acpid
├─atd
├─2*[sendmail]
├─sshd─┬─sshd───bash───pstree
│ └─sshd───bash───ping
咱們能夠看出,未使用 screen 時咱們所處的 bash 是 sshd 的子進程,當 ssh 斷開鏈接時,HUP 信號天然會影響到它下面的全部子進程(包括咱們新創建的 ping 進程)。
2. 使用了 screen 後新進程的進程樹
[root@pvcent107 ~]# screen -r Urumchi
[root@pvcent107 ~]# ping www.ibm.com &
[1] 9488
[root@pvcent107 ~]# pstree -H 9488
init─┬─Xvnc
├─acpid
├─atd
├─screen───bash───ping
├─2*[sendmail]
而使用了 screen 後就不一樣了,此時 bash 是 screen 的子進程,而 screen 是 init(PID爲1)的子進程。那麼當 ssh 斷開鏈接時,HUP 信號天然不會影響到 screen 下面的子進程了。
=========https://wiki.archlinux.org/index.php/GNU_Screen -4 Resolve hostnames only to IPv4 addresses. 僅將主機名解析爲IPv4地址。 -6 Resolve hostnames only to IPv6 addresses. 僅將主機名解析爲IPv6地址。 -a Force all capabilities into each window's termcap. 強制全部功能進入每一個窗口的termcap。 -A -[r|R] Adapt all windows to the new display width & height. 使全部窗口適應新的顯示寬度和高度。 -c file Read configuration file instead of '.screenrc'. 讀取配置文件而不是「.screenrc」。-d (-r) Detach the elsewhere running screen (and reattach here). 分離其餘正在運行的屏幕(並從新鏈接到此處)。-dmS name Start as daemon: Screen session in detached mode. 做爲守護程序啓動:處於分離模式的屏幕會話。-D (-r) Detach and logout remote (and reattach here). 分離並註銷遠程(並在此處從新鏈接)。-D -RR Do whatever is needed to get a screen session. 作任何須要的屏幕會話。-e xy Change command characters. 更改命令字符。-f Flow control on, -fn = off, -fa = auto. 流量控制開啓,-fn =關閉,-fa = auto。-h lines Set the size of the scrollback history buffer. 設置回滾歷史記錄緩衝區的大小。-i Interrupt output sooner when flow control is on. 當流量控制打開時,中斷輸出更快。-l Login mode on (update /var/run/utmp), -ln = off. 登陸模式開啓(更新/ var / run / utmp), - ln =關閉。-ls [match] or 要麼-list Do nothing, just list our SockDir [on possible matches]. 什麼都不作,只需列出咱們的SockDir [在可能的比賽中]。-L Turn on output logging. 打開輸出記錄。-Logfile file Set logfile name. 設置日誌文件名稱。-m ignore $STY variable, do create a new screen session. 忽略$ STY變量,以建立新的屏幕會話。-O Choose optimal output rather than exact vt100 emulation. 選擇最佳輸出而不是精確的vt100仿真。-p window Preselect the named window if it exists. 若是存在,則預選指定的窗口。-q Quiet startup. Exits with non-zero return code if unsuccessful. 安靜的啓動。若是不成功,則退出非零返回碼。-Q Commands will send the response to the stdout of the querying process. 命令會將響應發送到查詢過程的標準輸出。-r [session] Reattach to a detached screen process. 從新鏈接到分離的屏幕進程。-R Reattach if possible, otherwise start a new session. 若是可能,從新鏈接,不然,開始新的會話。-s shell Shell to execute rather than $SHELL. Shell執行而不是$ SHELL。-S sockname Name this session <pid>.sockname instead of <pid>.<tty>.<host>. 將此會話命名爲<pid> .sockname而不是<pid>。<tty>。<host>。-t title Set title. (window's name). 設置標題。 (窗口的名字)。-T term Use term as $TERM for windows, rather than "screen". 使用術語做爲Windows的$ TERM,而不是「屏幕」。-U Tell screen to use UTF-8 encoding. 告訴屏幕使用UTF-8編碼。-v Print "Screen version 4.06.02 (GNU) 23-Oct-17". 打印「屏幕版本4.06.02(GNU)23-Oct-17」。-wipe [match] Do nothing, just clean up SockDir [on possible matches]. 什麼都不作,只是清理SockDir [在可能的比賽中]。-x Attach to a not detached screen. (Multi display mode). 附加到未分離的屏幕。 (多顯示模式)。-X Execute <cmd> as a screen command in the specified session. 在指定的會話中執行<cmd>做爲屏幕命令。