centos

sys.14.2 linux进程和作业管理(pstree,ps,pgrep,pkill,pidof)

 

Linux系统上的进程查看及管理工具:
pstree , ps , pgrep , pkill , pidof ;
uptime , top ;
htop , vmstat , pmap , glances ;
dstat ;
kill , killall ;
job , bg , fg , nohup , nice , renice , …;

init程序启动后的进程名称及工作原理效率,对于centos系列的不同版本而不同:
centos 5 : SysV init , 在系统启动和引导时,其创建的各个子进程是通过脚本的方式,借助于shell来完成的,执行速度非常慢;
centos 6 : upstart;
centos 7 : Systemd;

关键词:pstree,ps,pgrep,pkill,pidof;

1、pstree 命令

pstree – display  a  tree  of  processes

[root@kouyuushinn ~]# pstree
systemd─┬─NetworkManager───2*[{NetworkManager}]
        ├─atd
        ├─auditd───{auditd}
        ├─crond
        ├─dbus-daemon
        ├─login───bash
        ├─lvmetad
        ├─master─┬─cleanup
        │        ├─2*[local]
        │        ├─pickup
        │        ├─qmgr
        │        └─trivial-rewrite
        ├─polkitd───5*[{polkitd}]
        ├─rsyslogd───2*[{rsyslogd}]
        ├─sshd───sshd───bash───pstree
        ├─systemd-journal
        ├─systemd-logind
        ├─systemd-udevd
        └─tuned───4*[{tuned}]
[root@kouyuushinn ~]#

2、ps命令
‘ps’命令执行后查看的的信息是由’/proc/’提供的;
/proc/ : 记录内核中的状态信息;

内核参数:可设置其值从而调整内核运行特性的参数;’/proc/sys’
状态变量,用于输出内核中的统计信息或状态信息,仅用于查看;

参数:模拟成文件系统类型;

进程:/proc/# , ‘#’表示PID;
/proc/mem : 保存的是内存的操作信息;

命令说明:
ps – report  a  snapshot  of  the  current  processes.

命令用法:
ps  [options]

OPTIONS的3种风格
1     UNIX  options, which  may  be  grouped  and  must  be  preceded  by  a  dash.     ‘-‘
2     BSD  options, which  may  be  grouped  and  must  not  be  used   with  a  dash.
3     GNU  long  options, which  are  preceded  by  two  dashes.     ‘–‘

启动进程的方式:
系统启动过程中自动启动,与终端无关的进程;
用户通过终端启动,与终端相关的进程;

常用OPTIONS:

a :  所有与终端相关的进程;
x :  所有与终端无关的进程;
u :  以用户为中心组织进程状态信息显示;
f :  以层级结构显示进程的相关信息,带有明显的符号表示,’\_’;

常用组合一:’aux’; 以PID号为顺序进行升序排列,数字从小到大,显示以用户为中心的所有进程;

-e :  以UNIX风格显示所有进程;
-f :  显示完整格式的进程信息;

常用组合二:’-ef’; 以UNIX风格,完整格式显示所有进程的信息;

-F :  显示完整格式的进程信息;比’-f’多了3个字段(SZ ,RSS ,PSR)
-H :  以层级结构显示进程的相关信息,下层结构带有空格缩进形式显示;

常用组合三:’-eFH’;

常用组合四:
-eo :  以UNIX风格显示;
axo :  以BSD风格显示;

o : 自定义要显示的字段列表,选项’o’后面接字段参数名称,多个字段名称用逗号’,’隔开;
常用的字段列表有:
—————–
user : 运行进程的用户;
pid :   进程的PID号;
ppid : 父进程号;

ni :  进程的nice值;
priority :  priority,进程的优先级;
rtprio :  real  time  priority,实时优先级;

psr :  进程运行在哪颗CPU上;
pcpu :  进程占用CPU的百分率;

tty :  与进程关联的终端;
stat :  进程的状态;
comm :  进程名称或运行进程的命令;

3、pgrep , pkill 命令:

命令说明:根据名称和其他属性查找或显示进程;
pgrep, pkill – look  up  or  signal  processes  based  on  name  and  other  attributes

命令用法:
pgrep  [options]  pattern
pkill  [options]  pattern

常用选项:
‘pgrep’后面跟进程名字(可以是完整进程名,也可做模糊匹配),表示查找指定进程的PID号;

-u  uid :   effective user,简称euser;指定用户名;’uid’标识用户名;
-U  uid :   real user,简称ruser;指定用户名;
‘euser’与’ruser’的区别:假如一个程序文件设置了SUID,普通用户A去访问这个程序,并且运行为一个进程,’ruser’就是此普通用户A,’euser’是此程序或者进程的属主用户;

-P   pid : 显示此进程的子进程;’pid’标识进程的PID号;

4、pidof 命令
根据进程名查找对应的PID号;类似’pgrep 进程名’;

命令说明:通过进程名查找其对应的PID号;
pidof — find  the  process  ID  of  a  running  program

‘ps  ef’显示信息中字段解释:

[root@kouyuushinn maps]# 
[root@kouyuushinn maps]# ps -ef
UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  0 May12 ?        00:00:39 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
root          2      0  0 May12 ?        00:00:00 [kthreadd]
root          3      2  0 May12 ?        00:00:03 [ksoftirqd/0]
root          5      2  0 May12 ?        00:00:00 [kworker/0:0H]
root          7      2  0 May12 ?        00:00:00 [migration/0]
root          8      2  0 May12 ?        00:00:00 [rcu_bh]

字段解释:
-----------------------
UID : 用户名称;
PID : 此进程的PID号;
PPID : 父进程的进程号;

C : CPU的占用百分比;

STIME : 进程启动时间和日期;
TTY : 与进程关联的终端;

TIME : 进程使用的总CPU时间;
CMD : 启用此进程的命令;

‘ps   -eF’显示信息中字段解释:

[root@kouyuushinn ~]# 
[root@kouyuushinn ~]# ps -eF |more
UID         PID   PPID  C    SZ   RSS PSR STIME TTY          TIME CMD
root          1      0  0 31331  3484   0 May12 ?        00:00:44 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
root          2      0  0     0     0   0 May12 ?        00:00:00 [kthreadd]
root          3      2  0     0     0   0 May12 ?        00:00:04 [ksoftirqd/0]
root          5      2  0     0     0   0 May12 ?        00:00:00 [kworker/0:0H]
root          7      2  0     0     0   0 May12 ?        00:00:00 [migration/0]
root          8      2  0     0     0   0 May12 ?        00:00:00 [rcu_bh]
root          9      2  0     0     0   0 May12 ?        00:00:11 [rcu_sched]
root         10      2  0     0     0   0 May12 ?        00:00:07 [watchdog/0]
root         12      2  0     0     0   0 May12 ?        00:00:00 [kdevtmpfs]
root         13      2  0     0     0   0 May12 ?        00:00:00 [netns]

字段解释:
-----------
UID : 用户名称;
PID : 此进程的PID号;
PPID : 父进程的进程号;

C : cpu utilization,CPU的占用百分比;

SZ : 占用的虚拟内存集;表示占用虚拟内存的大小;
RSS : 常驻内存集,Resident Size;

PSR : 多核CPU环境下,显示进程运行在哪颗CPU上;

STIME : 进程启动时间和日期;
TTY : 与进程关联的终端;

TIME : 进程使用的总CPU时间;
CMD : 启用此进程的命令;

‘ps’命令的常用组合中,格式与字段显示比较,第一组:

'ps aux':
----------
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root          1  0.0  0.3 125324  3484 ?        Ss   May12   0:44 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
root          2  0.0  0.0      0     0 ?        S    May12   0:00 [kthreadd]
root          3  0.0  0.0      0     0 ?        S    May12   0:04 [ksoftirqd/0]
root          5  0.0  0.0      0     0 ?        S<   May12   0:00 [kworker/0:0H]
root          7  0.0  0.0      0     0 ?        S    May12   0:00 [migration/0]

'ps -ef':
----------
UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  0 May12 ?        00:00:44 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
root          2      0  0 May12 ?        00:00:00 [kthreadd]
root          3      2  0 May12 ?        00:00:04 [ksoftirqd/0]
root          5      2  0 May12 ?        00:00:00 [kworker/0:0H]
root          7      2  0 May12 ?        00:00:00 [migration/0]

'ps -eF':
----------
UID         PID   PPID  C    SZ   RSS PSR STIME TTY          TIME CMD
root          1      0  0 31331  3484   0 May12 ?        00:00:44 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
root          2      0  0     0     0   0 May12 ?        00:00:00 [kthreadd]
root          3      2  0     0     0   0 May12 ?        00:00:04 [ksoftirqd/0]
root          5      2  0     0     0   0 May12 ?        00:00:00 [kworker/0:0H]
root          7      2  0     0     0   0 May12 ?        00:00:00 [migration/0]

'ps -eFH':
----------
UID         PID   PPID  C    SZ   RSS PSR STIME TTY          TIME CMD
root          2      0  0     0     0   0 May12 ?        00:00:00 [kthreadd]
root          3      2  0     0     0   0 May12 ?        00:00:04   [ksoftirqd/0]
root          5      2  0     0     0   0 May12 ?        00:00:00   [kworker/0:0H]
root          7      2  0     0     0   0 May12 ?        00:00:00   [migration/0]
root          8      2  0     0     0   0 May12 ?        00:00:00   [rcu_bh]

格式与字段显示比较,第二组(层级显示):

'ps auxf':
----------
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root          2  0.0  0.0      0     0 ?        S    May12   0:00 [kthreadd]
root          3  0.0  0.0      0     0 ?        S    May12   0:04  \_ [ksoftirqd/0]
root          5  0.0  0.0      0     0 ?        S<   May12   0:00  \_ [kworker/0:0H]
root          7  0.0  0.0      0     0 ?        S    May12   0:00  \_ [migration/0]
root          8  0.0  0.0      0     0 ?        S    May12   0:00  \_ [rcu_bh]
root          9  0.0  0.0      0     0 ?        R    May12   0:11  \_ [rcu_sched]
root         10  0.0  0.0      0     0 ?        S    May12   0:07  \_ [watchdog/0]
root         12  0.0  0.0      0     0 ?        S    May12   0:00  \_ [kdevtmpfs]
root         13  0.0  0.0      0     0 ?        S<   May12   0:00  \_ [netns]
root         14  0.0  0.0      0     0 ?        S    May12   0:00  \_ [khungtaskd]
root         15  0.0  0.0      0     0 ?        S<   May12   0:00  \_ [writeback]

'ps -eFH':
----------
UID         PID   PPID  C    SZ   RSS PSR STIME TTY          TIME CMD
root          2      0  0     0     0   0 May12 ?        00:00:00 [kthreadd]
root          3      2  0     0     0   0 May12 ?        00:00:04   [ksoftirqd/0]
root          5      2  0     0     0   0 May12 ?        00:00:00   [kworker/0:0H]
root          7      2  0     0     0   0 May12 ?        00:00:00   [migration/0]
root          8      2  0     0     0   0 May12 ?        00:00:00   [rcu_bh]
root          9      2  0     0     0   0 May12 ?        00:00:11   [rcu_sched]
root         10      2  0     0     0   0 May12 ?        00:00:07   [watchdog/0]
root         12      2  0     0     0   0 May12 ?        00:00:00   [kdevtmpfs]
root         13      2  0     0     0   0 May12 ?        00:00:00   [netns]
root         14      2  0     0     0   0 May12 ?        00:00:00   [khungtaskd]
root         15      2  0     0     0   0 May12 ?        00:00:00   [writeback]

通过’-o/o’选项,比较信息显示格式,第三组:
指定相同的字段列表: ‘user, pid, ppid, ni, priority, pcpu, psr, tty, comm’

'ps axo:'
----------
USER        PID   PPID  NI PRI %CPU PSR TT       COMMAND
root          1      0   0  20  0.0   0 ?        systemd
root          2      0   0  20  0.0   0 ?        kthreadd
root          3      2   0  20  0.0   0 ?        ksoftirqd/0
root          5      2 -20   0  0.0   0 ?        kworker/0:0H
root          7      2   - -100 0.0   0 ?        migration/0
root          8      2   0  20  0.0   0 ?        rcu_bh
root          9      2   0  20  0.0   0 ?        rcu_sched
root         10      2   - -100 0.0   0 ?        watchdog/0
root         12      2   0  20  0.0   0 ?        kdevtmpfs
root         13      2 -20   0  0.0   0 ?        netns
root         14      2   0  20  0.0   0 ?        khungtaskd

'ps -eo:'
----------
USER        PID   PPID  NI PRI %CPU PSR TT       COMMAND
root          1      0   0  20  0.0   0 ?        systemd
root          2      0   0  20  0.0   0 ?        kthreadd
root          3      2   0  20  0.0   0 ?        ksoftirqd/0
root          5      2 -20   0  0.0   0 ?        kworker/0:0H
root          7      2   - -100 0.0   0 ?        migration/0
root          8      2   0  20  0.0   0 ?        rcu_bh
root          9      2   0  20  0.0   0 ?        rcu_sched
root         10      2   - -100 0.0   0 ?        watchdog/0
root         12      2   0  20  0.0   0 ?        kdevtmpfs
root         13      2 -20   0  0.0   0 ?        netns
root         14      2   0  20  0.0   0 ?        khungtaskd


'ps axfo:'
----------
USER        PID   PPID  NI PRI %CPU PSR TT       COMMAND
root          2      0   0  20  0.0   0 ?        kthreadd
root          3      2   0  20  0.0   0 ?         \_ ksoftirqd/0
root          5      2 -20   0  0.0   0 ?         \_ kworker/0:0H
root          7      2   - -100 0.0   0 ?         \_ migration/0
root          8      2   0  20  0.0   0 ?         \_ rcu_bh
root          9      2   0  20  0.0   0 ?         \_ rcu_sched
root         10      2   - -100 0.0   0 ?         \_ watchdog/0
root         12      2   0  20  0.0   0 ?         \_ kdevtmpfs
root         13      2 -20   0  0.0   0 ?         \_ netns
root         14      2   0  20  0.0   0 ?         \_ khungtaskd
root         15      2 -20   0  0.0   0 ?         \_ writeback

'ps -eHo:'
----------
USER        PID   PPID  NI PRI %CPU PSR TT       COMMAND
root          2      0   0  20  0.0   0 ?        kthreadd
root          3      2   0  20  0.0   0 ?          ksoftirqd/0
root          5      2 -20   0  0.0   0 ?          kworker/0:0H
root          7      2   - -100 0.0   0 ?          migration/0
root          8      2   0  20  0.0   0 ?          rcu_bh
root          9      2   0  20  0.0   0 ?          rcu_sched
root         10      2   - -100 0.0   0 ?          watchdog/0
root         12      2   0  20  0.0   0 ?          kdevtmpfs
root         13      2 -20   0  0.0   0 ?          netns
root         14      2   0  20  0.0   0 ?          khungtaskd
root         15      2 -20   0  0.0   0 ?          writeback

‘pgrep’命令,第四组:

'pgrep -U root -l':
----------
1 systemd
2 kthreadd
3 ksoftirqd/0
5 kworker/0:0H
7 migration/0
8 rcu_bh

'pgrep -U root -a':
----------
1 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
2 kthreadd
3 ksoftirqd/0
5 kworker/0:0H
7 migration/0
8 rcu_bh

‘pgrep’ 可以根据进程名字做模糊匹配来查找对应的PID号:

[root@kouyuushinn ~]# pgrep nginx
17555
30401
[root@kouyuushinn ~]# pgrep mysql
10377
[root@kouyuushinn ~]#
[root@kouyuushinn ~]# pgrep sshd
882
14644
14646
[root@kouyuushinn ~]# pgrep php-fpm
1902
10573
17502
17503
17504
17505
17506
17507
19179
25641
29867
29881
30236
30240
31399
[root@kouyuushinn ~]#

GNU方式显示所有与终端相关的进程:
-------------------------------
[root@kouyuushinn maps]# 
[root@kouyuushinn maps]# ps a
   PID TTY      STAT   TIME COMMAND
  1078 tty1     Ss+    0:01 -bash
 75899 pts/0    Ss     0:00 -bash
 75921 pts/1    Ss     0:00 -bash
 75965 pts/1    S+     0:00 man ps
 75976 pts/1    S+     0:00 less -s
 76111 pts/0    R+     0:00 ps a
[root@kouyuushinn maps]# 


UNIX方式显示所有与终端相关的进程:
-------------------------------
[root@kouyuushinn maps]# ps -a
   PID TTY          TIME CMD
 75965 pts/1    00:00:00 man
 75976 pts/1    00:00:00 less
 76112 pts/0    00:00:00 ps
[root@kouyuushinn maps]# 


查看系统内所有进程:
-----------------------

 

Leave a Reply

Your email address will not be published. Required fields are marked *