码迷,mamicode.com
首页 > 系统相关 > 详细

linux进程管理杂记(1)

时间:2018-03-27 01:50:49      阅读:278      评论:0      收藏:0      [点我收藏+]

标签:ps 进程管理 linux 杂记 基础

进程命令

pstree

CentOS7显示进程树

[root@CentOS7 tmp]# pstree
systemd─┬─NetworkManager─┬─dhclient
        │                └─2*[{NetworkManager}]
        ├─abrt-watch-log
        ├─abrtd
        ├─atd
        ├─auditd───{auditd}
        ├─crond
        ├─dbus-daemon───{dbus-daemon}
        ├─dhclient
        ├─firewalld───{firewalld}
        ├─login───bash
        ├─lsmd
        ├─lvmetad
        ├─master─┬─pickup
        │        └─qmgr
        ├─polkitd───5*[{polkitd}]
        ├─rsyslogd───2*[{rsyslogd}]
        ├─smartd
        ├─sshd─┬─sshd───bash───pstree
        │      └─sshd───bash───man───less
        ├─systemd-journal
        ├─systemd-logind
        ├─systemd-udevd
        ├─tuned───4*[{tuned}]
        └─wpa_supplicant

CentOS7显示进程树

[root@zz ~]# pstree
init─┬─abrtd
     ├─acpid
     ├─atd
     ├─auditd───{auditd}
     ├─automount───4*[{automount}]
     ├─certmonger
     ├─console-kit-dae───63*[{console-kit-da}]
     ├─crond
     ├─cupsd
     ├─dbus-daemon
     ├─dhclient
     ├─hald─┬─hald-runner─┬─hald-addon-acpi
     │      │             ├─hald-addon-inpu
     │      │             └─hald-addon-rfki
     │      └─{hald}
     ├─httpd───8*[httpd]
     ├─login───bash
     ├─master─┬─pickup
     │        └─qmgr
     ├─mcelog
     ├─5*[mingetty]
     ├─mysqld_safe───mysqld───9*[{mysqld}]
     ├─rpc.statd
     ├─rpcbind
     ├─rsyslogd───3*[{rsyslogd}]
     ├─sshd───sshd───bash───pstree
     ├─udevd───2*[udevd]
     └─vsftpd

ps

显示与终端有关的进程

[root@zz ~]# ps a
  PID TTY      STAT   TIME COMMAND
 1915 tty2     Ss+    0:00 /sbin/mingetty /dev/tty2
 1917 tty3     Ss+    0:00 /sbin/mingetty /dev/tty3
 1920 tty4     Ss+    0:00 /sbin/mingetty /dev/tty4
 1926 tty5     Ss+    0:00 /sbin/mingetty /dev/tty5
 1935 tty6     Ss+    0:00 /sbin/mingetty /dev/tty6
 2017 tty1     Ss+    0:01 -bash
15342 pts/0    Ss     0:00 -bash
15474 pts/0    R+     0:00 ps a

显示与终端无关的进程

[root@zz ~]# ps x
  PID TTY      STAT   TIME COMMAND
    1 ?        Ss     0:18 /sbin/init
    2 ?        S      0:00 [kthreadd]
    3 ?        S      0:00 [migration/0]
    4 ?        S      1:40 [ksoftirqd/0]
    5 ?        S      0:00 [stopper/0]
    6 ?        S      0:34 [watchdog/0]
    7 ?        S     10:59 [events/0]
    8 ?        S      0:00 [events/0]
    9 ?        S      0:00 [events_long/0]
   10 ?        S      0:00 [events_power_ef]
   11 ?        S      0:00 [cgroup]
   12 ?        S      0:00 [khelper]
   13 ?        S      0:00 [netns]
   14 ?        S      0:00 [async/mgr]
   15 ?        S      0:00 [pm]
   16 ?        S      0:10 [sync_supers]
   17 ?        S      0:13 [bdi-default]
   18 ?        S      0:00 [kintegrityd/0]
   19 ?        S      0:16 [kblockd/0]
   20 ?        S      0:00 [kacpid]
   21 ?        S      0:00 [kacpi_notify]
   22 ?        S      0:00 [kacpi_hotplug]
   23 ?        S      0:00 [ata_aux]
   24 ?        S      0:00 [ata_sff/0]
   25 ?        S      0:00 [ksuspend_usbd]
   26 ?        S      0:07 [khubd]
   27 ?        S      0:00 [kseriod]
   28 ?        S      0:00 [md/0]
   29 ?        S      0:00 [md_misc/0]
   30 ?        S      0:00 [linkwatch]
   33 ?        S      0:01 [khungtaskd]
   34 ?        S      0:17 [kswapd0]
   35 ?        SN     0:00 [ksmd]
   36 ?        S      0:00 [aio/0]
   37 ?        S      0:00 [crypto/0]
   44 ?        S      0:00 [kthrotld/0]
   45 ?        S      0:00 [pciehpd]
   47 ?        S      0:00 [kpsmoused]
   48 ?        S      0:00 [usbhid_resumer]
   49 ?        S      0:00 [deferwq]
   81 ?        S      0:00 [kdmremove]
   82 ?        S      0:00 [kstriped]
  113 ?        S      0:00 [ttm_swap]
  242 ?        S      0:00 [scsi_eh_0]
  246 ?        S      0:00 [scsi_eh_1]
  266 ?        S      1:08 [mpt_poll_0]
  267 ?        S      0:00 [mpt/0]
  268 ?        S      0:00 [scsi_eh_2]
  363 ?        S      0:12 [flush-8:0]
  416 ?        S      0:02 [jbd2/sda5-8]
  417 ?        S      0:00 [ext4-dio-unwrit]
  509 ?        S<s    0:02 /sbin/udevd -d
  645 ?        S      0:54 [vmmemctl]
  802 ?        S      0:00 [bluetooth]
  869 ?        S      0:00 [jbd2/sda1-8]
  870 ?        S      0:00 [ext4-dio-unwrit]
  871 ?        S      0:00 [jbd2/sda7-8]
  872 ?        S      0:00 [ext4-dio-unwrit]
  873 ?        S      0:00 [jbd2/sda8-8]
  874 ?        S      0:00 [ext4-dio-unwrit]
  875 ?        S      0:01 [jbd2/sda6-8]
  876 ?        S      0:00 [ext4-dio-unwrit]
  877 ?        S      0:00 [jbd2/sda3-8]
  878 ?        S      0:00 [ext4-dio-unwrit]
  879 ?        S      0:19 [jbd2/sda2-8]
  880 ?        S      0:00 [ext4-dio-unwrit]
  923 ?        S      0:00 [kauditd]
 1207 ?        S<sl   0:03 auditd
 1241 ?        Sl     0:03 /sbin/rsyslogd -i /var/run/syslogd.pi
 1369 ?        Ss     0:00 cupsd -C /etc/cups/cupsd.conf
 1401 ?        Ss     0:00 /usr/sbin/acpid
 1414 ?        S      0:00 hald-runner
 1446 ?        S      0:00 hald-addon-input: Listening on /dev/i
 1488 ?        Ssl    0:17 automount --pid-file /var/run/autofs.
 1524 ?        Ss     0:00 /usr/sbin/mcelog --daemon
 1541 ?        Ss     0:01 /usr/sbin/sshd
 1578 ?        S      0:00 /bin/sh /usr/bin/mysqld_safe --datadi
 1799 ?        Ss     0:09 /usr/libexec/postfix/master
 1828 ?        Ss     0:00 /usr/sbin/abrtd
 1839 ?        Ss     1:29 /usr/sbin/httpd
 1851 ?        Ss     0:06 crond
 1866 ?        Ss     0:00 /usr/sbin/atd
 1882 ?        Ss     0:01 /usr/sbin/certmonger -S -p /var/run/c
 1912 ?        Ss     0:00 login -- root     
 1915 tty2     Ss+    0:00 /sbin/mingetty /dev/tty2
 1917 tty3     Ss+    0:00 /sbin/mingetty /dev/tty3
 1920 tty4     Ss+    0:00 /sbin/mingetty /dev/tty4
 1926 tty5     Ss+    0:00 /sbin/mingetty /dev/tty5
 1935 tty6     Ss+    0:00 /sbin/mingetty /dev/tty6
 1950 ?        Sl     0:00 /usr/sbin/console-kit-daemon --no-dae
 2017 tty1     Ss+    0:01 -bash
 6563 ?        Ss     0:04 dhclient
12320 ?        S<     0:02 /sbin/udevd -d
12321 ?        S<     0:00 /sbin/udevd -d
13048 ?        S      0:00 /usr/libexec/hald-addon-rfkill-killsw
13492 ?        Ss     0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.c
15340 ?        Rs     0:02 sshd: root@pts/0 
15342 pts/0    Ss     0:00 -bash
15475 pts/0    R+     0:00 ps x

以用户为中心显示所有进程

[root@zz ~]# ps aux

显示完整格式的所有进程

[root@zz ~]# ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 Jun15 ?        00:00:18 /sbin/init
root         2     0  0 Jun15 ?        00:00:00 [kthreadd]
root         3     2  0 Jun15 ?        00:00:00 [migration/0]
root         4     2  0 Jun15 ?        00:01:41 [ksoftirqd/0]
root         5     2  0 Jun15 ?        00:00:00 [stopper/0]

显示所有任务格式的进程

[root@zz ~]# ps -ej
  PID  PGID   SID TTY          TIME CMD
    1     1     1 ?        00:00:18 init
    2     0     0 ?        00:00:00 kthreadd
    3     0     0 ?        00:00:00 migration/0
    4     0     0 ?        00:01:41 ksoftirqd/0
    5     0     0 ?        00:00:00 stopper/0
    6     0     0 ?        00:00:35 watchdog/0
    7     0     0 ?        00:12:17 events/0
    8     0     0 ?        00:00:00 events/0

树状显示任务格式的所有进程

[root@zz ~]# ps -ejH
  PID  PGID   SID TTY          TIME CMD
    2     0     0 ?        00:00:00 kthreadd
    3     0     0 ?        00:00:00   migration/0
    4     0     0 ?        00:01:41   ksoftirqd/0
    5     0     0 ?        00:00:00   stopper/0
    6     0     0 ?        00:00:35   watchdog/0
    7     0     0 ?        00:12:17   events/0
    8     0     0 ?        00:00:00   events/0
    9     0     0 ?        00:00:00   events_long/0
   10     0     0 ?        00:00:00   events_power_ef
   11     0     0 ?        00:00:00   cgroup
   12     0     0 ?        00:00:00   khelper
   13     0     0 ?        00:00:00   netns
   14     0     0 ?        00:00:00   async/mgr
   15     0     0 ?        00:00:00   pm
   16     0     0 ?        00:00:10   sync_supers
   17     0     0 ?        00:00:14   bdi-default
   18     0     0 ?        00:00:00   kintegrityd/0
   19     0     0 ?        00:00:16   kblockd/0
   20     0     0 ?        00:00:00   kacpid
   21     0     0 ?        00:00:00   kacpi_notify
   22     0     0 ?        00:00:00   kacpi_hotplug
   23     0     0 ?        00:00:00   ata_aux
   24     0     0 ?        00:00:00   ata_sff/0
   25     0     0 ?        00:00:00   ksuspend_usbd
   26     0     0 ?        00:00:07   khubd
   27     0     0 ?        00:00:00   kseriod
   28     0     0 ?        00:00:00   md/0
   29     0     0 ?        00:00:00   md_misc/0
   30     0     0 ?        00:00:00   linkwatch
   33     0     0 ?        00:00:01   khungtaskd
   34     0     0 ?        00:00:17   kswapd0
   35     0     0 ?        00:00:00   ksmd
   36     0     0 ?        00:00:00   aio/0
   37     0     0 ?        00:00:00   crypto/0
   44     0     0 ?        00:00:00   kthrotld/0
   45     0     0 ?        00:00:00   pciehpd
   47     0     0 ?        00:00:00   kpsmoused
   48     0     0 ?        00:00:00   usbhid_resumer
   49     0     0 ?        00:00:00   deferwq
   81     0     0 ?        00:00:00   kdmremove
   82     0     0 ?        00:00:00   kstriped
  113     0     0 ?        00:00:00   ttm_swap
  242     0     0 ?        00:00:00   scsi_eh_0
  246     0     0 ?        00:00:00   scsi_eh_1
  266     0     0 ?        00:01:12   mpt_poll_0
  267     0     0 ?        00:00:00   mpt/0
  268     0     0 ?        00:00:00   scsi_eh_2
  363     0     0 ?        00:00:13   flush-8:0
  416     0     0 ?        00:00:02   jbd2/sda5-8
  417     0     0 ?        00:00:00   ext4-dio-unwrit
  645     0     0 ?        00:00:58   vmmemctl
  802     0     0 ?        00:00:00   bluetooth
  869     0     0 ?        00:00:00   jbd2/sda1-8
  870     0     0 ?        00:00:00   ext4-dio-unwrit
  871     0     0 ?        00:00:00   jbd2/sda7-8
  872     0     0 ?        00:00:00   ext4-dio-unwrit
  873     0     0 ?        00:00:00   jbd2/sda8-8
  874     0     0 ?        00:00:00   ext4-dio-unwrit
  875     0     0 ?        00:00:01   jbd2/sda6-8
  876     0     0 ?        00:00:00   ext4-dio-unwrit
  877     0     0 ?        00:00:00   jbd2/sda3-8
  878     0     0 ?        00:00:00   ext4-dio-unwrit
  879     0     0 ?        00:00:21   jbd2/sda2-8
  880     0     0 ?        00:00:00   ext4-dio-unwrit
  923     0     0 ?        00:00:00   kauditd
    1     1     1 ?        00:00:18 init
  509   509   509 ?        00:00:02   udevd
12320   509   509 ?        00:00:02     udevd
12321   509   509 ?        00:00:00     udevd
 1207  1207  1207 ?        00:00:03   auditd
 1241  1235   924 ?        00:00:03   rsyslogd
 1292  1292  1292 ?        00:00:03   rpcbind
 1314  1314  1314 ?        00:00:00   rpc.statd
 1348  1348  1348 ?        00:00:01   dbus-daemon
 1369  1369  1369 ?        00:00:00   cupsd
 1401  1401  1401 ?        00:00:00   acpid
 1413  1413  1413 ?        00:00:10   hald
 1414  1413  1413 ?        00:00:00     hald-runner
 1446  1413  1413 ?        00:00:00       hald-addon-inpu
 1464  1413  1413 ?        00:00:00       hald-addon-acpi
13048  1413  1413 ?        00:00:00       hald-addon-rfki
 1488  1488  1488 ?        00:00:19   automount
 1524  1524  1524 ?        00:00:00   mcelog
 1541  1541  1541 ?        00:00:01   sshd
15340 15340 15340 ?        00:00:03     sshd
15342 15342 15342 pts/0    00:00:00       bash
16685 16685 15342 pts/0    00:00:00         ps
 1578  1546   924 ?        00:00:00   mysqld_safe
 1682  1546   924 ?        00:04:16     mysqld
 1799  1799  1799 ?        00:00:10   master
 1810  1799  1799 ?        00:00:01     qmgr
16656  1799  1799 ?        00:00:00     pickup
 1828  1828  1828 ?        00:00:00   abrtd
 1839  1839  1839 ?        00:01:34   httpd
 1939  1839  1839 ?        00:00:00     httpd
 1940  1839  1839 ?        00:00:00     httpd
 1941  1839  1839 ?        00:00:00     httpd
 1942  1839  1839 ?        00:00:00     httpd
 1943  1839  1839 ?        00:00:00     httpd
 1944  1839  1839 ?        00:00:00     httpd
 1945  1839  1839 ?        00:00:00     httpd
 1946  1839  1839 ?        00:00:00     httpd
 1851  1851  1851 ?        00:00:06   crond
 1866  1866  1866 ?        00:00:00   atd
 1882  1882  1882 ?        00:00:01   certmonger
 1912  1912  1912 ?        00:00:00   login
 2017  2017  2017 tty1     00:00:01     bash
 1915  1915  1915 tty2     00:00:00   mingetty

以进程层级格式显示进程相关信息

[root@zz ~]# ps -eFH | head
UID        PID  PPID  C    SZ   RSS PSR STIME TTY          TIME CMD
root         2     0  0     0     0   0 Jun15 ?        00:00:00 [kthreadd]
root         3     2  0     0     0   0 Jun15 ?        00:00:00   [migration/0]
root         4     2  0     0     0   0 Jun15 ?        00:01:41   [ksoftirqd/0]
root         5     2  0     0     0   0 Jun15 ?        00:00:00   [stopper/0]
root         6     2  0     0     0   0 Jun15 ?        00:00:35   [watchdog/0]

查看selinux进程信息

[root@qq ~]# ps axZ | head
LABEL                             PID TTY      STAT   TIME COMMAND
system_u:system_r:init_t:s0         1 ?        Ss     0:58 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
system_u:system_r:kernel_t:s0       2 ?        S      0:00 [kthreadd]
system_u:system_r:kernel_t:s0       3 ?        S      0:04 [ksoftirqd/0]
system_u:system_r:kernel_t:s0       7 ?        S      0:00 [migration/0]
system_u:system_r:kernel_t:s0       8 ?        S      0:00 [rcu_bh]
system_u:system_r:kernel_t:s0       9 ?        S      0:00 [rcuob/0]
system_u:system_r:kernel_t:s0      10 ?        S      0:00 [rcuob/1]
system_u:system_r:kernel_t:s0      11 ?        S      0:00 [rcuob/2]
system_u:system_r:kernel_t:s0      12 ?        S      0:00 [rcuob/3]

只显示root的进程信息

[root@qq ~]# ps -U root | head
  PID TTY          TIME CMD
    1 ?        00:00:58 systemd
    2 ?        00:00:00 kthreadd
    3 ?        00:00:04 ksoftirqd/0
    7 ?        00:00:00 migration/0
    8 ?        00:00:00 rcu_bh
    9 ?        00:00:00 rcuob/0
   10 ?        00:00:00 rcuob/1
   11 ?        00:00:00 rcuob/2
   12 ?        00:00:00 rcuob/3

通过-o参数只显示相关列信息

[root@qq ~]# ps axo pid,psr,command | head
  PID PSR COMMAND
    1   0 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
    2   0 [kthreadd]
    3   0 [ksoftirqd/0]
    7   0 [migration/0]
    8   0 [rcu_bh]
    9   0 [rcuob/0]
   10   0 [rcuob/1]
   11   0 [rcuob/2]
   12   0 [rcuob/3]

pgrep和pkill

pgrep只显示进程号

[root@qq ~]# pgrep -u root | head
1
2
3
7
8
9
10
11
12
13

pgrep只显示进程号和进程名

[root@qq ~]# pgrep -u root -l | head -20
1 systemd
2 kthreadd
3 ksoftirqd/0
7 migration/0
8 rcu_bh
9 rcuob/0
10 rcuob/1
11 rcuob/2
12 rcuob/3
13 rcuob/4
14 rcuob/5
15 rcuob/6
16 rcuob/7
17 rcu_sched
18 rcuos/0
19 rcuos/1
20 rcuos/2
21 rcuos/3
22 rcuos/4
23 rcuos/5

pgrep只显示进程号的进程全名,包括使用的参数

[root@qq ~]# pgrep -u root -a 
1 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
2 kthreadd
3 ksoftirqd/0
7 migration/0
8 rcu_bh
9 rcuob/0
10 rcuob/1
11 rcuob/2
12 rcuob/3
13 rcuob/4
14 rcuob/5
15 rcuob/6
16 rcuob/7
17 rcu_sched
18 rcuos/0
19 rcuos/1
20 rcuos/2
21 rcuos/3
22 rcuos/4
23 rcuos/5
24 rcuos/6
25 rcuos/7
26 watchdog/0
27 khelper
28 kdevtmpfs
29 netns
30 perf
31 writeback
32 kintegrityd
33 bioset
34 kblockd
35 md
40 khungtaskd
41 kswapd0
42 ksmd
43 fsnotify_mark
44 crypto
52 kthrotld
54 kmpath_rdacd
55 kpsmoused
57 ipv6_addrconf
77 deferwq
108 kauditd
244 ata_sff
245 scsi_eh_0
246 scsi_tmf_0
247 scsi_eh_1
248 scsi_tmf_1
252 mpt_poll_0
253 mpt/0
255 events_power_ef
261 scsi_eh_2
262 scsi_tmf_2
264 ttm_swap
283 xfsalloc
284 xfs_mru_cache
285 xfs-buf/sda6
286 xfs-data/sda6
287 xfs-conv/sda6
288 xfs-cil/sda6
289 xfsaild/sda6
311 xfs-buf/sda5
312 xfs-data/sda5
313 xfs-conv/sda5
314 xfs-cil/sda5
315 xfsaild/sda5
373 /usr/lib/systemd/systemd-journald
387 /usr/sbin/lvmetad -f
401 /usr/lib/systemd/systemd-udevd

pidof

根据进程名获取pid
[root@qq ~]# pidof sshd
11127 1184

top

top

P:以占率的CPU百分比:
M:占率内存百分比;
T:累积占率CPU时常

top修改刷新时间间隔

top - 05:51:44 up 19:27,  2 users,  load average: 0.00, 0.01, 0
Tasks: 132 total,   2 running, 130 sleeping,   0 stopped,   0 z
%Cpu(s):  0.0 us,  1.0 sy,  0.0 ni, 99.0 id,  0.0 wa,  0.0 hi, 
KiB Mem :   500148 total,    12888 free,   141488 used,   34577
KiB Swap:  4194300 total,  4193364 free,      936 used.   30878
Change delay from 1.0 to 
  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM 
    1 root      20   0  126560   7384   2632 S  0.0  1.5 
    2 root      20   0       0      0      0 S  0.0  0.0 
    3 root      20   0       0      0      0 S  0.0  0.0 
    7 root      rt   0       0      0      0 S  0.0  0.0 
    8 root      20   0       0      0      0 S  0.0  0.0 
    9 root      20   0       0      0      0 S  0.0 

通过q命令kill掉top命令中的进程

top - 05:52:43 up 19:28,  2 users,  load average: 0.00, 0.01, 0
Tasks: 132 total,   3 running, 129 sleeping,   0 stopped,   0 z
%Cpu(s):  0.3 us,  0.5 sy,  0.0 ni, 99.1 id,  0.1 wa,  0.0 hi, 
KiB Mem :   500148 total,    12776 free,   141600 used,   34577
KiB Swap:  4194300 total,  4193364 free,      936 used.   30867
PID to signal/kill [default pid = 11925] 
  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM 
11925 root      20   0  146160   1920   1360 R  6.2  0.4 
    1 root      20   0  126560   7384   2632 S  0.0  1.5 
    2 root      20   0       0      0      0 S  0.0  0.0

top每一秒刷新一次

[root@qq ~]# top -d 1

top从上到下批次显示所有进程

[root@qq ~]# top -b

top只显示3批进程后结束

[root@qq ~]# top -b -n 3

htop

linux进程管理杂记(1)

标签:ps 进程管理 linux 杂记 基础

原文地址:http://blog.51cto.com/zhongle21/2091430

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!