Linux操作系统的进程管理

时间:2023-03-10 01:53:36
Linux操作系统的进程管理

       Linux操作系统的进程管理

                       作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

一.进程相关概念

1>.进程概述

内核的功用:
  进程管理、文件系统、网络功能、内存管理、驱动程序、安全功能等

Process:
  运行中的程序的一个副本,是被载入内存的一个指令集合
  进程ID(Process ID,PID)号码被用来标记各个进程
  UID、GID、和SELinux语境决定对文件系统的存取和访问权限
  通常从执行进程的用户来继承
  存在生命周期
task struct:
  Linux内核存储进程信息的数据结构格式

task list:
  多个任务的的task struct组成的链表

进程创建:
  init:第一个进程
  进程:都由其父进程创建,父子关系,CoW(写时复制)
    fork(), clone()

2>.用户和内核空间

Linux操作系统的进程管理

3>.进程,线程和协程

Linux操作系统的进程管理

4>.进程的基本状态和转换

进程的基本状态
  创建状态:
    进程在创建时需要申请一个空白PCB(process control block进程控制块),向其中填写控制和管理进程的信息,完成资源分配。如果创建工作无法完成,比如资源无法满足,就无法被调度运行,把此时进程所处状态称为创建状态
  就绪状态:
    进程已准备好,已分配到所需资源,只要分配到CPU就能够立即运行
  执行状态:
    进程处于就绪状态被调度后,进程进入执行状态
  阻塞状态:
    正在执行的进程由于某些事件(I/O请求,申请缓存区失败)而暂时无法运行,进程受到阻塞。在满足请求时进入就绪状态等待系统调用
  终止状态:
    进程结束,或出现错误,或被系统终止,进入终止状态。无法再执行 状态之间转换六种情况
运行——>就绪:
  ()主要是进程占用CPU的时间过长,而系统分配给该进程占用CPU的时间是有限的;
  ()在采用抢先式优先级调度算法的系统中,当有更高优先级的进程要运行时,该进程就*让出CPU,该进程便由执行状态转变为就绪状态
就绪——>运行:
  运行的进程的时间片用完,调度就转到就绪队列中选择合适的进程分配CPU
运行——>阻塞:
  正在执行的进程因发生某等待事件而无法执行,则进程由执行状态变为阻塞状态,如发生了I/O请求
阻塞——>就绪:
  进程所等待的事件已经发生,就进入就绪队列
以下两种状态是不可能发生的:
阻塞——>运行:
    即使给阻塞进程分配CPU,也无法执行,操作系统在进行调度时不会从阻塞队列进行挑选,而是从就绪队列中选取
就绪——>阻塞:
    就绪态根本就没有执行,谈不上进入阻塞态

Linux操作系统的进程管理

5>.进程优先级

进程优先级:
  系统优先级:数字越小,优先级越高
    -(CentOS4,)
      各有140个运行队列和过期队列
    -,(CentOS6)
      实时优先级: - 值最大优先级最高
    nice值:-20到19,对应系统优先级100-139或99
    在CentOS4和CentOS 5有140个优先级,从99之后的每一个优先级对应一个nice值,而CentOS 6只有99个优先级,其中99对应的时nice值,因此尽管你再怎么调整nice值对应的优先级依旧是99。

Big O:时间复杂度,用时和规模的关系
  O(), O(logn), O(n)线性, O(n^)抛物线, O(^n)

Linux操作系统的进程管理

6>.进程内存

Page Frame: 
  页框,用存储页面数据,存储Page 4k
LRU:
  Least Recently Used 近期最少使用算法,释放内存

物理地址空间和线性地址空间
  MMU:
    Memory Management Unit,负责转换线性和物理地址
  TLB:
    Translation Lookaside Buffer,翻译后备缓冲器,用于保存虚拟地址和物理地址映射关系的缓存

7>.IPC(全称:"Inter Process Communication")

同一主机:
  signal:信号
  shm: shared memory
  semaphore:信号量,一种计数器
不同主机:
  socket: IP和端口号
  RPC: remote procedure call
  MQ:消息队列,Kafka,ActiveMQ

8>.LRU算法

假设序列为 "4,3,4,2,3,1,4,2",物理块有3个,则
  第1轮 4调入内存
  第2轮 3调入内存
  第3轮 4调入内存
  第4轮 2调入内存
  第5轮 3调入内存
  第6轮 1调入内存
  第7轮 4调入内存
  第8轮 2调入内存

Linux操作系统的进程管理

9>.进程状态

Linux内核:
  抢占式多任务 进程类型:
  守护进程: daemon,在系统引导过程中启动的进程,和终端无关进程
  前台进程:跟终端相关,通过终端启动的进程
  注意:两者可相互转化 进程状态:
  运行态:running
  就绪态:ready
  睡眠态:
    可中断:interruptable(操作系统大多数程序都处于该状态)
    不可中断:uninterruptable
  停止态:stopped,暂停于内存,但不会被调度,除非手动启动
  僵死态:zombie,结束进程,父进程结束前,子进程不关闭

10>.进程的分类

CPU-Bound:
CPU密集型,非交互 IO-Bound:
IO密集型,交互

二.Linux系统管理工具

1>.常见Linux系统状态的查看及管理工具

pstree
ps
pidof
pgrep
top
htop
glance
pmap
vmstat
dstat
kill
pkill
job
bg
fg
nohup

2>.pstree(display a tree of processes)

[root@node101.yinzhengjie.org.cn ~]# pstree --help
pstree: unrecognized option '--help'
Usage: pstree [ -a ] [ -c ] [ -h | -H PID ] [ -l ] [ -n ] [ -p ] [ -g ] [ -u ]
[ -A | -G | -U ] [ PID | USER ]
pstree -V
Display a tree of processes. -a, --arguments show command line arguments
-A, --ascii use ASCII line drawing characters
-c, --compact don't compact identical subtrees
-h, --highlight-all highlight current process and its ancestors
-H PID,
--highlight-pid=PID highlight this process and its ancestors
-g, --show-pgids show process group ids; implies -c
-G, --vt100 use VT100 line drawing characters
-l, --long don't truncate long lines
-n, --numeric-sort sort output by PID
-N type,
--ns-sort=type sort by namespace type (ipc, mnt, net, pid, user, uts)
-p, --show-pids show PIDs; implies -c
-s, --show-parents show parents of the selected process
-S, --ns-changes show namespace transitions
-u, --uid-changes show uid transitions
-U, --unicode use UTF- (Unicode) line drawing characters
-V, --version display version information
-Z,
--security-context show SELinux security contexts
PID start at this PID; default is (init)
USER show only trees rooted at processes of this user [root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# pstree --help    #查看帮助信息

[root@node101.yinzhengjie.org.cn ~]# pstree
systemd─┬─NetworkManager───*[{NetworkManager}]
├─agetty
├─atd
├─auditd───{auditd}
├─crond
├─dbus-daemon
├─irqbalance
├─lvmetad
├─polkitd───*[{polkitd}]
├─rsyslogd───*[{rsyslogd}]
├─sshd─┬─*[sshd───bash]
│ └─sshd───bash───pstree
├─systemd-journal
├─systemd-logind
├─systemd-udevd
└─tuned───*[{tuned}]
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# pstree        #显示进程关系

[root@node101.yinzhengjie.org.cn ~]# pstree -p
systemd()─┬─NetworkManager()─┬─{NetworkManager}()
│ └─{NetworkManager}()
├─agetty()
├─atd()
├─auditd()───{auditd}()
├─crond()
├─dbus-daemon()
├─irqbalance()
├─lvmetad()
├─polkitd()─┬─{polkitd}()
│ ├─{polkitd}()
│ ├─{polkitd}()
│ ├─{polkitd}()
│ ├─{polkitd}()
│ └─{polkitd}()
├─rsyslogd()─┬─{rsyslogd}()
│ └─{rsyslogd}()
├─sshd()─┬─sshd()───bash()
│ ├─sshd()───bash()
│ └─sshd()───bash()───pstree()
├─systemd-journal()
├─systemd-logind()
├─systemd-udevd()
└─tuned()─┬─{tuned}()
├─{tuned}()
├─{tuned}()
└─{tuned}()
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# pstree -p      #显示进程的PID

3>.ps(process state)

[root@node101.yinzhengjie.org.cn ~]# man ps         #查看帮助信息
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps        #只显示当前终端所开启的进程
PID TTY TIME CMD
pts/ :: bash
pts/ :: ps
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ls /proc// #Linux系统各进程的相关信息均保存在/proc/PID目录下的各文件中
attr cmdline environ io mem ns pagemap sched stack task
autogroup comm exe limits mountinfo numa_maps patch_state schedstat stat timers
auxv coredump_filter fd loginuid mounts oom_adj personality sessionid statm uid_map
cgroup cpuset fdinfo map_files mountstats oom_score projid_map setgroups status wchan
clear_refs cwd gid_map maps net oom_score_adj root smaps syscall
[root@node101.yinzhengjie.org.cn ~]# 温馨提示:
  ps支持三种风格:
    UNIX选项风格,可以分组,前面必须有一个短划线,如:"ps -a"
    BSD选项风格,可以分组,不能与短划线一起使用,如:"ps a"
    GNU长选项风格,前面有两个破折号,如:"ps --forest" ps输出属性:
  USER:进程所有者信息
  PID:进程的pid号
  %CPU:进程CPU使用率,如果超出100%表示使用的内核数大于1,如376%表示使用了4颗CPU。
  %MEM:进程使用内存的使用率
  VSZ: Virtual memory SiZe,虚拟内存集,线性内存
  RSS: ReSident Size, 常驻内存集,即实际使用的内存
  PSR: 进程运行在哪颗CPU上,我们知道CPU存在一级缓存,二级缓存和三级缓存它的速度比内存还要快。建议运行程序时将程序始终绑定到一颗CPU上运行,感兴趣的小伙伴可以学习一下"taskset"命令。
  TTY: 进程所在终端
  STAT:进程状态
    R:running
    S: interruptable sleeping
    D: uninterruptable sleeping
    T: stopped
    Z: zombie,僵尸进程,进程已经被杀死但就是不释放资源,这一般都是开发人员写的程序有BUG,如果有大量的僵尸进程一般的解决方案就是杀掉僵尸进程的父进程或者重启操作才能解决。
    +: 前台进程
    l: 多线程进程
    L:内存分页并带锁
    N:低优先级进程
    <: 高优先级进程
    s: session leader,会话(子进程)发起者
    ni: nice值
    pri: priority 优先级
    psr: processor CPU编号
    rtprio: 实时优先级,比较霸道,当它的优先级越高会尽可能的多的占用CPU资源。
  START:进程的启动时间
  TIME:进程的获取CPU的时间
  COMMAND:启动进程时调用的指令
[root@node101.yinzhengjie.org.cn ~]# ps a        #查看所有终端中的进程
PID TTY STAT TIME COMMAND
tty1 Ss+ : /sbin/agetty --noclear tty1 linux
pts/ Ss+ : -bash
pts/ Ss+ : -bash
pts/ Ss : -bash
pts/ R+ : ps a
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# ps a           #查看所有终端中的进程

[root@node101.yinzhengjie.org.cn ~]# ps x         #查看不链接终端的进程
PID TTY STAT TIME COMMAND
? Ss : /usr/lib/systemd/systemd --switched-root --system --deserialize
? S : [kthreadd]
? S : [ksoftirqd/]
? S< : [kworker/:0H]
? S : [migration/]
? S : [rcu_bh]
? S : [rcu_sched]
? S< : [lru-add-drain]
? S : [watchdog/]
? S : [watchdog/]
? S : [migration/]
? S : [ksoftirqd/]
? S< : [kworker/:0H]
? S : [watchdog/]
? S : [migration/]
? S : [ksoftirqd/]
? S : [kworker/:]
? S< : [kworker/:0H]
? S : [watchdog/]
? S : [migration/]
? S : [ksoftirqd/]
? S< : [kworker/:0H]
? S : [kdevtmpfs]
? S< : [netns]
? S : [khungtaskd]
? S< : [writeback]
? S< : [kintegrityd]
? S< : [bioset]
? S< : [bioset]
? S< : [bioset]
? S< : [kblockd]
? S< : [md]
? S< : [edac-poller]
? S< : [watchdogd]
? S : [kworker/:]
? S : [kswapd0]
? SN : [ksmd]
? S< : [crypto]
? S< : [kthrotld]
? S : [kworker/u8:]
? S< : [kmpath_rdacd]
? S< : [kaluad]
? S< : [kpsmoused]
? S< : [ipv6_addrconf]
? S : [kworker/:]
? S< : [deferwq]
? S : [kauditd]
? S< : [ata_sff]
? S : [scsi_eh_0]
? S< : [scsi_tmf_0]
? S : [scsi_eh_1]
? S< : [scsi_tmf_1]
? S : [kworker/u8:]
? S : [scsi_eh_2]
? S< : [scsi_tmf_2]
? S< : [ttm_swap]
? S : [irq/-vmwgfx]
? S< : [kworker/:1H]
? S< : [kworker/:1H]
? S< : [kdmflush]
? S< : [bioset]
? R : [kworker/:]
? S< : [kdmflush]
? S< : [bioset]
? S< : [bioset]
? S< : [xfsalloc]
? S< : [xfs_mru_cache]
? S< : [xfs-buf/dm-]
? S< : [xfs-data/dm-]
? S< : [xfs-conv/dm-]
? S< : [xfs-cil/dm-]
? S< : [xfs-reclaim/dm-]
? S< : [xfs-log/dm-]
? S< : [xfs-eofblocks/d]
? S : [xfsaild/dm-]
? Ss : /usr/lib/systemd/systemd-journald
? Ss : /usr/sbin/lvmetad -f
? Ss : /usr/lib/systemd/systemd-udevd
? S< : [xfs-buf/sda1]
? S< : [xfs-data/sda1]
? S< : [xfs-conv/sda1]
? S< : [xfs-cil/sda1]
? S< : [xfs-reclaim/sda]
? S< : [xfs-log/sda1]
? S< : [xfs-eofblocks/s]
? S : [xfsaild/sda1]
? S< : [kdmflush]
? S< : [bioset]
? S< : [xfs-buf/dm-]
? S< : [xfs-data/dm-]
? S< : [xfs-conv/dm-]
? S< : [xfs-cil/dm-]
? S< : [xfs-reclaim/dm-]
? S< : [xfs-log/dm-]
? S< : [xfs-eofblocks/d]
? S : [xfsaild/dm-]
? S<sl : /sbin/auditd
? Ss : /usr/lib/systemd/systemd-logind
? Ss : /usr/sbin/irqbalance --foreground
? Ssl : /usr/sbin/NetworkManager --no-daemon
? Ss : /usr/sbin/crond -n
? Ss : /usr/sbin/atd -f
? S< : [kworker/:1H]
? S< : [kworker/:1H]
? Ssl : /usr/bin/python2 -Es /usr/sbin/tuned -l -P
? Ss : /usr/sbin/sshd -D
? Ssl : /usr/sbin/rsyslogd -n
tty1 Ss+ : /sbin/agetty --noclear tty1 linux
? Ss : sshd: root@pts/
? Ss : sshd: root@pts/
? Ss : sshd: root@pts/
pts/ Ss+ : -bash
pts/ Ss+ : -bash
pts/ Ss : -bash
? S : [kworker/:]
? S : [kworker/:]
? S : [kworker/:]
? S : [kworker/:]
? S : [kworker/:]
pts/ R+ : ps x
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps x | wc -l [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps ax | wc -l [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# ps x           #查看不链接终端的进程

[root@node101.yinzhengjie.org.cn ~]# ps u             #查看进程所有者的信息
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 0.0 0.0 tty1 Ss+ : : /sbin/agetty --noclear tty1 linux
root 0.0 0.0 pts/ Ss+ : : -bash
root 0.0 0.0 pts/ Ss+ : : -bash
root 0.0 0.0 pts/ Ss : : -bash
root 0.0 0.0 pts/ R+ : : ps u
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# ps u           #查看进程所有者的信息

[root@node101.yinzhengjie.org.cn ~]# ps f               #显示进程所有者的信息
PID TTY STAT TIME COMMAND
pts/ Ss : -bash
pts/ S : \_ bash
pts/ S : \_ bash
pts/ R+ : \_ ps f
pts/ Ss+ : -bash
pts/ Ss+ : -bash
tty1 Ss+ : /sbin/agetty --noclear tty1 linux
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# pstree -p | grep sshd
|-sshd()-+-sshd()---bash()
| |-sshd()---bash()
| `-sshd()---bash()---bash()---bash()-+-grep()
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# ps f           #显示进程所有者的信息

[root@node101.yinzhengjie.org.cn ~]# ps L          #显示支持的属性列表
%cpu %CPU
%mem %MEM
_left LLLLLLLL
_left2 L2L2L2L2
_right RRRRRRRR
_right2 R2R2R2R2
_unlimited U
_unlimited2 U2
alarm ALARM
args COMMAND
atime TIME
blocked BLOCKED
bsdstart START
bsdtime TIME
c C
caught CAUGHT
cgroup CGROUP
class CLS
cls CLS
cmd CMD
comm COMMAND
command COMMAND
context CONTEXT
cp CP
cpuid CPUID
cputime TIME
drs DRS
dsiz DSIZ
egid EGID
egroup EGROUP
eip EIP
esp ESP
etime ELAPSED
etimes ELAPSED
euid EUID
euser EUSER
f F
fgid FGID
fgroup FGROUP
flag F
flags F
fname COMMAND
fsgid FSGID
fsgroup FSGROUP
fsuid FSUID
fsuser FSUSER
fuid FUID
fuser FUSER
gid GID
group GROUP
ignored IGNORED
intpri PRI
ipcns IPCNS
label LABEL
lastcpu C
lim LIM
longtname TTY
lsession SESSION
lstart STARTED
luid LUID
lwp LWP
m_drs DRS
m_size SIZE
m_trs TRS
machine MACHINE
maj_flt MAJFL
majflt MAJFLT
min_flt MINFL
minflt MINFLT
mntns MNTNS
netns NETNS
ni NI
nice NI
nlwp NLWP
nwchan WCHAN
opri PRI
ouid OWNER
pagein PAGEIN
pcpu %CPU
pending PENDING
pgid PGID
pgrp PGRP
pid PID
pidns PIDNS
pmem %MEM
policy POL
ppid PPID
pri PRI
pri_api API
pri_bar BAR
pri_baz BAZ
pri_foo FOO
priority PRI
psr PSR
rgid RGID
rgroup RGROUP
rss RSS
rssize RSS
rsz RSZ
rtprio RTPRIO
ruid RUID
ruser RUSER
s S
sched SCH
seat SEAT
sess SESS
session SESS
sgi_p P
sgi_rss RSS
sgid SGID
sgroup SGROUP
sid SID
sig PENDING
sig_block BLOCKED
sig_catch CATCHED
sig_ignore IGNORED
sig_pend SIGNAL
sigcatch CAUGHT
sigignore IGNORED
sigmask BLOCKED
size SIZE
slice SLICE
spid SPID
stackp STACKP
start STARTED
start_stack STACKP
start_time START
stat STAT
state S
stime STIME
suid SUID
supgid SUPGID
supgrp SUPGRP
suser SUSER
svgid SVGID
svgroup SVGROUP
svuid SVUID
svuser SVUSER
sz SZ
tgid TGID
thcgr THCGR
thcount THCNT
tid TID
time TIME
tname TTY
tpgid TPGID
trs TRS
trss TRSS
tsig PENDING
tsiz TSIZ
tt TT
tty TT
tty4 TTY
tty8 TTY
ucmd CMD
ucomm COMMAND
uid UID
uid_hack UID
uname USER
unit UNIT
user USER
userns USERNS
util C
utsns UTSNS
uunit UUNIT
vsize VSZ
vsz VSZ
wchan WCHAN
wname WCHAN
zone ZONE
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# ps L            #显示支持的属性列表

[root@node101.yinzhengjie.org.cn ~]# ps o pid,%cpu,%mem,cmd,uname,size  #显示定制的信息,支持的属性可查看"ps -L"
PID %CPU %MEM CMD USER SIZE
0.0 0.0 /sbin/agetty --noclear tty1 root
0.0 0.0 -bash root
0.0 0.0 -bash root
0.0 0.0 -bash root
0.0 0.0 bash root
0.0 0.0 bash root
0.0 0.0 ps o pid,%cpu,%mem,cmd,unam root
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# ps o pid,%cpu,%mem,cmd,uname,size  #显示定制的信息,支持的属性可查看"ps -L"

[root@node101.yinzhengjie.org.cn ~]# ps -C ping,vi   #显示指定命令,多个命令用,分隔
PID TTY TIME CMD
pts/ :: vi
pts/ :: ping
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# ps -C ping,vi       #显示指定命令,多个命令用,分隔

[root@node101.yinzhengjie.org.cn ~]# ps -L              #显示线程
PID LWP TTY TIME CMD
pts/ :: bash
pts/ :: bash
pts/ :: bash
pts/ :: mysqld_safe
pts/ :: ps
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# ps -L            #显示线程

[root@node101.yinzhengjie.org.cn ~]# ps -e               #显示所有进程,相当于-A
PID TTY TIME CMD
? :: systemd
? :: kthreadd
? :: ksoftirqd/
? :: kworker/:0H
? :: migration/
? :: rcu_bh
? :: rcu_sched
? :: lru-add-drain
? :: watchdog/
? :: watchdog/
? :: migration/
? :: ksoftirqd/
? :: kworker/:0H
? :: watchdog/
? :: migration/
? :: ksoftirqd/
? :: kworker/:
? :: kworker/:0H
? :: watchdog/
? :: migration/
? :: ksoftirqd/
? :: kworker/:0H
? :: kdevtmpfs
? :: netns
? :: khungtaskd
? :: writeback
? :: kintegrityd
? :: bioset
? :: bioset
? :: bioset
? :: kblockd
? :: md
? :: edac-poller
? :: watchdogd
? :: kworker/:
? :: kswapd0
? :: ksmd
? :: crypto
? :: kthrotld
? :: kworker/u8:
? :: kmpath_rdacd
? :: kaluad
? :: kpsmoused
? :: ipv6_addrconf
? :: kworker/:
? :: deferwq
? :: kauditd
? :: ata_sff
? :: scsi_eh_0
? :: scsi_tmf_0
? :: scsi_eh_1
? :: scsi_tmf_1
? :: kworker/u8:
? :: scsi_eh_2
? :: scsi_tmf_2
? :: ttm_swap
? :: irq/-vmwgfx
? :: kworker/:1H
? :: kworker/:1H
? :: kdmflush
? :: bioset
? :: kworker/:
? :: kdmflush
? :: bioset
? :: bioset
? :: xfsalloc
? :: xfs_mru_cache
? :: xfs-buf/dm-
? :: xfs-data/dm-
? :: xfs-conv/dm-
? :: xfs-cil/dm-
? :: xfs-reclaim/dm-
? :: xfs-log/dm-
? :: xfs-eofblocks/d
? :: xfsaild/dm-
? :: systemd-journal
? :: lvmetad
? :: systemd-udevd
? :: xfs-buf/sda1
? :: xfs-data/sda1
? :: xfs-conv/sda1
? :: xfs-cil/sda1
? :: xfs-reclaim/sda
? :: xfs-log/sda1
? :: xfs-eofblocks/s
? :: xfsaild/sda1
? :: kdmflush
? :: bioset
? :: xfs-buf/dm-
? :: xfs-data/dm-
? :: xfs-conv/dm-
? :: xfs-cil/dm-
? :: xfs-reclaim/dm-
? :: xfs-log/dm-
? :: xfs-eofblocks/d
? :: xfsaild/dm-
? :: auditd
? :: polkitd
? :: systemd-logind
? :: irqbalance
? :: dbus-daemon
? :: NetworkManager
? :: crond
? :: atd
? :: kworker/:1H
? :: kworker/:1H
? :: tuned
? :: sshd
? :: rsyslogd
tty1 :: agetty
? :: sshd
? :: sshd
? :: sshd
pts/ :: bash
pts/ :: bash
pts/ :: bash
? :: kworker/:
pts/ :: bash
pts/ :: bash
? :: kworker/:
? :: kworker/:
? :: kworker/:
? :: kworker/:
pts/ :: mysqld_safe
pts/ :: mysqld
? :: kworker/u8:
? :: kworker/:
pts/ :: ps
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps -e | wc -l [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps -A | wc -l [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# ps -e            #显示所有进程,相当于-A

[root@node101.yinzhengjie.org.cn ~]# ps -f             #显示完整格式程序信息
UID PID PPID C STIME TTY TIME CMD
root : pts/ :: -bash
root : pts/ :: bash
root : pts/ :: bash
root : pts/ :: /bin/sh /home/softwares/mysql//bin/mysqld_safe --datadir=/home/softwares/mysql/data/ --
root : pts/ :: ps -f
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# ps -f           #显示完整格式程序信息

[root@node101.yinzhengjie.org.cn ~]# ps -F                #显示更完整格式的进程信息
UID PID PPID C SZ RSS PSR STIME TTY TIME CMD
root : pts/ :: -bash
root : pts/ :: bash
root : pts/ :: bash
root : pts/ :: /bin/sh /home/softwares/mysql//bin/mysqld_safe --datadir=/home/software
root : pts/ :: ps -F
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# ps -F           #显示更完整格式的进程信息

[root@node101.yinzhengjie.org.cn ~]# ps -H       #以进程层级格式显示进程相关信息
PID TTY TIME CMD
pts/ :: bash
pts/ :: bash
pts/ :: bash
pts/ :: ps
pts/ :: mysqld_safe
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# ps -H           #以进程层级格式显示进程相关信息

[root@node101.yinzhengjie.org.cn ~]# ps o pid,cmd,user,euser,ruser
PID CMD USER EUSER RUSER
/sbin/agetty --noclear tty1 root root root
-bash root root root
-bash root root root
-bash root root root
bash root root root
bash root root root
/bin/sh /home/softwares/mys root root root
su yinzhengjie root root root
passwd root root yinzhengjie
ps o pid,cmd,user,euser,rus root root root
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps -u yinzhengjie     #指定有效的用户ID或名称
PID TTY TIME CMD
pts/ :: bash
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# ps -u yinzhengjie     #指定有效的用户ID或名称

[root@node101.yinzhengjie.org.cn ~]# ps o pid,cmd,user,euser,ruser
PID CMD USER EUSER RUSER
/sbin/agetty --noclear tty1 root root root
-bash root root root
-bash root root root
-bash root root root
bash root root root
bash root root root
/bin/sh /home/softwares/mys root root root
su yinzhengjie root root root
passwd root root yinzhengjie
ps o pid,cmd,user,euser,rus root root root
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps -u yinzhengjie
PID TTY TIME CMD
pts/ :: bash
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps -U yinzhengjie         #指定真正的用户ID或名称
PID TTY TIME CMD
pts/ :: bash
pts/ :: passwd
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# ps -U yinzhengjie     #指定真正的用户ID或名称

[root@node101.yinzhengjie.org.cn ~]# ps -g yinzhengjie         #gid或groupname 指定有效的gid或组名称
PID TTY TIME CMD
pts/ :: bash
pts/ :: passwd
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# ps -g yinzhengjie     #gid或groupname 指定有效的gid或组名称

[root@node101.yinzhengjie.org.cn ~]# ps -G yinzhengjie        #指定真正的gid或组名称
PID TTY TIME CMD
pts/ :: bash
pts/ :: passwd
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# ps -G yinzhengjie     #指定真正的gid或组名称(和-U)类似

[root@node101.yinzhengjie.org.cn ~]# ps
PID TTY TIME CMD
pts/ :: bash
pts/ :: bash
pts/ :: bash
pts/ :: mysqld_safe
pts/ :: ps
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps -p      #显示指pid的进程
PID TTY TIME CMD
pts/ :: mysqld_safe
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# ps -p 7590          #显示指pid的进程

[root@node101.yinzhengjie.org.cn ~]# ps f
PID TTY STAT TIME COMMAND
pts/ S+ : passwd
pts/ Ss : -bash
pts/ S : \_ bash
pts/ S : \_ bash
pts/ R+ : \_ ps f
pts/ Ss : -bash
pts/ S : \_ su yinzhengjie
pts/ Ss+ : -bash
pts/ S : /bin/sh /home/softwares/mysql//bin/mysqld_safe --datadir=/home/softwares/mysql/data/ --pid-file=/home/softwa
tty1 Ss+ : /sbin/agetty --noclear tty1 linux
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps --ppid       #显示属于pid的子进程
PID TTY TIME CMD
pts/ :: bash
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps --ppid
PID TTY TIME CMD
pts/ :: bash
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# ps --ppid 7149       #显示属于pid的子进程

[root@node101.yinzhengjie.org.cn ~]# ps -M                     #显示SELinux信息,相当于Z
LABEL PID TTY TIME CMD
- pts/ :: bash
- pts/ :: bash
- pts/ :: bash
- pts/ :: mysqld_safe
- pts/ :: ps
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps Z
LABEL PID TTY STAT TIME COMMAND
- tty1 Ss+ : /sbin/agetty --noclear tty1 linux
- pts/ Ss+ : -bash
- pts/ Ss : -bash
- pts/ Ss : -bash
- pts/ S : bash
- pts/ S : bash
- pts/ S : /bin/sh /home/softwares/mysql//bin/mysqld_safe --datadir=/home/softwares/mys
- pts/ S : su yinzhengjie
- pts/ S+ : passwd
- pts/ R+ : ps Z
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# ps -M             #显示SELinux信息,相当于Z

[root@node101.yinzhengjie.org.cn ~]# ps axo pid,cmd,psr,ni,pri,rtprio  #查看pid,cmd,psr(CPU编号,从0开始),ni(NICE)值,pri(priority优先级),rtprio(实时优先级)
PID CMD PSR NI PRI RTPRIO
/usr/lib/systemd/systemd -- -
[kthreadd] -
[ksoftirqd/] -
[kworker/:0H] - -
[migration/] -
[rcu_bh] -
[rcu_sched] -
[lru-add-drain] - -
[watchdog/] -
[watchdog/] -
[migration/] -
[ksoftirqd/] -
[kworker/:0H] - -
[watchdog/] -
[migration/] -
[ksoftirqd/] -
[kworker/:] -
[kworker/:0H] - -
[watchdog/] -
[migration/] -
[ksoftirqd/] -
[kworker/:0H] - -
[kdevtmpfs] -
[netns] - -
[khungtaskd] -
[writeback] - -
[kintegrityd] - -
[bioset] - -
[bioset] - -
[bioset] - -
[kblockd] - -
[md] - -
[edac-poller] - -
[watchdogd] - -
[kworker/:] -
[kswapd0] -
[ksmd] -
[crypto] - -
[kthrotld] - -
[kworker/u8:] -
[kmpath_rdacd] - -
[kaluad] - -
[kpsmoused] - -
[ipv6_addrconf] - -
[kworker/:] -
[deferwq] - -
[kauditd] -
[ata_sff] - -
[scsi_eh_0] -
[scsi_tmf_0] - -
[scsi_eh_1] -
[scsi_tmf_1] - -
[scsi_eh_2] -
[scsi_tmf_2] - -
[ttm_swap] - -
[irq/-vmwgfx] -
[kworker/:1H] - -
[kworker/:1H] - -
[kdmflush] - -
[bioset] - -
[kworker/:] -
[kdmflush] - -
[bioset] - -
[bioset] - -
[xfsalloc] - -
[xfs_mru_cache] - -
[xfs-buf/dm-] - -
[xfs-data/dm-] - -
[xfs-conv/dm-] - -
[xfs-cil/dm-] - -
[xfs-reclaim/dm-] - -
[xfs-log/dm-] - -
[xfs-eofblocks/d] - -
[xfsaild/dm-] -
/usr/lib/systemd/systemd-jo -
/usr/sbin/lvmetad -f -
/usr/lib/systemd/systemd-ud -
[xfs-buf/sda1] - -
[xfs-data/sda1] - -
[xfs-conv/sda1] - -
[xfs-cil/sda1] - -
[xfs-reclaim/sda] - -
[xfs-log/sda1] - -
[xfs-eofblocks/s] - -
[xfsaild/sda1] -
[kdmflush] - -
[bioset] - -
[xfs-buf/dm-] - -
[xfs-data/dm-] - -
[xfs-conv/dm-] - -
[xfs-cil/dm-] - -
[xfs-reclaim/dm-] - -
[xfs-log/dm-] - -
[xfs-eofblocks/d] - -
[xfsaild/dm-] -
/sbin/auditd - -
/usr/lib/polkit-/polkitd - -
/usr/lib/systemd/systemd-lo -
/usr/sbin/irqbalance --fore -
/usr/bin/dbus-daemon --syst -
/usr/sbin/NetworkManager -- -
/usr/sbin/crond -n -
/usr/sbin/atd -f -
[kworker/:1H] - -
[kworker/:1H] - -
/usr/bin/python2 -Es /usr/s -
/usr/sbin/sshd -D -
/usr/sbin/rsyslogd -n -
/sbin/agetty --noclear tty1 -
sshd: root@pts/ -
sshd: root@pts/ -
sshd: root@pts/ -
-bash -
-bash -
-bash -
[kworker/:] -
bash -
bash -
[kworker/:] -
/bin/sh /home/softwares/mys -
/home/softwares/mysql/bin/m -
[kworker/u8:] -
su yinzhengjie -
bash -
bash -
bash -
bash -
passwd -
[kworker/:] -
[kworker/:] -
ping -f 127.0.0.1 -S -
[kworker/:] -
ps axo pid,cmd,psr,ni,pri,r -
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# ps axo pid,cmd,psr,ni,pri,rtprio #查看pid,cmd,psr(CPU编号,从0开始),ni(NICE)值,pri(priority优先级),rtprio(实时优先级)

[root@node101.yinzhengjie.org.cn ~]# ps -x                               #查询你拥有的所有进程
PID TTY STAT TIME COMMAND
? Ss : /usr/lib/systemd/systemd --switched-root --system --deserialize
? S : [kthreadd]
? S : [ksoftirqd/]
? S< : [kworker/:0H]
? S : [migration/]
? S : [rcu_bh]
? S : [rcu_sched]
? S< : [lru-add-drain]
? S : [watchdog/]
? S : [watchdog/]
? S : [migration/]
? S : [ksoftirqd/]
? S< : [kworker/:0H]
? S : [watchdog/]
? S : [migration/]
? S : [ksoftirqd/]
? S : [kworker/:]
? S< : [kworker/:0H]
? S : [watchdog/]
? S : [migration/]
? S : [ksoftirqd/]
? S< : [kworker/:0H]
? S : [kdevtmpfs]
? S< : [netns]
? S : [khungtaskd]
? S< : [writeback]
? S< : [kintegrityd]
? S< : [bioset]
? S< : [bioset]
? S< : [bioset]
? S< : [kblockd]
? S< : [md]
? S< : [edac-poller]
? S< : [watchdogd]
? R : [kworker/:]
? S : [kswapd0]
? SN : [ksmd]
? S< : [crypto]
? S< : [kthrotld]
? S : [kworker/u8:]
? S< : [kmpath_rdacd]
? S< : [kaluad]
? S< : [kpsmoused]
? S< : [ipv6_addrconf]
? S : [kworker/:]
? S< : [deferwq]
? S : [kauditd]
? S< : [ata_sff]
? S : [scsi_eh_0]
? S< : [scsi_tmf_0]
? S : [scsi_eh_1]
? S< : [scsi_tmf_1]
? S : [scsi_eh_2]
? S< : [scsi_tmf_2]
? S< : [ttm_swap]
? S : [irq/-vmwgfx]
? S< : [kworker/:1H]
? S< : [kworker/:1H]
? S< : [kdmflush]
? S< : [bioset]
? S : [kworker/:]
? S< : [kdmflush]
? S< : [bioset]
? S< : [bioset]
? S< : [xfsalloc]
? S< : [xfs_mru_cache]
? S< : [xfs-buf/dm-]
? S< : [xfs-data/dm-]
? S< : [xfs-conv/dm-]
? S< : [xfs-cil/dm-]
? S< : [xfs-reclaim/dm-]
? S< : [xfs-log/dm-]
? S< : [xfs-eofblocks/d]
? S : [xfsaild/dm-]
? Ss : /usr/lib/systemd/systemd-journald
? Ss : /usr/sbin/lvmetad -f
? Ss : /usr/lib/systemd/systemd-udevd
? S< : [xfs-buf/sda1]
? S< : [xfs-data/sda1]
? S< : [xfs-conv/sda1]
? S< : [xfs-cil/sda1]
? S< : [xfs-reclaim/sda]
? S< : [xfs-log/sda1]
? S< : [xfs-eofblocks/s]
? S : [xfsaild/sda1]
? S< : [kdmflush]
? S< : [bioset]
? S< : [xfs-buf/dm-]
? S< : [xfs-data/dm-]
? S< : [xfs-conv/dm-]
? S< : [xfs-cil/dm-]
? S< : [xfs-reclaim/dm-]
? S< : [xfs-log/dm-]
? S< : [xfs-eofblocks/d]
? S : [xfsaild/dm-]
? S<sl : /sbin/auditd
? Ss : /usr/lib/systemd/systemd-logind
? Ss : /usr/sbin/irqbalance --foreground
? Ssl : /usr/sbin/NetworkManager --no-daemon
? Ss : /usr/sbin/crond -n
? Ss : /usr/sbin/atd -f
? S< : [kworker/:1H]
? S< : [kworker/:1H]
? Ssl : /usr/bin/python2 -Es /usr/sbin/tuned -l -P
? Ss : /usr/sbin/sshd -D
? Ssl : /usr/sbin/rsyslogd -n
tty1 Ss+ : /sbin/agetty --noclear tty1 linux
? Ss : sshd: root@pts/
? Ss : sshd: root@pts/
? Ss : sshd: root@pts/
pts/ Ss : -bash
pts/ Ss : -bash
pts/ Ss : -bash
? S : [kworker/:]
pts/ S : bash
pts/ S : bash
? R : [kworker/:]
pts/ S : /bin/sh /home/softwares/mysql//bin/mysqld_safe --datadir=/home/softwares/mysql/data/ --pid-file=/home/softwa
? S : [kworker/u8:]
pts/ S : su yinzhengjie
pts/ S+ : passwd
? S : [kworker/:]
pts/ R+ : ping -f 127.0.0.1 -S
? S : [kworker/:]
? S : [kworker/:]
pts/ R+ : ps -x
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# ps -x #查询你拥有的所有进程

[root@node101.yinzhengjie.org.cn ~]# ps -ft pts/              #要按tty显示所属进程
UID PID PPID C STIME TTY TIME CMD
root : pts/ :: -bash
root : pts/ :: ps -ft pts/
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps
PID TTY TIME CMD
pts/ :: bash
pts/ :: ps
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# ps -ft pts/3 #要按tty显示所属进程

[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep ssh | grep -v grep
root : ? :: /usr/sbin/sshd -D
root : ? :: sshd: root@pts/
root : ? :: sshd: root@pts/
root : ? :: sshd: root@pts/
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps -C sshd -o pid=       #查找指定进程名所有的所属PID,在编写需要从std输出或文件读取PID的脚本时这个参数很有用 [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# ps -C sshd -o pid=       #查找指定进程名所有的所属PID,在编写需要从std输出或文件读取PID的脚本时这个参数很有用

[root@node101.yinzhengjie.org.cn ~]# ps -eo comm,etime,user | grep sshd          #检查一个进程的执行时间
sshd :: root
sshd :: root
sshd :: root
sshd :: root
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# ps -eo comm,etime,user | grep sshd    #检查一个进程的执行时间

[root@node101.yinzhengjie.org.cn ~]# ps --context                             #显示安全信息
PID CONTEXT COMMAND
- -bash
- ps --context
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps -eM
LABEL PID TTY TIME CMD
- ? :: systemd
- ? :: kthreadd
- ? :: ksoftirqd/
- ? :: kworker/:0H
- ? :: migration/
- ? :: rcu_bh
- ? :: rcu_sched
- ? :: lru-add-drain
- ? :: watchdog/
- ? :: watchdog/
- ? :: migration/
- ? :: ksoftirqd/
- ? :: kworker/:0H
- ? :: watchdog/
- ? :: migration/
- ? :: ksoftirqd/
- ? :: kworker/:
- ? :: kworker/:0H
- ? :: watchdog/
- ? :: migration/
- ? :: ksoftirqd/
- ? :: kworker/:0H
- ? :: kdevtmpfs
- ? :: netns
- ? :: khungtaskd
- ? :: writeback
- ? :: kintegrityd
- ? :: bioset
- ? :: bioset
- ? :: bioset
- ? :: kblockd
- ? :: md
- ? :: edac-poller
- ? :: watchdogd
- ? :: kworker/:
- ? :: kswapd0
- ? :: ksmd
- ? :: crypto
- ? :: kthrotld
- ? :: kworker/u8:
- ? :: kmpath_rdacd
- ? :: kaluad
- ? :: kpsmoused
- ? :: ipv6_addrconf
- ? :: kworker/:
- ? :: deferwq
- ? :: kauditd
- ? :: ata_sff
- ? :: scsi_eh_0
- ? :: scsi_tmf_0
- ? :: scsi_eh_1
- ? :: scsi_tmf_1
- ? :: scsi_eh_2
- ? :: scsi_tmf_2
- ? :: ttm_swap
- ? :: irq/-vmwgfx
- ? :: kworker/:1H
- ? :: kworker/:1H
- ? :: kdmflush
- ? :: bioset
- ? :: kworker/:
- ? :: kdmflush
- ? :: bioset
- ? :: bioset
- ? :: xfsalloc
- ? :: xfs_mru_cache
- ? :: xfs-buf/dm-
- ? :: xfs-data/dm-
- ? :: xfs-conv/dm-
- ? :: xfs-cil/dm-
- ? :: xfs-reclaim/dm-
- ? :: xfs-log/dm-
- ? :: xfs-eofblocks/d
- ? :: xfsaild/dm-
- ? :: systemd-journal
- ? :: lvmetad
- ? :: systemd-udevd
- ? :: xfs-buf/sda1
- ? :: xfs-data/sda1
- ? :: xfs-conv/sda1
- ? :: xfs-cil/sda1
- ? :: xfs-reclaim/sda
- ? :: xfs-log/sda1
- ? :: xfs-eofblocks/s
- ? :: xfsaild/sda1
- ? :: kdmflush
- ? :: bioset
- ? :: xfs-buf/dm-
- ? :: xfs-data/dm-
- ? :: xfs-conv/dm-
- ? :: xfs-cil/dm-
- ? :: xfs-reclaim/dm-
- ? :: xfs-log/dm-
- ? :: xfs-eofblocks/d
- ? :: xfsaild/dm-
- ? :: auditd
- ? :: polkitd
- ? :: systemd-logind
- ? :: irqbalance
- ? :: dbus-daemon
- ? :: NetworkManager
- ? :: crond
- ? :: atd
- ? :: kworker/:1H
- ? :: kworker/:1H
- ? :: tuned
- ? :: sshd
- ? :: rsyslogd
- tty1 :: agetty
- ? :: sshd
- ? :: sshd
- ? :: sshd
- pts/ :: bash
- pts/ :: bash
- pts/ :: bash
- ? :: kworker/:
- pts/ :: bash
- pts/ :: bash
- ? :: kworker/:
- pts/ :: mysqld_safe
- pts/ :: mysqld
- ? :: kworker/u8:
- pts/ :: su
- pts/ :: bash
- pts/ :: bash
- pts/ :: bash
- pts/ :: bash
- pts/ :: passwd
- ? :: kworker/:
- pts/ :: ping
- ? :: kworker/:
- ? :: kworker/:
- pts/ :: ps
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# ps --context #显示安全信息

[root@node101.yinzhengjie.org.cn ~]# ps -eo euser,ruser,suser,fuser,f,comm,label
EUSER RUSER SUSER FUSER F COMMAND LABEL
root root root root systemd -
root root root root kthreadd -
root root root root ksoftirqd/ -
root root root root kworker/:0H -
root root root root migration/ -
root root root root rcu_bh -
root root root root rcu_sched -
root root root root lru-add-drain -
root root root root watchdog/ -
root root root root watchdog/ -
root root root root migration/ -
root root root root ksoftirqd/ -
root root root root kworker/:0H -
root root root root watchdog/ -
root root root root migration/ -
root root root root ksoftirqd/ -
root root root root kworker/: -
root root root root kworker/:0H -
root root root root watchdog/ -
root root root root migration/ -
root root root root ksoftirqd/ -
root root root root kworker/:0H -
root root root root kdevtmpfs -
root root root root netns -
root root root root khungtaskd -
root root root root writeback -
root root root root kintegrityd -
root root root root bioset -
root root root root bioset -
root root root root bioset -
root root root root kblockd -
root root root root md -
root root root root edac-poller -
root root root root watchdogd -
root root root root kworker/: -
root root root root kswapd0 -
root root root root ksmd -
root root root root crypto -
root root root root kthrotld -
root root root root kworker/u8: -
root root root root kmpath_rdacd -
root root root root kaluad -
root root root root kpsmoused -
root root root root ipv6_addrconf -
root root root root kworker/: -
root root root root deferwq -
root root root root kauditd -
root root root root ata_sff -
root root root root scsi_eh_0 -
root root root root scsi_tmf_0 -
root root root root scsi_eh_1 -
root root root root scsi_tmf_1 -
root root root root scsi_eh_2 -
root root root root scsi_tmf_2 -
root root root root ttm_swap -
root root root root irq/-vmwgfx -
root root root root kworker/:1H -
root root root root kworker/:1H -
root root root root kdmflush -
root root root root bioset -
root root root root kworker/: -
root root root root kdmflush -
root root root root bioset -
root root root root bioset -
root root root root xfsalloc -
root root root root xfs_mru_cache -
root root root root xfs-buf/dm- -
root root root root xfs-data/dm- -
root root root root xfs-conv/dm- -
root root root root xfs-cil/dm- -
root root root root xfs-reclaim/dm- -
root root root root xfs-log/dm- -
root root root root xfs-eofblocks/d -
root root root root xfsaild/dm- -
root root root root systemd-journal -
root root root root lvmetad -
root root root root systemd-udevd -
root root root root xfs-buf/sda1 -
root root root root xfs-data/sda1 -
root root root root xfs-conv/sda1 -
root root root root xfs-cil/sda1 -
root root root root xfs-reclaim/sda -
root root root root xfs-log/sda1 -
root root root root xfs-eofblocks/s -
root root root root xfsaild/sda1 -
root root root root kdmflush -
root root root root bioset -
root root root root xfs-buf/dm- -
root root root root xfs-data/dm- -
root root root root xfs-conv/dm- -
root root root root xfs-cil/dm- -
root root root root xfs-reclaim/dm- -
root root root root xfs-log/dm- -
root root root root xfs-eofblocks/d -
root root root root xfsaild/dm- -
root root root root auditd -
polkitd polkitd polkitd polkitd polkitd -
root root root root systemd-logind -
root root root root irqbalance -
dbus dbus dbus dbus dbus-daemon -
root root root root NetworkManager -
root root root root crond -
root root root root atd -
root root root root kworker/:1H -
root root root root kworker/:1H -
root root root root tuned -
root root root root sshd -
root root root root rsyslogd -
root root root root agetty -
root root root root sshd -
root root root root sshd -
root root root root sshd -
root root root root bash -
root root root root bash -
root root root root bash -
root root root root kworker/: -
root root root root bash -
root root root root bash -
root root root root kworker/: -
root root root root mysqld_safe -
mysql mysql mysql mysql mysqld -
root root root root kworker/u8: -
root root root root su -
yinzhen+ yinzhen+ yinzhen+ yinzhen+ bash -
yinzhen+ yinzhen+ yinzhen+ yinzhen+ bash -
yinzhen+ yinzhen+ yinzhen+ yinzhen+ bash -
yinzhen+ yinzhen+ yinzhen+ yinzhen+ bash -
root yinzhen+ root root passwd -
root root root root kworker/: -
root root root root ping -
root root root root kworker/: -
root root root root kworker/: -
root root root root ps -
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# ps -eo euser,ruser,suser,fuser,f,comm,label

4>.tastset命令绑定程序到一颗CPU

[root@node101.yinzhengjie.org.cn ~]# ping -f 127.0.0.1 -S 65507    #一个终端执行测试命令
[root@node101.yinzhengjie.org.cn ~]# ps axo pid,cmd,psr,ni,pri,rtprio  | grep -e " PID\|ping" | grep -v grep
PID CMD PSR NI PRI RTPRIO
ping -f 127.0.0.1 -S -
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps axo pid,cmd,psr,ni,pri,rtprio | grep -e " PID\|ping" | grep -v grep
PID CMD PSR NI PRI RTPRIO
ping -f 127.0.0.1 -S -
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps axo pid,cmd,psr,ni,pri,rtprio | grep -e " PID\|ping" | grep -v grep
PID CMD PSR NI PRI RTPRIO
ping -f 127.0.0.1 -S -
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps axo pid,cmd,psr,ni,pri,rtprio | grep -e " PID\|ping" | grep -v grep #不难发现,ping进程总是不断的切换CPU执行。
PID CMD PSR NI PRI RTPRIO
ping -f 127.0.0.1 -S -
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# taskset -p       #查看7972进程可以运行的CPU个数
pid 's current affinity mask: f
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# taskset -cp     #我们将7972进程绑定到计算机第一颗CPU上(计算机计数是从0开始的)
pid 's current affinity list: 0-3
pid 's new affinity list: 0
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# taskset -p
pid 's current affinity mask: 1
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps axo pid,cmd,psr,ni,pri,rtprio | grep -e " PID\|ping" | grep -v grep      #再次查看CPU的运行时所在的CPU核心始终为第一颗CPU(编号为"")
PID CMD PSR NI PRI RTPRIO
ping -f 127.0.0.1 -S -
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps axo pid,cmd,psr,ni,pri,rtprio | grep -e " PID\|ping" | grep -v grep
PID CMD PSR NI PRI RTPRIO
ping -f 127.0.0.1 -S -
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps axo pid,cmd,psr,ni,pri,rtprio | grep -e " PID\|ping" | grep -v grep
PID CMD PSR NI PRI RTPRIO
ping -f 127.0.0.1 -S -
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps axo pid,cmd,psr,ni,pri,rtprio | grep -e " PID\|ping" | grep -v grep
PID CMD PSR NI PRI RTPRIO
ping -f 127.0.0.1 -S -
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps axo pid,cmd,psr,ni,pri,rtprio | grep -e " PID\|ping" | grep -v grep
PID CMD PSR NI PRI RTPRIO
ping -f 127.0.0.1 -S -
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# taskset -cp 0 7972    #我们将7972进程绑定到计算机第一颗CPU上(计算机计数是从0开始的)

5>.使用watch实用程序执行重复的输出以实现对就程进行实时的监视 

[root@node101.yinzhengjie.org.cn ~]# watch -n  'ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head'

6>.进程优先级

进程优先级调整
  静态优先级:-
  进程默认启动时的nice值为0,优先级为120
  只有根用户才能降低nice值(提高优先性),数字越小优先级越高,nice的优先级数字范围是-20~19 nice命令
  nice [OPTION] [COMMAND [ARG]...] renice命令
  renice [-n] priority pid... 查看
  ps axo pid,comm,ni 温馨提示:
  进程优先级高程序运行的不一定会很快,它和程序设计有着密切关系,我们调优先级起到的作用是有限的。
[root@node101.yinzhengjie.org.cn ~]# ps axo pid,cmd,nice  | grep -e " PID\|ping" | grep -v grep
PID CMD NI
ping -f 127.0.0.1 -S
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# renice -n -       #将优先级设置为-(nice的最高优先级)
(process ID) old priority , new priority -
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps axo pid,cmd,nice | grep -e " PID\|ping" | grep -v grep
PID CMD NI
ping -f 127.0.0.1 -S -
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# renice -n -20 7972    #将优先级设置为-20(nice的最高优先级)

[root@node101.yinzhengjie.org.cn ~]# nice -n - ping -f 127.0.0.1 -S         #启动程序时可以指定优先级
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps axo pid,cmd,nice | grep -e " PID\|ping" | grep -v grep
PID CMD NI
ping -f 127.0.0.1 -S -
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nice - ping -f 127.0.0.1 -S
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps axo pid,cmd,nice | grep -e " PID\|ping" | grep -v grep
PID CMD NI
ping -f 127.0.0.1 -S
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nice -- ping -f 127.0.0.1 -S
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps axo pid,cmd,nice | grep -e " PID\|ping" | grep -v grep
PID CMD NI
ping -f 127.0.0.1 -S -
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# nice -n -5 ping -f 127.0.0.1 -S 65507   #启动程序时可以指定优先级

7>.搜索进程

最灵活:
  ps 选项 | 其它命令

按预定义的模式:pgrep
  pgrep [options] pattern
  -u uid: effective user,生效者
  -U uid: real user,真正发起运行命令者
  -t terminal: 与指定终端相关的进程
  -l: 显示进程名
  -a: 显示完整格式的进程名
  -P pid: 显示指定进程的子进程

按确切的程序名称:/sbin/pidof
  pidof bash
[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep bash
root : pts/ :: -bash
root : pts/ :: -bash
root : pts/ :: -bash
root : pts/ :: bash
root : pts/ :: bash
yinzhen+ : pts/ :: bash
yinzhen+ : pts/ :: bash
yinzhen+ : pts/ :: bash
yinzhen+ : pts/ :: bash
root : pts/ :: grep --color=auto bash
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep bash | wc -l [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# pidof bash [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# pidof bash

三.系统工具

[root@node101.yinzhengjie.org.cn ~]# uptime       #显示当前时间,系统已启动的时间、当前上线人数,系统平均负载(、、10分钟的平均负载,一般不会超过1)
:: up min, user, load average: 0.04, 0.02, 0.01
[root@node101.yinzhengjie.org.cn ~]# 系统平均负载:
  指在特定时间间隔内运行队列中的平均进程数
  通常每个CPU内核的当前活动进程数不大于3,那么系统的性能良好。如果每个CPU内核的任务数大于5,那么此主机的性能有严重问题
  如果linux主机是1个双核CPU,当Load Average 为6的时候说明机器已经被充分使用

四.进程管理工具

博主推荐阅读:
https://www.cnblogs.com/yinzhengjie/p/10367853.html

五.内存工具

博主推荐阅读:
https://www.cnblogs.com/yinzhengjie/p/6489374.html

六.远程主机系统监控-glances命令

1>.安装glances工具

[root@node101.yinzhengjie.org.cn ~]# yum -y install glances
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* epel: mirrors.tuna.tsinghua.edu.cn
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package glances.noarch :2.5.-.el7 will be installed
--> Processing Dependency: python-psutil >= 2.0. for package: glances-2.5.-.el7.noarch
--> Running transaction check
---> Package python2-psutil.x86_64 :2.2.-.el7 will be installed
--> Finished Dependency Resolution Dependencies Resolved =======================================================================================================================================
Package Arch Version Repository Size
=======================================================================================================================================
Installing:
glances noarch 2.5.-.el7 epel k
Installing for dependencies:
python2-psutil x86_64 2.2.-.el7 epel k Transaction Summary
=======================================================================================================================================
Install Package (+ Dependent package) Total download size: k
Installed size: 1.9 M
Downloading packages:
(/): glances-2.5.-.el7.noarch.rpm | kB ::
(/): python2-psutil-2.2.-.el7.x86_64.rpm | kB ::
---------------------------------------------------------------------------------------------------------------------------------------
Total kB/s | kB ::
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : python2-psutil-2.2.-.el7.x86_64 /
Installing : glances-2.5.-.el7.noarch /
Verifying : glances-2.5.-.el7.noarch /
Verifying : python2-psutil-2.2.-.el7.x86_64 / Installed:
glances.noarch :2.5.-.el7 Dependency Installed:
python2-psutil.x86_64 :2.2.-.el7 Complete!
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# yum -y install glances      #安装glances依赖于EPEL源

2>.服务器模式

[root@node101.yinzhengjie.org.cn ~]# glances -s -B 127.0.0.1      #启用服务端,启动后会自动绑定端口
Glances server is running on 127.0.0.1:

3>.客户端模式

[root@node101.yinzhengjie.org.cn ~]# glances -c 127.0.0.1      #连接成功后会弹出如下图所示的监控

Linux操作系统的进程管理

4>.内建命令

a:
Sort processes automatically
l:
Show/hide logs
c:
Sort processes by CPU%
b:
Bytes or bits for network I/O
m:
Sort processes by MEM%
w:
Delete warning logs
p:
Sort processes by name
x:
Delete warning and critical logs
i:
Sort processes by I/O rate
:
Global CPU or per-CPU stats
d:
Show/hide disk I/O stats
h:
Show/hide this help screen
f:
Show/hide file system stats
t:
View network I/O as combination
n:
Show/hide network stats
u:
View cumulative network I/O
s:
Show/hide sensors stats
q:
Quit (Esc and Ctrl-C also work)
y:
Show/hide hddtemp stats

5>.常用选项

-b:
以Byte为单位显示网卡数据速率
-d:
关闭磁盘I/O模块
-f /path/to/somefile:
设定输入文件位置
-o {HTML|CSV}:
输出格式
-m:
禁用mount模块
-n:
禁用网络模块
-t:
延迟时间间隔
-:
每个CPU的相关数据单独显示

七.dstat命令(系统资源统计,可代替vmstat,iostat)

1>.安装dstat

[root@node101.yinzhengjie.org.cn ~]# yum -y install dstat
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* epel: mirrors.tuna.tsinghua.edu.cn
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package dstat.noarch :0.7.-.el7 will be installed
--> Finished Dependency Resolution Dependencies Resolved ================================================================================================================
Package Arch Version Repository Size
================================================================================================================
Installing:
dstat noarch 0.7.-.el7 base k Transaction Summary
================================================================================================================
Install Package Total download size: k
Installed size: k
Downloading packages:
dstat-0.7.-.el7.noarch.rpm | kB ::
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : dstat-0.7.-.el7.noarch /
Verifying : dstat-0.7.-.el7.noarch / Installed:
dstat.noarch :0.7.-.el7 Complete!
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# yum -y install dstat

2>.使用dstat默认选项查看系统资源状态

[root@node101.yinzhengjie.org.cn ~]# dstat
You did not select any stats, using -cdngy by default.
----total-cpu-usage---- -dsk/total- -net/total- ---paging-- ---system--
usr sys idl wai hiq siq| read writ| recv send| in out | int csw
| 48k 3721k| | |
| | 60B 826B| |
| | 60B 346B| |
| | 13k 906k| |
| | 15k 1043k| |
| | 15k 1098k| |
| | 16k 1118k| |
| | 15k 1118k| |
| 248M| 15k 1064k| |
| 316M| 13k 945k| |
| 494M| 12k 907k| |
| 314M| 14k 981k| |
| 151M| 14k 1018k| |
| 257M| 14k 1010k| |
| 156M| 15k 1034k| |
| | 15k 1061k| |
| | 15k 1073k| |
| |2372B 141k| |
| | 60B 346B| |
| 459M| 60B 346B| | ^C
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# dstat

Linux操作系统的进程管理

3>.常用选项

-c:
显示cpu相关信息
-C:
  #,#,...,total
-d:
  显示disk相关信息
-D:
  total,sda,sdb,...
-g:
  显示page相关统计数据
-m:
  显示memory相关统计数据
-n:
  显示network相关统计数据
-p:
  显示process相关统计数据
-r:
  显示io请求相关的统计数据
-s:
  显示swapped相关的统计数据
--tcp
--udp
--unix
--raw
--socket
--ipc
--top-cpu:
  显示最占用CPU的进程
--top-io:
  显示最占用io的进程
--top-mem:
  显示最占用内存的进程
--top-latency:
  显示延迟最大的进程

八.iotop命令(一个用来监视磁盘I/O使用状况的top类工具)

1>.安装iotop(iotop具有与top相似的UI,其中包括PID、用户、I/O、进程等相关信息,可查看每个进程是如何使用IO)

[root@node101.yinzhengjie.org.cn ~]# yum -y install iotop
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* epel: mirrors.tuna.tsinghua.edu.cn
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package iotop.noarch :0.6-.el7 will be installed
--> Finished Dependency Resolution Dependencies Resolved ================================================================================================================
Package Arch Version Repository Size
================================================================================================================
Installing:
iotop noarch 0.6-.el7 base k Transaction Summary
================================================================================================================
Install Package Total download size: k
Installed size: k
Downloading packages:
iotop-0.6-.el7.noarch.rpm | kB ::
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : iotop-0.6-.el7.noarch /
Verifying : iotop-0.6-.el7.noarch / Installed:
iotop.noarch :0.6-.el7 Complete!
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# yum -y install iotop

2>.iotop输出信息

第一行:Read和Write速率总计
第二行:实际的Read和Write速率
第三行:参数如下:
  线程ID(按p切换为进程ID)
  优先级
  用户
  磁盘读速率
  磁盘写速率
  swap交换百分比
  IO等待所占的百分比
  线程/进程命令

Linux操作系统的进程管理

3>.iotop常用参数

-o, 
  --only只显示正在产生I/O的进程或线程,除了传参,可以在运行过程中按o生效
-b,
  --batch非交互模式,一般用来记录日志
-n NUM,
  --iter=NUM设置监测的次数,默认无限。在非交互模式下很有用
-d SEC,
  --delay=SEC设置每次监测的间隔,默认1秒,接受非整形数据例如1.
-p PID,
  --pid=PID指定监测的进程/线程
-u USER,
  --user=USER指定监测某个用户产生的I/O
-P,
  --processes仅显示进程,默认iotop显示所有线程
-a,
  --accumulated显示累积的I/O,而不是带宽
-k,
  --kilobytes使用kB单位,而不是对人友好的单位。在非交互模式下,脚本编程有用
-t,
  --time 加上时间戳,非交互非模式
-q,
  --quiet 禁止头几行,非交互模式,有三种指定方式
  -q 只在第一次监测时显示列名
  -qq 永远不显示列名
  -qqq 永远不显示I/O汇总

4>.交互按键

left和right方向键:
  改变排序
r:
  反向排序
o:
  切换至选项--only
p:
  切换至--processes选项
a:
  切换至--accumulated选项
q:
  退出
i:
  改变线程的优先级