進程管理
查看進程
進程控制
作業控制
進程優先級
======================================
ps process snapshot
查看當前終端上運行的進程
#ps
#ps aux
#ps auxf
a 只能查看所有終端進程
u 顯示進程擁有者
x 顯示系統內所有進程
f 顯示進程之間的父子關係(使用pstree查看更詳細的父子關係)
user 進程擁有者
pid process identify
TTY 進程在哪個終端運行
查看tty的方法:
#tty
? 表示這個進程開啓的時候沒有佔用終端
time 進程佔用cpu的總時間
cmd 進程名稱
%cpu 進程佔用的cpu百分比
%mem 進程佔用memory百分比
VSZ 進程佔用的虛擬內存大小
RSS 佔用的物理內存大小
STAT 當前進程狀態
#man ps
R running
S sleep
T stop
Z zombie(僵死,殭屍)
D Uninterruptible sleep (usually IO)
R Running or runnable (on run queue)
S Interruptible sleep (waiting for an event to complete)
T Stopped, either by a job control signal or because it is
being traced.
W paging (not valid since the 2.6.xx kernel)
X dead (should never be seen)
Z Defunct ("zombie") process, terminated but not reaped by
its parent.
< high-priority (not nice to other users)
N low-priority (nice to other users)
L has pages locked into memory (for real-time and
custom IO)
s is a session leader
l is multi-threaded (using CLONE_THREAD, like NPTL
pthreads do)
+ is in the foreground process group
按指定字段排序:
[root@wing ~]# ps aux --sort %cpu | less
[root@wing ~]# ps aux --sort %-cpu | less
顯示指定字段:
[root@wing ~]# ps axo user,%cpu,command --sort -%cpu | less
[root@wing ~]# ps -eo user,%cpu,%mem,command --sort %cpu
#ps -elf
-e 顯示所有進程
-l 長格式顯示
-f 完整格式
#ps -e 常用
pstree
查看進程樹
#pstree
-a 顯示參數
-p 顯示pid
-u 顯示用戶名
lsof(應用範圍有限,只能查看帶端口的進程)
lsof -i:80
查看端口爲80的進程
pidof
#pidof 進程名稱
pgrep
pgrep -l eyes
1179 xeyes
進程pid文件
#cd /var/run
cat /var/run/httpd/httpd.pid
12265
======================================
w
[root@server ~]# w
16:51:43 up 8:30, 7 users, load average: 0.11, 0.27, 0.30
USER TTY FROM LOGIN@ IDLE JCPU
PCPU WHAT
root :0 :0 08:22 ?xdm? 29:39 0.23s
gdm-session-
root pts/0 :0 14:13 1:43 0.30s 0.30s bash
root pts/1 :0 11:19 5:19 0.62s 0.62s bash
root pts/2 :0 14:13 1:39m 0.06s 0.01s less
root pts/3 :0 14:54 7.00s 0.06s 0.01s w
root pts/4 :0 16:25 25:51 0.06s 0.06s bash
root pts/5 :0 16:40 7:11 0.24s 0.24s bash
======================================
who
[root@server ~]# who
root :0 2017-11-27 08:22 (:0)
root pts/0 2017-11-27 14:13 (:0)
root pts/1 2017-11-27 11:19 (:0)
root pts/2 2017-11-27 14:13 (:0)
root pts/3 2017-11-27 14:54 (:0)
root pts/4 2017-11-27 16:25 (:0)
root pts/5 2017-11-27 16:40 (:0)
======================================
users
[root@server ~]# users
root root root root root root root
======================================
top
實時的查看進程的狀態
h|? 幫助
往下翻頁
< 往上翻頁
M 按內存排序
P 按cpu排序
k 輸入pid殺死進程
h 打印幫助
q 退出
1
f
W
R 排序反轉
z 彩色顯示
r 調整進程的nice優先級
[root@wing ~]# top -d 2 -p 1068 -bn1
[root@wing ~]# top -u apache
======================================
進程控制
按pid殺死進程
#kill 信號(signal) pid
-1 HUP 重新加載進程或者重新加載配置文件
-9 KILL 強制殺死
-15 TERM 正常殺死(這個信號可以默認不寫)
-18 CONT 激活進程
-19 STOP 掛起進程
#kill -HUP pid
#kill -STOP pid
#kill -l
#man 7 signal 所有信號的解釋
#killall 信號 進程名稱
#pkill -9 進程名稱
#pkill -t 終端
不加-9只殺死在終端上運行的進程,加-9連終端本身一起幹掉
#pkill -u 用戶名稱
#xkill
======================================
作業控制:
作業控制之jobs:
ctrl+z 把程序放到後臺(這方法會讓程序在後臺暫停)
#fg %1 把程序調到前臺,%是用來修飾job number,1就是job number。
#jobs 查看工作號
#bg %1 讓暫停的程序在後臺運行
#kill -9 %1
#kill -9 pid
作業控制之screen:
screen提供會話恢復和多窗口功能,本節講解常用的會話恢復功能
#yum install screen -y
實例:
開啓screen會話
[root@wing ~]#screen -S lala //給screen起個名字叫lala
[root@wing ~]# ssh 192.168.245.3 //遠程連到245.3這臺機器
[email protected]'s password:
Last login: Sun Nov 26 19:48:39 2017 from 192.168.245.137
[root@master ~]# vim aa.txt //編輯一個文件,在沒有編輯完的情況下關閉終端
恢復screen會話:
[root@wing ~]# screen -list
There are screens on:
39160.lala (Attached)
38966.pts-0.wing (Detached)
2 Sockets in /var/run/screen/S-root.
[root@wing ~]# screen -r 38966 //38966是screen lala中運行的會話進程
回車後神奇恢復被意外中斷的會話
======================================
進程優先級
優先級本身是"不能"修改,通過nice值修改優先級。
普通賬戶只能調高nice值,不能往低調
root賬戶隨便調
nice值越高,優先級越低。
nice值範圍:-20到19
查看進程優先級
# top
# ps axo pid,command,nice --sort=-nice | less
# ps -elf
PR:priority
NI:nice
指定進程優先級
#nice --15 firefox &
修改進程優先級
# renice 10 7116 nice值前面不能加-
netstat
查看網絡進程
#netstat -auntpl
-a 所有進程
-u udp
-t tcp
-n 以數字形式顯示協議名稱
-p 顯示進程的名稱和PID
-l 只顯示正在被監聽的端口
#netstat -auntpl | grep 22
ss 推薦使用
查看網絡進程
#netstat -auntpl
-a 所有進程
-u udp
-t tcp
-n 以數字形式顯示協議名稱
-p 顯示進程的名稱和PID
-l 只顯示正在被監聽的端口