一、進程管理基礎知識
1、應用程序、系統、內核、Lib庫、硬件之間的關係:
在Linux上,應用程序都是對Kernel內核或者Libraries庫調用實現應用程序功能的
應用程序運行在用戶和內核之間,實現用戶空間和內核空間之間的交互
(用戶指令、內核或庫調用、內核特權指令)
內核負責進程管理、文件系統管理、網絡功能、內存管理、驅動管理、安全、硬件管理...
2、進程:Process
進程是運行起來的程序中的一個副本,使用內存及CPU進行庫、內核的調用等運算操作,是一個動態的實體,而程序則時一個靜態的文件。
3、進程的特性:
存在聲明週期
有自己的元數據:進程運行時間、PID、進程發起者...
4、Linu系統啓動流程:
(01、上電開機,BIOS自檢
(02、磁盤引導MBR
(03、grub引導菜單(Boot Loader)
(04、加載Kernel及必要組建
(05、啓動init進程,依據inittab設定運行級別
(06、init進程,執信rc.sysinit文件
(07、啓動內核模塊,執信不同級別的腳本程序
(08、執行/etc/rc.d/rc.loacl
(09、啓動mingetty,進入系統登陸界面
5、進程優先級0--139:
1--99:實時優先級(數字越大優先級越高)
100--139:靜態優先級(數字越小優先級越高)
Nice值:-20--19可以通過調整Nice值調整進程的靜態優先級(對應靜態優先級)
優先級=nice+120
6、進程隊列
等待隊列:等待運行的隊列
過期隊列:運行過的隊列,等待隊列運行完成後才能再次被運行調度
7、進程類型:
守護進程:daemon,由內核在系統引導過程中啓動的進程
用戶進程(前臺進程):通過終端啓動的進程
8、進程的狀態:
運行狀態:running
就緒準太:ready
睡眠狀態:
可中斷睡眠狀態:interruptable,可隨時啓動
不可終端睡眠狀態:uninterruptabl,不能被啓動
停止狀態:stopped,暫停在內存中,但不會被調用,除非手動啓動
僵死狀態:zombie
9、線程:一個進程可以分爲多個線程運行
10、常用進程管理工具
pstree # 進程樹查看工具
ps # 進程狀態查看工具
pgrep # 進程狀態過濾顯示命令
pidof # 根據進程名獲取PID
top # 進程動態信息查看
htop # 進程動態信息查看(top升級版,支持鼠標操作)
kill # 進程控制命令,通過向進程發送控制信號控制進程狀態
nice、renice # 進程優先級的調整
fuser # 查看佔用指定文件或者端口的進程
11、作業:爲實現同一個項目的多組進程
前臺作業:運行時佔據了終端的命令行窗口的作業
後臺作業:運行時後臺運行,不佔用終端的作業
二、pstree:進程樹查看工具(yum install -y psmisc)
pstree 選項 pid
常用選項
-a # 顯示該行程的完整指令及參數
-c # 如果有重覆的行程名, 則分開列出(默認會在前面加上 *)
-apnh # 顯示進程間的關係
-u # 顯示用戶名稱
示例:
[oneapm@ONEAPM-NGINX01 ~]$ pstree -apnhu 97712
nginx,97712,oneapm
├─nginx,97713
├─nginx,97714
└─nginx,97715
三、ps:進程狀態查看工具
ps 選項
常用選項
u # 以當前用戶爲中心組織進程狀態信息
a # 顯示與終端相關的進程
x # 顯示與終端無關的進程
ax # 顯示所有進程
Z # 顯示安全相關信息
-e # 顯示所有進程,相當於ax
-f # 顯示完整格式的進程信息
-F # 顯示更加完整格式的進程信息
-H # 顯示進程樹關係(子進程及父進程關係)
-j # 以任務格式顯示
-U # 指定以某個用戶身份運行的進程(-U root)
-o # 自定義顯示字段
-o euser,ruser,fuser,f,comm,label,tty,pid,ppid,psr,tid,class,rtprio,ni,pri,
-o psr,pcpu,stat,wchan:14,euid,ruid,tpgid,sess,pgrp
常用字段:
ni nice值
pri priority,優先級
psr processor,使用的是那顆CPU
rtptrio 實時優先級
euid 有效用戶
ruid 實際用戶
pid 進程
ppid 父進程
顯示字段說明:
USER(屬主):進程的屬主
PID(進程號):process id,進程的ID
PPID(父進程號):進程的夫進程號
%CPU(CPU佔用率):該進程佔用的cpu
%MEM(內存佔用率):該進程佔用的內存
VSZ(虛擬內存集):Virtual memory SiZe,虛擬內存大小
RSS(常駐內存集):常駐內存,固定佔用,不能用於動態交換
TTY(終端):進程的終端(?表示與終端無關)
SATA(進程狀態):
R running
S interruptable sleeping
D uninterruptable sleeping
T stopped
Z zombie
+ 用戶進程
l 多線程進程
N 低優先級進程
< 高優先級進程
s session leader會話領導進程
(該進程啓動會引導其它進程啓動,終止該進程,着領導啓動的進程也會終止)
常見用法示例;
[oneapm@ONEAPM-NGINX01 ~]$ ps -U oneapm -FH
UID PID PPID C SZ RSS PSR STIME TTY TIME CMD
oneapm 26506 26497 0 40870 2416 2 15:34 ? 00:00:00 sshd: oneapm@pts/4
oneapm 26507 26506 0 29217 3584 2 15:34 pts/4 00:00:00 -bash
oneapm 29704 26507 0 38830 1864 0 15:45 pts/4 00:00:00 ps -U oneapm -FH
oneapm 21837 21831 0 40870 2560 1 15:15 ? 00:00:00 sshd: oneapm@pts/3
oneapm 21838 21837 0 29189 3356 3 15:15 pts/3 00:00:00 -bash
oneapm 21895 21838 1 30726 2604 2 15:15 pts/3 00:00:29 htop
oneapm 99165 1 2 1460380 772244 2 Feb27 ? 03:33:12 /data/oneapm/jdk1.8.0_65/bin/java -cp /data/oneapm/usercenter/usercenter_system:/
oneapm 97712 1 0 10956 1136 1 Feb27 ? 00:00:00 nginx: master process /data/oneapm/NginxHome_new/nginx-1.1.6.1/sbin/nginx -c /dat
oneapm 97713 97712 2 13422 11348 2 Feb27 ? 04:08:26 nginx: worker process
oneapm 97714 97712 3 13382 11292 1 Feb27 ? 06:34:10 nginx: worker process
oneapm 97715 97712 6 13602 12052 3 Feb27 ? 11:27:32 nginx: worker process
oneapm 63641 1 0 35690 700 2 2019 ? 00:00:01 sz Agent_10001_log.59.6.tgz
oneapm 72381 1 0 40493 2040 0 2018 ? 00:00:08 top
oneapm 46637 1 0 40493 1316 1 2018 ? 00:00:48 top
[oneapm@ONEAPM-NGINX01 ~]$ ps -eF | grep nginx
oneapm 28601 26507 0 28176 980 0 15:42 pts/4 00:00:00 grep --color=auto nginx
oneapm 97712 1 0 10956 1136 1 Feb27 ? 00:00:00 nginx: master process /data/oneapm/NginxHome_new/nginx-1.1.6.1/sbin/nginx -c /data/oneapm/NginxHome_new/nginx-1.1.6.1/conf/nginx.conf
oneapm 97713 97712 2 13488 11612 2 Feb27 ? 04:08:19 nginx: worker process
oneapm 97714 97712 3 13482 11736 2 Feb27 ? 06:34:00 nginx: worker process
oneapm 97715 97712 6 13862 13192 3 Feb27 ? 11:27:09 nginx: worker process
[oneapm@ONEAPM-NGINX01 ~]$ ps -xfo user,ppid,pid,tid,pri,psr,%cpu,%mem,stat,cmd
USER PPID PID TID PRI PSR %CPU %MEM STAT CMD
oneapm 26497 26506 26506 19 2 0.0 0.0 S sshd: oneapm@pts/4
oneapm 26506 26507 26507 19 3 0.0 0.0 Ss \_ -bash
oneapm 26507 31409 31409 19 3 0.0 0.0 R+ \_ ps -xfo user,ppid,pid,tid,pri,psr,%cpu,%mem,stat,cmd
oneapm 21831 21837 21837 19 1 0.0 0.0 S sshd: oneapm@pts/3
oneapm 21837 21838 21838 19 3 0.0 0.0 Ss \_ -bash
oneapm 21838 21895 21895 19 0 1.6 0.0 S+ \_ htop
oneapm 1 99165 99165 19 2 2.1 9.6 Sl /data/oneapm/jdk1.8.0_65/bin/java -cp /data/oneapm/usercenter/usercenter_system:/data/oneapm/u
oneapm 1 97712 97712 19 1 0.0 0.0 Ss nginx: master process /data/oneapm/NginxHome_new/nginx-1.1.6.1/sbin/nginx -c /data/oneapm/Ngin
oneapm 97712 97713 97713 19 2 2.4 0.1 S \_ nginx: worker process
oneapm 97712 97714 97714 19 0 3.8 0.1 S \_ nginx: worker process
oneapm 97712 97715 97715 19 1 6.7 0.1 S \_ nginx: worker process
oneapm 1 63641 63641 19 2 0.0 0.0 S sz Agent_10001_log.59.6.tgz
oneapm 1 72381 72381 19 0 0.0 0.0 S top
oneapm 1 46637 46637 19 1 0.0 0.0 S top
四、pgrep:進程狀態過濾顯示命令
pgrep 選項
選項說明:
-u 指明有效用戶(顯示該用戶下進程id)
-U 指明實際用戶id(顯示該用戶下進程id)
-l 顯示進程名字
-a 顯示完整格式的進程名字
-t 指明tty終端
-P pid 顯示指定pid父進程下的子進程列表
示例:
[oneapm@ONEAPM-NGINX01 ~]$ pgrep -a -P 97712
97713 nginx: worker process
97714 nginx: worker process
97715 nginx: worker process
[oneapm@ONEAPM-NGINX01 ~]$ pgrep -U oneapm -l
21837 sshd
21838 bash
21895 htop
26506 sshd
26507 bash
46637 top
63641 sz
72381 top
97712 nginx
97713 nginx
97714 nginx
97715 nginx
99165 java
五、pidof:根據進程名獲取PID
pidof 進程名
示例:
[oneapm@ONEAPM-NGINX01 ~]$ pidof nginx
97715 97714 97713 97712
六、top:進程動態信息查詢
top內置子命令:
排序:
P 以佔據CPU百分比排序
M 以佔據內存百分比排序
切換:
m 切換是否顯示內存使用情況
l 切換是否顯示首行信息
t 切換是否顯示Task統計及顯示格式
1 切換是否分別顯示多顆CPU的task統計信息
刷新時間:
s 後跟時間(單位秒),修改TOP刷新時間(默認爲3s)
結束進程:
k 後根PID,結束指定PID進程
退出:
q
top顯示信息說明:
top - 20:14:58 up 1 day, 19:17, 2 users, load average: 0.00, 0.01, 0.05
#當前系統時間 系統運行時長 用戶數 平均負載(1分鐘,5分鐘,10分鐘)
Tasks: 374 total, 2 running, 370 sleeping, 2 stopped 0 zombie
#進程數 running進程數 sleeping睡眠進程數 stopped停止進程數 zombie僵死進程數
%Cpu0 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu1 : 0.0 us, 0.4 sy, 0.0 ni, 99.6 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu2 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu3 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
#us用戶空間程序佔用百分比,sy系統空間程序佔用百分比,ni爲nice值佔據的時間,id空閒比例,wa等待IO完成
的時間,hi硬件終段時間百分比,si軟件終段時間百分比,st偷走時間比(如虛擬機)
KiB Mem : 3866948 total, 3137836 free, 170632 used, 558480 buff/cache
#total物理總內存,free剩餘內存,used佔用內存,buff/cache緩存內存
KiB Swap: 5242876 total, 5242876 free, 0 used. 3436820 avail Mem
#swap total交換空間總內存,swap free交換空間剩餘內存,swap used交換空間佔用內存,acail Mem物理可用空間
進程號 屬主 優先級 nice值 虛擬內存集 常駐內存集 共享內存 準太 cpu佔比 內存佔比 運行時長 啓動該進程的命令
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
31705 root 20 0 146276 2276 1428 R 1.0 0.1 0:00.11 top
1 root 20 0 91976 7316 2628 S 0.0 0.2 0:17.11 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.27 kthreadd
3 root 20 0 0 0 0 S 0.0 0.0 0:00.78 ksoftirqd/0
七、htop
htop子命令:
排序:
P 以佔據CPU百分比排序
M 以佔據內存百分比排序
進程跟蹤:
s # 跟蹤顯示選定進程的系統調用
l # 跟蹤顯示選定進程打開的文件列表
進程綁定CPU
a # 將選定進程綁定至某指定CPU
退出
q
F系列按鍵
F1 # 使用幫助
F2 # 設置
F3 # 查找
F4 # 過濾
F5 # 樹狀顯示父子進程
F6 # 排序
F7 # -nice值
F8 # +nice值
F9 # kill進程
F10 # 退出
八、kill命令:進程控制命令,通過向進程發送控制信號控制進程狀態
kill -l # 顯示kill的信號列表
常用信號:
1) SIGHUP # 通知進程重讀配置文件,而不用重啓進程
2) SIGINT # 終止正在運行的進程,相當於Ctrl + C
9) SIGKILL # 殺死正在運行的進程(立即終止進程)
15) SIGTERM # 優雅終止正在運行的進程(完成目前的讀寫操作後終止)
18) SIGCONT # 繼續停止了的進程
19) SIGSTOP # 停止進程
kill -[信號名|信號代碼] pid # 對指定pid進程發送信號,並執行信號代表的操作
示例:
kill -9 3655 # 立即終止指定pid的進程
kill -1 3655 # 再不重啓進程的情況下重讀配置文件
killall -[信號名|信號代碼] 進程名 # 對同一進程名的進程發送信號並執行
示例:
killall -9 nginx
killall -HUP 進程名字 # 再不重啓進程的情況下重讀配置文件
九、nice、renice進程優先級的調整
nice值:-20--19
優先級=nice+120,結果數值越小越優先
若程序啓動時進程的nice值爲0,優先級爲120
以指定nice值啓動進程:nice -n nice值 進程腳本
示例:
[root@localhost ~]# nice -n -20 /data/nginxHome/sbin/nginx
修改進程的nice值:renice -n nice值 PID
查看進程的nice值:ps axo pid,comm,ni
十、fuser進程查看
fuser -v -m /data/ # 查看指定文件佔用的進程
fuser -v -n tcp 80 # 參考指定的端口占用的進程
十一、作業(多組進程)管理
作業:爲實現同一個項目的多組進程
前臺作業:運行時佔據了終端的命令行窗口的作業
後臺作業:運行時後臺運行,不佔用終端的作業
作業前後臺切換:可以使用vi/top等命令測試
將前臺運行的作業切換到後臺(仍然與終端關聯)
Ctrl + Z
啓動作業後,立即運行爲後臺作業(仍然與終端關聯)
作業運行腳本 &
啓動作業後,立即運行爲後臺,並剝離終端運行
nohup 作業運行腳本 &
查看目前正在運行的作業
jobs
將後臺作業調回前臺
fg %作業號
使後臺停止狀態的作業繼續運行
bg %作業號
終止指定作業
kill %作業號