1. 程式人生 > >Linux作業控制

Linux作業控制

spa span 顯示 conf amp config pan 模式 前臺

啟動、停止、無條件終止以及恢復作業的這些功能統稱為作業控制,通過作業控制,你就能完全控制shell中正在運行的所有作業。

標註:Ctrl+C組合鍵結束正在運行的作業,Ctrl+Z組合鍵暫停正在運行的作業。

1、查看作業

jobs命令允許查看shell當前正在處理的作業:

[[email protected] home]# ping 192.168.10.10

PING 192.168.10.10(192.168.10.10) 56(84) bytes of data.

64 bytes from 192.168.10.10: icmp_seq=1 ttl=64 time=2.92 ms

64 bytes from 192.168.10.10: icmp_seq=2 ttl=64 time=0.555 ms

^Z #Ctrl+Z組合鍵暫停作業

[1]+ Stopped ping 192.168.10.10

[[email protected] home]# jobs -l

[1]+ 4227 停止 ping 192.168.10.10

標註:jobs命令顯示作業時,會看見帶加號的作業會被當作默認的作業,帶減號的作業是後面要執行的作業,如何多個作業一般只有一個加號和一個減號,默認作業執行加號後再執行減號的作業。

[[email protected] home]# jobs -l

[1] 4227 停止 ping 192.168.10.10

[2] 4241 停止 ./test.sh

[3]- 4244 停止 ./test.sh

[4]+ 4246 停止 ./test.sh

2、重啟暫停的作業

在bash作業控制中,可以將已暫停的作業作為後臺進程或前臺進程重啟。

(1)、以後臺模式重啟一個作業,可以使用bg命令加上作業號:

[[email protected] home]# cat test.sh

#!/bin/bash

ping -c 5 192.168.10.254 > /home/ifconfig.txt

[[email protected] home]# ./test.sh

^Z

[1]+ Stopped ./test.sh

[[email protected] home]# cat ifconfig.txt

PING 192.168.10.254 (192.168.10.254) 56(84) bytes of data.

64 bytes from 192.168.10.254: icmp_seq=1 ttl=125 time=0.633 ms

[[email protected] home]# bg 1

[1]+ ./test.sh &

[[email protected] home]# jobs

[1]+ Done ./test.sh

[[email protected] home]# cat ifconfig.txt

PING 192.168.10.254 (192.168.10.254) 56(84) bytes of data.

64 bytes from 192.168.10.254: icmp_seq=1 ttl=125 time=0.633 ms

64 bytes from 192.168.10.254: icmp_seq=2 ttl=125 time=17.8 ms

64 bytes from 192.168.10.254: icmp_seq=3 ttl=125 time=0.469 ms

64 bytes from 192.168.10.254: icmp_seq=4 ttl=125 time=0.713 ms

64 bytes from 192.168.10.254: icmp_seq=5 ttl=125 time=0.536 ms

--- 192.168.10.254 ping statistics ---

5 packets transmitted, 5 received, 0% packet loss, time 15229ms

rtt min/avg/max/mdev = 0.469/4.030/17.802/6.886 ms

(2)、以後臺模式重啟一個作業,可以使用bg命令加上作業號:

[[email protected] home]# cat test.sh

#!/bin/bash

ping -c 5 192.168.10.254 > /home/ifconfig.txt

[[email protected] home]# ./test.sh

^Z

[1]+ Stopped ./test.sh

[[email protected] home]# cat ifconfig.txt

PING 192.168.10.254 (192.168.10.254) 56(84) bytes of data.

64 bytes from 192.168.10.254: icmp_seq=1 ttl=125 time=0.573 ms

[[email protected] home]# fg 1

./test.sh

[[email protected] home]# cat ifconfig.txt

PING 192.168.10.254 (192.168.10.254) 56(84) bytes of data.

64 bytes from 192.168.10.254: icmp_seq=1 ttl=125 time=0.573 ms

64 bytes from 192.168.10.254: icmp_seq=2 ttl=125 time=1.89 ms

64 bytes from 192.168.10.254: icmp_seq=3 ttl=125 time=1.87 ms

64 bytes from 192.168.10.254: icmp_seq=4 ttl=125 time=2.20 ms

64 bytes from 192.168.10.254: icmp_seq=5 ttl=125 time=0.773 ms

--- 192.168.10.254 ping statistics ---

5 packets transmitted, 5 received, 0% packet loss, time 25642ms

rtt min/avg/max/mdev = 0.573/1.462/2.204/0.659 ms

3、結束後臺運行或暫停的作業

使用kill命令即可殺死進程,需要註意如果作業有子進程和父進程的,如果直接結束作業父進程,子進程還是會繼續運行,所以,需要先結束子進程再結束父進程,ps命令可以查看作業的子進程和父進程,jobs只能看到父進程。

[[email protected] home]# cat test.sh

#!/bin/bash

ping -c 5 10.98.97.1 > /home/ifconfig.txt

[[email protected] home]# cat ifconfig.txt

PING 10.98.97.1 (10.98.97.1) 56(84) bytes of data.

64 bytes from 10.98.97.1: icmp_seq=1 ttl=125 time=0.565 ms

[[email protected] home]# ./test.sh

^Z

[1]+ Stopped ./test.sh

[[email protected] home]# jobs -l

[1]+ 4346 停止 ./test.sh #父進程

[[email protected] home]# ps au | grep T

USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND

root 4346 0.0 0.0 106008 1144 pts/0 T 23:28 0:00 /bin/bash ./test.sh #父進程

root 4347 0.0 0.0 103164 676 pts/0 T 23:28 0:00 ping -c 5 10.98.97.1 #子進程

root 4350 0.0 0.0 103160 816 pts/0 S+ 23:29 0:00 grep T

[[email protected] home]# kill -9 4347 #先結束子進程

[[email protected] home]# kill -9 4346 #最後結束父進程

[[email protected] home]# jobs

[1]+ 已殺死 ./test.sh

Linux作業控制