在上一章節中,講述了Linux process之基本原理與機制,以及控制jobs工作的方法,並讓工作執行可以在前景與背景之間切換,本章節,將繼續延續上一章節的內容,繼續講解有關於Linux process的原理。
當一個指令或是程式被執行起來的時候,運行的程式或是指令就是process,作業系統會針對每一個process程序配置一個id編號並有唯一性,這是用來識別這個process程序用的,除此之外,每個process程序可以接收訊號,訊號是讓作業系統能夠操控程序的狀態,相關的訊號列表如下說明:
可以使用kill指令並搭配訊號數字參數將指定的process id給停止或是砍掉,以sleep 100 &
指令為例子,並搭配不同的訊號數字來觀看停止process程序會有哪些的狀態,相關的指令執行後的結果如下:
[rockylinux@workstation ~]$ sleep 100 &
[2] 2595
[rockylinux@workstation ~]$ kill -1 2595
[rockylinux@workstation ~]$
[2]- Hangup sleep 100
[rockylinux@workstation ~]$ sleep 100 &
[2] 3541
[rockylinux@workstation ~]$
[rockylinux@workstation ~]$ kill -2 3541
[rockylinux@workstation ~]$
[2]- Interrupt sleep 100
[rockylinux@workstation ~]$
[rockylinux@workstation ~]$ kill -3 4206
[rockylinux@workstation ~]$
[2]- Quit (core dumped) sleep 100
[rockylinux@workstation ~]$
[rockylinux@workstation ~]$ kill -9 4226
[rockylinux@workstation ~]$
[2]- Killed sleep 100
[rockylinux@workstation ~]$
[rockylinux@workstation ~]$ kill -15 4227
[rockylinux@workstation ~]$
[2]- Terminated sleep 100
[rockylinux@workstation ~]$
[rockylinux@workstation ~]$ kill 4228
[rockylinux@workstation ~]$
[2]- Terminated sleep 100
[rockylinux@workstation ~]$ sleep 100 &
[2] 4237
[rockylinux@workstation ~]$ kill -19 4237
[2]+ Stopped sleep 100
[rockylinux@workstation ~]$ kill -18 4237
[rockylinux@workstation ~]$
[rockylinux@workstation ~]$
[rockylinux@workstation ~]$ jobs -l
[2]- 4237 Running sleep 100 &
[rockylinux@workstation ~]$
除了使用kill指令之外,pkill指的是可以帶使用者名稱或是群組名稱的參數並將使用使用者或是群組所執行的process程序給砍掉,相關的執行指令用法如下:
[rockylinux@workstation ~]$ pkill -U rockylinux
Connection to 192.168.0.21 closed by remote host.
Connection to 192.168.0.21 closed.
從上面的執行指令的輸出就可以發現,將rockylinux使用者所執行的process程序都被砍掉了,連遠端所建立的連線shell也被砍掉,因此導致遠端連線關閉與中斷。
此外,也可以使用killall
指令將有關於某個正在以此指令所執行的程序給砍掉,相關的執行指令的輸出訊息如下:
[rockylinux@workstation ~]$ sleep 100 &
[1] 4535
[rockylinux@workstation ~]$ sleep 100 &
[2] 4536
[rockylinux@workstation ~]$
[rockylinux@workstation ~]$ killall sleep
[1]- Terminated sleep 100
[2]+ Terminated sleep 100
[rockylinux@workstation ~]$
另外,可以使用w
這個指令來找到目前誰登入到這個使用者在做存取使用,相關的執行指令之輸出訊息如下:
[rockylinux@workstation ~]$ w
01:01:20 up 3:59, 1 user, load average: 0.01, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
rockylin pts/0 192.168.0.9 00:58 0.00s 0.13s 0.02s w
pgrep
指令是用來查看某個指定的使用者或是群組名稱正在以此名稱執行的程序有哪些,相關的指令執行方法與輸出的訊息如下:
[rockylinux@workstation ~]$ pgrep -l -u rockylinux
4395 systemd
4400 (sd-pam)
4415 pulseaudio
4416 sshd
4419 bash
4454 dbus-daemon
4459 gvfsd
4466 gvfsd-fuse
pstree
指令是將process以樹狀圖的方式呈現,可以看到process之間的關係,因為有的process會從某一個process中fork出來做執行,那主要process就是parent,而從parent process所fork出來的就叫做child process,而可以使用pstree
指令並搭配-p
指令並指定使用者名稱,便可以輸出以指定的使用者名稱執行相關指令的清單,相關的執行指令輸出的訊息如下:
[rockylinux@workstation ~]$ pstree -p rockylinux
sshd(4416)───bash(4419)───pstree(4703)
systemd(4395)─┬─(sd-pam)(4400)
├─dbus-daemon(4454)───{dbus-daemon}(4457)
├─gvfsd(4459)─┬─{gvfsd}(4461)
│ └─{gvfsd}(4463)
├─gvfsd-fuse(4466)─┬─{gvfsd-fuse}(4469)
│ ├─{gvfsd-fuse}(4470)
│ ├─{gvfsd-fuse}(4471)
│ ├─{gvfsd-fuse}(4474)
│ └─{gvfsd-fuse}(4478)
└─pulseaudio(4415)───{pulseaudio}(4490)
在Linux作業系統上,有平均載入的時間與其計算,就是先前的章節有提到CPU在處理每一個任務的時候會有request queue與waiting quque,一直不斷的把process在上述的queue佇列之間做切換,而可以使用uptime
這個指令來查看作業系統已經運行了多久,相關的執行指令的方式與輸出訊息如下:
[rockylinux@workstation ~]$ uptime
01:18:19 up 4:16, 2 users, load average: 0.04, 0.26, 0.14
可以從可以從上述的指令輸出訊息可以知道,目前有兩個使用者在這個作業系統上進行存取,4:16,指的是目前已經運行了4小時左右,另外也可以使用-p
參數讓輸出運行的時間之可讀性變高,相關的執行指令所輸出的訊息如下:
[rockylinux@workstation ~]$ uptime -p
up 4 hours, 21 minutes
[rockylinux@workstation ~]$
而作業系統上要查看所使用的CPU(中央處理器)資訊也是可以的,可以使用lscpu
指令或是將/proc/cpuinfo
的檔案內容印出來都可以,相關執行指令的方式與輸出的訊息如下:
[rockylinux@workstation ~]$ lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 2
On-line CPU(s) list: 0,1
Thread(s) per core: 1
Core(s) per socket: 2
Socket(s): 1
NUMA node(s): 1
Vendor ID: GenuineIntel
CPU family: 6
Model: 42
Model name: Intel(R) Core(TM) i5-2500K CPU @ 3.30GHz
Stepping: 7
CPU MHz: 3292.522
BogoMIPS: 6585.04
Hypervisor vendor: KVM
Virtualization type: full
L1d cache: 32K
L1i cache: 32K
L2 cache: 256K
L3 cache: 6144K
NUMA node0 CPU(s): 0,1
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx rdtscp lm constant_tsc rep_good nopl xtopology nonstop_tsc cpuid tsc_known_freq pni pclmulqdq ssse3 cx16 sse4_1 sse4_2 x2apic popcnt aes xsave avx hypervisor lahf_lm pti md_clear flush_l1d
[rockylinux@workstation ~]$
[rockylinux@workstation ~]$ cat /proc/cpuinfo
processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 42
model name : Intel(R) Core(TM) i5-2500K CPU @ 3.30GHz
stepping : 7
cpu MHz : 3292.522
cache size : 6144 KB
physical id : 0
siblings : 2
core id : 0
cpu cores : 2
apicid : 0
initial apicid : 0
fpu : yes
fpu_exception : yes
cpuid level : 13
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx rdtscp lm constant_tsc rep_good nopl xtopology nonstop_tsc cpuid tsc_known_freq pni pclmulqdq ssse3 cx16 sse4_1 sse4_2 x2apic popcnt aes xsave avx hypervisor lahf_lm pti md_clear flush_l1d
bugs : cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf mds swapgs itlb_multihit
bogomips : 6585.04
clflush size : 64
cache_alignment : 64
address sizes : 36 bits physical, 48 bits virtual
power management:
processor : 1
vendor_id : GenuineIntel
cpu family : 6
model : 42
model name : Intel(R) Core(TM) i5-2500K CPU @ 3.30GHz
stepping : 7
cpu MHz : 3292.522
cache size : 6144 KB
physical id : 0
siblings : 2
core id : 1
cpu cores : 2
apicid : 1
initial apicid : 1
fpu : yes
fpu_exception : yes
cpuid level : 13
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx rdtscp lm constant_tsc rep_good nopl xtopology nonstop_tsc cpuid tsc_known_freq pni pclmulqdq ssse3 cx16 sse4_1 sse4_2 x2apic popcnt aes xsave avx hypervisor lahf_lm pti md_clear flush_l1d
bugs : cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf mds swapgs itlb_multihit
bogomips : 6585.04
clflush size : 64
cache_alignment : 64
address sizes : 36 bits physical, 48 bits virtual
power management:
[rockylinux@workstation ~]$
最後,總結一下process管理的方式,若要查看process相關的一瞬間內容,可以用ps
指令並搭配一些參數做到最基本的查看,若要看到每個process之即時執行的狀態等相關的資訊,則可以使用top
指令做查看,或者額外安裝htop
這個套件,並使用htop
指令做查看也是可以,這個指令可以看到更多的process狀態資訊以及豐富的額外功能會顯示在process之即時輸出訊息中,相關安裝htop
指令相關的執行後輸出的資訊如下:
[rockylinux@workstation ~]$ sudo yum install epel-release
Last metadata expiration check: 1:33:11 ago on Wed 29 Sep 2021 12:23:18 AM CST.
Dependencies resolved.
============================================================================================================
Package Architecture Version Repository Size
============================================================================================================
Installing:
epel-release noarch 8-13.el8 extras 23 k
Transaction Summary
============================================================================================================
Install 1 Package
Total download size: 23 k
Installed size: 35 k
Is this ok [y/N]: y
Downloading Packages:
epel-release-8-13.el8.noarch.rpm 474 kB/s | 23 kB 00:00
------------------------------------------------------------------------------------------------------------
Total 36 kB/s | 23 kB 00:00
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : epel-release-8-13.el8.noarch 1/1
Running scriptlet: epel-release-8-13.el8.noarch 1/1
Verifying : epel-release-8-13.el8.noarch 1/1
Installed products updated.
Installed:
epel-release-8-13.el8.noarch
Complete!
[rockylinux@workstation ~]$ sudo yum update
Extra Packages for Enterprise Linux Modular 8 - x86_64 269 kB/s | 955 kB 00:03
Extra Packages for Enterprise Linux 8 - x86_64 347 kB/s | 10 MB 00:30
Last metadata expiration check: 0:00:02 ago on Wed 29 Sep 2021 01:57:04 AM CST.
由於htop
不在既有的鏡像裡面,因此需要先安裝epel-release(Extra Packages for Enterprise Linux (EPEL) ),後面的章節還會提到這些安裝的方式與執行的方法,接著安裝好之後,就可以安裝htop指令了,相關安裝指令執行輸出的訊息如下:
[rockylinux@workstation ~]$ sudo yum install -y htop
Last metadata expiration check: 0:02:04 ago on Wed 29 Sep 2021 01:57:04 AM CST.
Dependencies resolved.
============================================================================================================
Package Architecture Version Repository Size
============================================================================================================
Installing:
htop x86_64 3.0.5-1.el8 epel 142 k
Transaction Summary
============================================================================================================
Install 1 Package
Total download size: 142 k
Installed size: 326 k
Downloading Packages:
htop-3.0.5-1.el8.x86_64.rpm 394 kB/s | 142 kB 00:00
------------------------------------------------------------------------------------------------------------
Total 125 kB/s | 142 kB 00:01
warning: /var/cache/dnf/epel-2f52d25564da0fd7/packages/htop-3.0.5-1.el8.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID 2f86d6a1: NOKEY
Extra Packages for Enterprise Linux 8 - x86_64 1.6 MB/s | 1.6 kB 00:00
Importing GPG key 0x2F86D6A1:
Userid : "Fedora EPEL (8) <epel@fedoraproject.org>"
Fingerprint: 94E2 79EB 8D8F 25B2 1810 ADF1 21EA 45AB 2F86 D6A1
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-8
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : htop-3.0.5-1.el8.x86_64 1/1
Running scriptlet: htop-3.0.5-1.el8.x86_64 1/1
Verifying : htop-3.0.5-1.el8.x86_64 1/1
Installed products updated.
Installed:
htop-3.0.5-1.el8.x86_64
Complete!
[rockylinux@workstation ~]$
sleep 100
指令並試著將此process程序暫時停止。kill
指令將上述停止的程序重新運行。kill
指令用預設的訊號號碼將上述正在運行的指令給砍掉。sleep 100 &
指令。kill
指令將上述在背景運行的指令強制結束與關閉。sleep 100 &
指令3次,並確定此三個程序有在背景中確切的運行。