ps & pstree
ps:
ps在默认情况下,仅会列出与目前所在的bash shell有关的PID
- 排序
ps aux --sort=-pcpu # pcpu降序排列
ps aux --sort=+pmem # pmem升序排列 - 定制显示选项
ps -e -o pid,user,pcpu,pmem,rss ps ax -o pid,user,pcpu,pmem,rss,command,nice,etime
- --forest选项显示进程数
ps auxf ps aux --forest ps -elf --forest ps -elfH
- -C 显示某个进程,注意此选项不要与-e x等同时使用,可以加入其他修饰选项
ps -fjl --forest -C sshd
ps au --forest -C sshd
-
显示完整用户名
使用-o选项
ps ax -o user=userforlongname,command|grep elasticsearch
- -L选项显示LWP,light-weighted process线程相关信息
ps -elfL|awk '$NF ~ /^[^[]/ && $3 ~ /polkitd|UID/{print $0}'
把polkitd的LWP信息删选出来
PID相同,LWP是线程ID,NLWP显示PID 571内的LWP数量
ps -lfjL 571
根据当前polkitd的进程ID来查找也是可以的
-
ps -elfj > vv awk 'NR==FNR{b[$2]++} NR!=FNR && ++p[$2]==1{print $2,b[$2]}' vv vv
Linux中的进程状态
D Uninterruptible sleep (usually IO) 不可中断睡眠
R Running or runnable (on run queue)
S Interruptible sleep 可中断睡眠
T Stopped,either by a job control signal or because it is being traced
Z Zombie,defunct process,terminated but not reapted by its parentD uninterruptible sleep (usually IO) 不可中断睡眠 R running or runnable (on run queue) (正在运行或在运行队列中等待) S interruptible sleep (waiting for an event to complete) T stopped by job control signal t stopped by debugger during the tracing Z defunct ("zombie") process, terminated but not reaped by its parent
For BSD formats and when the stat keyword is used, additional characters may be displayed: < 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
F列(Process flag),上图统计了flag的各进程数量
1 forked but didn't exec 4 used super-user privileges
- 查询进程的线程数量
PID查询
pstree -p 571
进程名称
pstree -p `ps -e|grep polkitd|grep -v grep|awk '{print $1}'`
- 统计处于Interruptible sleep的LWP数量
#!/bin/env sh sum=0 for b in `ps -elf|awk 'BEGIN{IGNORECASE=1} $2 ~ /s/{print $4}'`;do p=`pstree -p $b | wc --lines` let sum=sum+$p done echo -e "\e[7;36msleeping LWP number: $sum\e[0m"
-
发送STOP信号后,脚本zx处于了T状态
发送 CONT信号后,处于了S状态
-
pstree: Display a tree of processes
pstree 后接 PID | USER,如果不接显示全部
- -p 显示额外PID
- -a arguments 显示额外参数
pstack:
查看进程的stack信息
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
[root@perpetual vbn]# pstack 571
Thread 7 (Thread 0x7f170f6ff700 (LWP 608)):
#0 0x00007f1713309c3d in poll () from /lib64/libc.so.6
#1 0x00007f171428c37c in g_main_context_iterate.isra.19 () from /lib64/libglib-2.0.so.0
#2 0x00007f171428c4ac in g_main_context_iteration () from /lib64/libglib-2.0.so.0
#3 0x00007f171428c4e9 in glib_worker_main () from /lib64/libglib-2.0.so.0
#4 0x00007f17142b3540 in g_thread_proxy () from /lib64/libglib-2.0.so.0
#5 0x00007f17135ebea5 in start_thread () from /lib64/libpthread.so.0
#6 0x00007f17133148dd in clone () from /lib64/libc.so.6
Thread 6 (Thread 0x7f170eefe700 (LWP 609)):
#0 0x00007f1713309c3d in poll () from /lib64/libc.so.6
#1 0x00007f171428c37c in g_main_context_iterate.isra.19 () from /lib64/libglib-2.0.so.0
#2 0x00007f171428c6ca in g_main_loop_run () from /lib64/libglib-2.0.so.0
#3 0x00007f1714876c36 in gdbus_shared_thread_func () from /lib64/libgio-2.0.so.0
#4 0x00007f17142b3540 in g_thread_proxy () from /lib64/libglib-2.0.so.0
#5 0x00007f17135ebea5 in start_thread () from /lib64/libpthread.so.0
#6 0x00007f17133148dd in clone () from /lib64/libc.so.6
Thread 5 (Thread 0x7f170e6fd700 (LWP 610)):
#0 0x00007f1713309c3d in poll () from /lib64/libc.so.6
#1 0x00007f171428c37c in g_main_context_iterate.isra.19 () from /lib64/libglib-2.0.so.0
#2 0x00007f171428c6ca in g_main_loop_run () from /lib64/libglib-2.0.so.0
#3 0x0000555f98ea6615 in child_reaper_thread_func ()
#4 0x00007f17142b3540 in g_thread_proxy () from /lib64/libglib-2.0.so.0
#5 0x00007f17135ebea5 in start_thread () from /lib64/libpthread.so.0
#6 0x00007f17133148dd in clone () from /lib64/libc.so.6
Thread 4 (Thread 0x7f170defc700 (LWP 611)):
#0 0x00007f17135efa35 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
#1 0x00007f17106af270 in PR_WaitCondVar () from /lib64/libnspr4.so
#2 0x00007f17138aefb0 in js::GCHelperThread::threadLoop() () from /lib64/libmozjs-17.0.so
#3 0x00007f17106b4c5b in _pt_root () from /lib64/libnspr4.so
#4 0x00007f17135ebea5 in start_thread () from /lib64/libpthread.so.0
#5 0x00007f17133148dd in clone () from /lib64/libc.so.6
Thread 3 (Thread 0x7f170d2fb700 (LWP 612)):
#0 0x00007f17135efa35 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
#1 0x00007f17106af270 in PR_WaitCondVar () from /lib64/libnspr4.so
#2 0x00007f17139452be in js::SourceCompressorThread::threadLoop() () from /lib64/libmozjs-17.0.so
#3 0x00007f17106b4c5b in _pt_root () from /lib64/libnspr4.so
#4 0x00007f17135ebea5 in start_thread () from /lib64/libpthread.so.0
#5 0x00007f17133148dd in clone () from /lib64/libc.so.6
Thread 2 (Thread 0x7f16f7fff700 (LWP 613)):
#0 0x00007f1713309c3d in poll () from /lib64/libc.so.6
#1 0x00007f171428c37c in g_main_context_iterate.isra.19 () from /lib64/libglib-2.0.so.0
#2 0x00007f171428c6ca in g_main_loop_run () from /lib64/libglib-2.0.so.0
#3 0x0000555f98ea64ad in runaway_killer_thread_func ()
#4 0x00007f17142b3540 in g_thread_proxy () from /lib64/libglib-2.0.so.0
#5 0x00007f17135ebea5 in start_thread () from /lib64/libpthread.so.0
#6 0x00007f17133148dd in clone () from /lib64/libc.so.6
Thread 1 (Thread 0x7f1714f69900 (LWP 571)):
#0 0x00007f1713309c3d in poll () from /lib64/libc.so.6
#1 0x00007f171428c37c in g_main_context_iterate.isra.19 () from /lib64/libglib-2.0.so.0
#2 0x00007f171428c6ca in g_main_loop_run () from /lib64/libglib-2.0.so.0
#3 0x0000555f98ea25c5 in main ()