Shell補充之後臺執行指令碼程式
阿新 • • 發佈:2020-10-12
在實際工作中,一般會通過客戶端SSH連線伺服器,因此可能就會有在指令碼或命令執行期間不能中斷的需求,若中斷,則會前功盡棄,更可能會破壞系統資料。為了預防因為ssh連結視窗的關閉而導致指令碼執行中斷,我們可以把指令碼放在後臺執行。
案例說明:
指令碼資訊
[root@node1 scripts]# cat while1.sh #! /bin/bash while true do uptime >> /tmp/uptime.log sleep 2 done
把指令碼放入後臺執行
[root@node1 scripts]# bash while1.sh & [1] 23277[root@node1 scripts]# jobs [1]+ Running bash while1.sh & [root@node1 scripts]#
把後臺指令碼轉到前臺執行
[root@node1 scripts]# jobs [1]+ Running bash while1.sh & #後臺指令碼執行時的jobs編號 [root@node1 scripts]# fg 1 #fg加jobs相應指令碼或程式的編號,可以把相應的指令碼或程式轉到前臺執行 bash while1.sh
[root@node1 scripts]# jobs [1]+ Running bash while1.sh & [root@node1 scripts]# fg 1 bash while1.sh #ctrl+c殺掉在前臺執行的指令碼或程式
kill命令關閉jobs任務指令碼
[root@node1 scripts]# bash while1.sh & [1] 23720 [root@node1 scripts]# bash while1.sh & [2] 23723 [root@node1 scripts]# bash while1.sh & [3] 23727 [root@node1 scripts]# jobs [1] Running bash while1.sh & [2]- Running bash while1.sh & [3]+ Running bash while1.sh & 殺掉jobs中的第二個指令碼 [root@node1 scripts]# jobs [1] Running bash while1.sh & [2]- Running bash while1.sh & [3]+ Running bash while1.sh & [root@node1 scripts]# kill %2 [root@node1 scripts]# jobs [1] Running bash while1.sh & [2]- Terminated bash while1.sh #jobs中第2個執行的指令碼已經被殺掉 [3]+ Running bash while1.sh &