Linux进程间的关系ps命令
ps -ef 是标准显示模式,而ps -aux是BSD显示模式
[root@WzcWyt ~]# ps -ef | less
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 Apr19 ? 00:01:54 /usr/lib/systemd/systemd --switched-root --system --deserialize 18
root 2 0 0 Apr19 ? 00:00:02 [kthreadd]
root 3 2 0 Apr19 ? 00:00:00 [rcu_gp]
[root@WzcWyt ~]# ps -aux | less
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.2 179296 11216 ? SNs Apr19 1:54 /usr/lib/systemd/systemd --switched-root --system --deserialize 18
root 2 0.0 0.0 0 0 ? S Apr19 0:02 [kthreadd]
root 3 0.0 0.0 0 0 ? I< Apr19 0:00 [rcu_gp]
UID PID PPID C STIME TTY TIME CMD
UID是用户ID,PID是进程ID,PPID是父进程ID
[root@WzcWyt ~]# sleep 100
^Z
[1]+ Stopped sleep 100
[root@WzcWyt ~]# ps -ef | grep sleep
root 404283 404241 0 15:35 pts/3 00:00:00 sleep 100
root 404285 404241 0 15:36 pts/3 00:00:00 grep --color=auto sleep
[root@WzcWyt ~]# ps -ef | grep 404241
root 404241 404240 0 15:35 pts/3 00:00:00 -bash
root 404283 404241 0 15:35 pts/3 00:00:00 sleep 100
root 404286 404241 0 15:36 pts/3 00:00:00 ps -ef
root 404287 404241 0 15:36 pts/3 00:00:00 grep --color=auto 404241
[root@WzcWyt ~]# ps -ef |grep 404240
root 404240 404238 0 15:35 ? 00:00:00 sshd: root@pts/3
root 404241 404240 0 15:35 pts/3 00:00:00 -bash
root 404289 404241 0 15:36 pts/3 00:00:00 grep --color=auto 404240
[root@WzcWyt ~]# ps -ef |grep 404238
root 404238 1009 0 15:35 ? 00:00:00 sshd: root [priv]
root 404240 404238 0 15:35 ? 00:00:00 sshd: root@pts/3
root 404291 404241 0 15:37 pts/3 00:00:00 grep --color=auto 404238
[root@WzcWyt ~]# ps -ef |grep 1009
root 1009 1 0 Apr19 ? 00:00:06 /usr/sbin/sshd -D -oCiphers=aes256-gcm@openssh.com,chacha20-poly1305@openssh.com,aes256-ctr,aes256-cbc,aes128-gcm@openssh.com,aes128-ctr,aes128-cbc -oMACs=hmac-sha2-256-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha2-256,hmac-sha1,umac-128@openssh.com,hmac-sha2-512 -oGSSAPIKexAlgorithms=gss-curve25519-sha256-,gss-nistp256-sha256-,gss-group14-sha256-,gss-group16-sha512-,gss-gex-sha1-,gss-group14-sha1- -oKexAlgorithms=curve25519-sha256,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group14-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1 -oHostKeyAlgorithms=ecdsa-sha2-nistp256,ecdsa-sha2-nistp256-cert-v01@openssh.com,ecdsa-sha2-nistp384,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp521,ecdsa-sha2-nistp521-cert-v01@openssh.com,ssh-ed25519,ssh-ed25519-cert-v01@openssh.com,rsa-sha2-256,rsa-sha2-256-cert-v01@openssh.com,rsa-sha2-512,rsa-sha2-512-cert-v01@openssh.com,ssh-rsa,ssh-rsa-cert-v01@openssh.com -oPubkeyAcceptedKeyTypes=ecdsa-sha2-nistp256,ecdsa-sha2-nistp256-cert-v01@openssh.com,ecdsa-sha2-nistp384,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp521,ecdsa-sha2-nistp521-cert-v01@openssh.com,ssh-ed25519,ssh-ed25519-cert-v01@openssh.com,rsa-sha2-256,rsa-sha2-256-cert-v01@openssh.com,rsa-sha2-512,rsa-sha2-512-cert-v01@openssh.com,ssh-rsa,ssh-rsa-cert-v01@openssh.com -oCASignatureAlgorithms=ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,ssh-ed25519,rsa-sha2-256,rsa-sha2-512,ssh-rsa
root 404060 1009 0 15:19 ? 00:00:00 sshd: root [priv]
root 404146 1009 0 15:31 ? 00:00:00 sshd: root [priv]
root 404194 1009 0 15:34 ? 00:00:00 sshd: root [priv]
root 404238 1009 0 15:35 ? 00:00:00 sshd: root [priv]
root 404293 404241 0 15:37 pts/3 00:00:00 grep --color=auto 1009
[root@WzcWyt ~]#
ps查询命令
uname:用户信息
pid:进程id
ppid:父进程id
thcount:线程数
ni:这个范围从19到-20,数越大越好
pri:优先级
psr:进程当前分配给的处理器数
pcpu:CPU
pmem:内存占用
rss:保留内存,常驻集大小,任务已使用的非交换物理内存(以KB为单位)
vsz:虚拟内存
sz:在物理页面大小的核心图像的进程。这包括文本、数据和堆栈空间。设备映射目前被排除在外
start_time:流程的开始时间或日期
time:累计CPU时间
comm:命令名(仅可执行文件名)
c:
command:执行的命令
[root@WzcWyt ~]# ps -o uname,pid,ppid,thcount,ni,pri,psr,pcpu,pmem,rss,vsz,sz,start_time,time,comm,c,command -e | less
USER PID PPID THCNT NI PRI PSR %CPU %MEM RSS VSZ SZ START TIME COMMAND C COMMAND
root 1 0 1 19 0 0 0.0 0.2 11216 179296 44824 Apr19 00:01:54 systemd 0 /usr/lib/systemd/systemd --switched-root --system --deserialize 18
root 2 0 1 0 19 0 0.0 0.0 0 0 0 Apr19 00:00:02 kthreadd 0 [kthreadd]
root 3 2 1 -20 39 0 0.0 0.0 0 0 0 Apr19 00:00:00 rcu_gp 0 [rcu_gp]
案例:统计某个进程的数据信息
[root@WzcWyt test]# ps -ef | grep -i python
root 916 1 0 Apr19 ? 00:17:03 /usr/libexec/platform-python -Es /usr/sbin/tuned -l -P
root 404387 404064 0 16:05 pts/0 00:00:00 python -m http.server --cgi 8082
root 404560 404391 0 17:08 pts/4 00:00:00 grep --color=auto -i python
[root@WzcWyt test]# ps -o uname,pid,ppid,thcount,ni,pri,psr,pcpu,pmem,rss,vsz,sz,start_time,time,comm,c,command -p 404387
USER PID PPID THCNT NI PRI PSR %CPU %MEM RSS VSZ SZ START TIME COMMAND C COMMAND
root 404387 404064 1 19 0 0 0.0 0.4 17516 230784 57696 16:05 00:00:00 python 0 python -m http.server --cgi 8082
案例:统计某一进程内存平均值
[root@WzcWyt test]# ps -o %mem -p 404387
%MEM
0.4
[root@WzcWyt test]# ps -o %mem -p 404387 | tail -1
0.4
[root@WzcWyt test]# echo $(seq 20)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
[root@WzcWyt test]# for i in $(seq 20);do ps -o %mem -p 404387 | tail -1;sleep 1;done
0.4
0.4
0.4
^C
[root@WzcWyt test]# for i in $(seq 20);do ps -o %mem -p 404387 | tail -1;sleep 1;done | awk '{print $1}'
0.4
0.4
0.4
^C
[root@WzcWyt test]# for i in $(seq 20);do ps -o %mem -p 404387 | tail -1;sleep 1;done | awk '{t+=$1;print $1,t/NR}'
0.4 0.4
0.4 0.4
0.4 0.4
0.4 0.4
^C
[root@WzcWyt test]#
top方式统计某个进程的cpu和内存使用情况
[root@WzcWyt ~]# ps -ef | grep -i python
root 916 1 0 Apr19 ? 00:17:03 /usr/libexec/platform-python -Es /usr/sbin/tuned -l -P
root 404387 404064 0 16:05 pts/0 00:00:00 python -m http.server --cgi 8082
root 404742 404703 0 17:34 pts/5 00:00:00 grep --color=auto -i python
[root@WzcWyt ~]# top -p 404387
top - 17:35:25 up 143 days, 23:43, 6 users, load average: 0.00, 0.01,
Tasks: 1 total, 0 running, 1 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.2 us, 0.2 sy, 0.0 ni, 99.3 id, 0.0 wa, 0.2 hi, 0.2 si,
MiB Mem : 3738.1 total, 179.8 free, 1603.3 used, 1955.0 buff/c
MiB Swap: 0.0 total, 0.0 free, 0.0 used. 1850.9 avail
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+
404387 root 39 19 230784 17524 8592 S 0.0 0.5 0:00.64
[root@WzcWyt ~]# top -n 20 -p 404387
top - 17:36:34 up 143 days, 23:44, 6 users, load average: 0.00, 0.01,
Tasks: 1 total, 0 running, 1 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.2 us, 0.3 sy, 0.0 ni, 99.3 id, 0.0 wa, 0.2 hi, 0.0 si,
MiB Mem : 3738.1 total, 179.9 free, 1603.2 used, 1955.0 buff/c
MiB Swap: 0.0 total, 0.0 free, 0.0 used. 1851.0 avail
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+
404387 root 39 19 230784 17524 8592 S 0.0 0.5 0:00.65
[root@WzcWyt ~]# top -d 1 -n 20 -p 404387
top - 17:42:54 up 143 days, 23:50, 6 users, load average: 0.00, 0.00, 0.00
Tasks: 1 total, 0 running, 1 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.5 sy, 0.0 ni, 99.5 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 3738.1 total, 178.5 free, 1604.5 used, 1955.1 buff/cache
MiB Swap: 0.0 total, 0.0 free, 0.0 used. 1849.7 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
404387 root 39 19 230784 17524 8592 S 0.0 0.5 0:00.69 python
[root@WzcWyt ~]# top -b 1 -n 20 -p 404387
top - 17:36:46 up 143 days, 23:44, 6 users, load average: 0.00, 0.00,
Tasks: 1 total, 0 running, 1 sleeping, 0 stopped, 0 zombie
%Cpu0 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si,
%Cpu1 : 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si,
MiB Mem : 3738.1 total, 177.0 free, 1606.1 used, 1955.0 buff/ca
MiB Swap: 0.0 total, 0.0 free, 0.0 used. 1848.2 avail M
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+
404387 root 39 19 230784 17524 8592 S 0.0 0.5 0:00.65
top - 17:36:49 up 143 days, 23:44, 6 users, load average: 0.00, 0.00,
Tasks: 1 total, 0 running, 1 sleeping, 0 stopped, 0 zombie
%Cpu0 : 0.0 us, 0.3 sy, 0.0 ni, 99.7 id, 0.0 wa, 0.0 hi, 0.0 si,
%Cpu1 : 0.0 us, 0.7 sy, 0.0 ni, 99.3 id, 0.0 wa, 0.0 hi, 0.0 si,
MiB Mem : 3738.1 total, 177.0 free, 1606.1 used, 1955.0 buff/ca
MiB Swap: 0.0 total, 0.0 free, 0.0 used. 1848.2 avail M
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+
404387 root 39 19 230784 17524 8592 S 0.0 0.5 0:00.65
[root@WzcWyt ~]# top -b -d 1 -n 20 -p 404387 | grep '^ 404387'
404387 root 39 19 230784 17524 8592 S 0.0 0.5 0:00.71 python
404387 root 39 19 230784 17524 8592 S 0.0 0.5 0:00.71 python
404387 root 39 19 230784 17524 8592 S 0.0 0.5 0:00.71 python
404387 root 39 19 230784 17524 8592 S 0.0 0.5 0:00.71 python
404387 root 39 19 230784 17524 8592 S 0.0 0.5 0:00.71 python
404387 root 39 19 230784 17524 8592 S 0.0 0.5 0:00.71 python
^C
[root@WzcWyt ~]# top -b -d 1 -n 20 -p 404387 | grep --line-buffered '^ 404387' | awk '{cpu+=$9;mem+=$10}{print $9,$10,cpu/NR,mem/NR}'
0.0 0.5 0 0.5
0.0 0.5 0 0.5
[root@WzcWyt ~]# top -b -d 1 -n 20 -p 404387 | grep --line-buffered '^ 404387' | awk '{cpu+=$9;mem+=$10}{print $9,$10,cpu/NR,mem/NR}' | gnuplot -e "set terminal dumb;plot '<cat' using 1 with line"
Warning: empty y range [0:0], adjusting to [-1:1]
1 +--------------------------------------------------------------------+
| + + + + + + + + + |
| '<cat' using 1 ******* |
| |
| |
0.5 |-+ +-|
| |
| |
| |
| |
0 |******************************************************************+-|
| |
| |
| |
| |
-0.5 |-+ +-|
| |
| |
| |
| + + + + + + + + + |
-1 +--------------------------------------------------------------------+
0 2 4 6 8 10 12 14 16 18 20
[root@WzcWyt ~]#
本文详细解读了Linux中ps命令的不同用法,包括ps-ef与ps-aux的区别,以及如何通过ps查询进程信息,如用户ID、PID、内存占用等。展示了统计特定进程数据和内存平均值的方法,以及利用top命令实时监控特定进程的CPU和内存使用情况。
1万+

被折叠的 条评论
为什么被折叠?



