1-查看进程
2016.6.21
程序和进程的关系:
程序
- 保存在硬盘、光盘等介质中的可执行代码和数据
- 静态保存的代码
进程
- 在CPU及内存中运行的程序代码
- 动态执行的代码
- 父、子进程:每个进程可以创建一个或多个进程
静态查看进程统计信息ps:
- 命令ps aux
- 以简单列表的形式显示出进程信息
- a:显示当前终端下的所有进程信息,包括其他用户的进程
- u:使用以用户为主的格式输出进程信息
- x:显示当前用户在所有终端下的进程信息
- 命令ps -elf
- 以长格式显示系统中的进程信息,并且包含更丰富的内容
- -e:显示系统内的所有进程信息
- -l:使用长(Long)格式显示进程信息
- -f:使用完整的(Full)格式显示进程信息
- 直接执行不带任何选项的ps命令时,将值显示当前用户会话中打开的进程
- 由于系统中运行的进程数量较多,需要查询某一个进程的信息时可以结合管道操作和grep命令进行过滤。例如执行以下操作可以过滤出包含“bash”的进程信息
- ps aux | grep bash
[root@test1 ~]# ps x
PID TTY STAT TIME COMMAND
1 ? Ss 0:02 /sbin/init
2 ? S 0:00 [kthreadd]
3 ? S 0:00 [migration/0]
4 ? S 0:00 [ksoftirqd/0]
5 ? S 0:00 [migration/0]
6 ? S 0:00 [watchdog/0]
7 ? S 0:00 [migration/1]
8 ? S 0:00 [migration/1]
9 ? S 0:00 [ksoftirqd/1]
10 ? S 0:00 [watchdog/1]
11 ? S 0:01 [migration/2]
12 ? S 0:00 [migration/2]
13 ? S 0:00 [ksoftirqd/2]
14 ? S 0:00 [watchdog/2]
15 ? S 0:02 [migration/3]
16 ? S 0:00 [migration/3]
17 ? S 0:00 [ksoftirqd/3]
18 ? S 0:00 [watchdog/3]
19 ? S 0:02 [events/0]
20 ? S 0:22 [events/1]
21 ? S 0:02 [events/2]
22 ? R 0:09 [events/3]
23 ? S 0:00 [cgroup]
24 ? S 0:00 [khelper]
25 ? S 0:00 [netns]
26 ? S 0:00 [async/mgr]
27 ? S 0:00 [pm]
28 ? S 0:00 [sync_supers]
29 ? S 0:00 [bdi-default]
30 ? S 0:00 [kintegrityd/0]
31 ? S 0:00 [kintegrityd/1]
32 ? S 0:00 [kintegrityd/2]
33 ? S 0:00 [kintegrityd/3]
34 ? S 0:01 [kblockd/0]
35 ? S 0:00 [kblockd/1]
36 ? S 0:00 [kblockd/2]
37 ? S 0:00 [kblockd/3]
38 ? S 0:00 [kacpid]
39 ? S 0:00 [kacpi_notify]
40 ? S 0:00 [kacpi_hotplug]
41 ? S 0:00 [ata_aux]
42 ? S 0:00 [ata_sff/0]
43 ? S 0:00 [ata_sff/1]
44 ? S 0:00 [ata_sff/2]
45 ? S 0:00 [ata_sff/3]
46 ? S 0:00 [ksuspend_usbd]
47 ? S 0:00 [khubd]
48 ? S 0:00 [kseriod]
49 ? S 0:00 [md/0]
50 ? S 0:00 [md/1]
51 ? S 0:00 [md/2]
52 ? S 0:00 [md/3]
53 ? S 0:00 [md_misc/0]
54 ? S 0:00 [md_misc/1]
55 ? S 0:00 [md_misc/2]
56 ? S 0:00 [md_misc/3]
57 ? S 0:00 [linkwatch]
58 ? S 0:00 [khungtaskd]
59 ? S 0:00 [kswapd0]
60 ? SN 0:00 [ksmd]
61 ? SN 0:01 [khugepaged]
62 ? S 0:00 [aio/0]
63 ? S 0:00 [aio/1]
64 ? S 0:00 [aio/2]
65 ? S 0:00 [aio/3]
66 ? S 0:00 [crypto/0]
67 ? S 0:00 [crypto/1]
68 ? S 0:00 [crypto/2]
69 ? S 0:00 [crypto/3]
74 ? S 0:00 [kthrotld/0]
75 ? S 0:00 [kthrotld/1]
76 ? S 0:00 [kthrotld/2]
77 ? S 0:00 [kthrotld/3]
78 ? S 0:00 [pciehpd]
80 ? S 0:00 [kpsmoused]
81 ? S 0:00 [usbhid_resumer]
112 ? S 0:00 [kstriped]
256 ? S 0:00 [scsi_eh_0]
260 ? S 0:00 [scsi_eh_1]
280 ? S 0:01 [mpt_poll_0]
281 ? S 0:00 [mpt/0]
282 ? S 0:00 [scsi_eh_2]
347 ? S 0:00 [kdmflush]
349 ? S 0:00 [kdmflush]
369 ? S 0:02 [jbd2/dm-0-8]
370 ? S 0:00 [ext4-dio-unwrit]
457 ? S 0:04 [flush-253:0]
458 ? S<s 0:00 /sbin/udevd -d
691 ? S 0:56 [vmmemctl]
889 ? S 0:00 [bluetooth]
994 ? S< 0:00 /sbin/udevd -d
995 ? S< 0:00 /sbin/udevd -d
1038 ? S 0:00 [jbd2/sda1-8]
1039 ? S 0:00 [ext4-dio-unwrit]
1136 ? S 0:00 [kauditd]
1497 ? Ssl 0:00 /usr/sbin/vmware-vmblock-fuse -o subtype=vmware-vmblock,default_permissions,
1533 ? Sl 0:48 /usr/sbin/vmtoolsd
1588 ? S 0:00 /usr/lib/vmware-vgauth/VGAuthService -s
1712 ? Sl 0:15 /usr/lib/vmware-caf/pme/bin/ManagementAgentHost
1892 ? S<sl 0:00 auditd
1910 ? Ss 0:00 /sbin/portreserve
1917 ? Sl 0:00 /sbin/rsyslogd -i /var/run/syslogd.pid -c 5
1946 ? Ss 0:07 irqbalance --pid=/var/run/irqbalance.pid
2070 ? Ssl 0:00 NetworkManager --pid-file=/var/run/NetworkManager/NetworkManager.pid
2074 ? S 0:00 /usr/sbin/modem-manager
2122 ? Ss 0:00 cupsd -C /etc/cups/cupsd.conf
2135 ? Ss 0:00 /usr/sbin/wpa_supplicant -c /etc/wpa_supplicant/wpa_supplicant.conf -B -u -f
2160 ? Ss 0:00 /usr/sbin/acpid
2170 ? S 0:00 hald-runner
2214 ? S 0:00 hald-addon-input: Listening on /dev/input/event2 /dev/input/event0
2239 ? Ssl 0:01 automount --pid-file /var/run/autofs.pid
2259 ? S<s 0:00 /usr/sbin/bluetoothd --udev
2297 ? Ss 0:00 /usr/sbin/sshd
2305 ? Ss 0:00 xinetd -stayalive -pidfile /var/run/xinetd.pid
2396 ? S< 0:00 [krfcommd]
2474 ? Ss 0:00 /usr/libexec/postfix/master
2498 ? Ss 0:00 /usr/sbin/abrtd
2506 ? Ss 0:00 crond
2517 ? Ss 0:00 /usr/sbin/atd
2530 ? Ss 0:00 /usr/sbin/certmonger -S -p /var/run/certmonger.pid
2544 tty2 Ss+ 0:00 /sbin/mingetty /dev/tty2
2546 tty3 Ss+ 0:00 /sbin/mingetty /dev/tty3
2548 tty4 Ss+ 0:00 /sbin/mingetty /dev/tty4
2550 tty5 Ss+ 0:00 /sbin/mingetty /dev/tty5
2552 tty6 Ss+ 0:00 /sbin/mingetty /dev/tty6
3066 ? Ss 0:00 login -- root
3070 ? Sl 0:00 /usr/sbin/console-kit-daemon --no-daemon
3137 tty1 Ss+ 0:00 -bash
3257 ? S 0:00 /usr/libexec/polkit-1/polkitd
4363 ? Ss 0:00 /usr/bin/perl /usr/libexec/webmin/miniserv.pl /etc/webmin/miniserv.conf
7616 ? Ss 0:01 /usr/sbin/httpd -k start
7766 ? S 0:00 /usr/libexec/hald-addon-rfkill-killswitch
8907 ? S 0:00 /sbin/dhclient -d -4 -sf /usr/libexec/nm-dhcp-client.action -pf /var/run/dhc
9850 ? Ss 0:00 sshd: root@pts/0
9852 pts/0 Ss 0:00 -bash
10416 ? S 0:00 [flush-8:0]
10417 ? S 0:00 [flush-8:16]
10433 pts/0 R+ 0:00 ps x
[root@test1 ~]#
State | Meaning |
---|---|
R | Running. The process is running or ready to run. |
S | Sleeping. The process is not running; rather, it is waiting for an event, such as a keystroke or network packet. |
D | Uninterruptible sleep. Process is waiting for I/O such as a disk drive. |
T | Stopped. Process has been instructed to stop (more on this later). |
Z | A defunct or “zombie” process. This is a child process that has terminated but has not been cleaned up by its parent. |
< | A high-priority process. It’s possible to grant more importance to a process, giving it more time on the CPU. This property of a process is called niceness. A process with high priority is said to be less nice because it’s taking more of the CPU’s time, which leaves less for everybody else. |
N | A low-priority process. A process with low priority (a nice process) will get processor time only after other processes with higher priority have been serviced. |
动态查看进程统计信息top:
- top命令将会在当前终端以全屏交互式的界面显示进程排名,及时跟中包括CPU、内存等系统资源占用情况,默认情况下每三秒刷新一次
- 排序
- 按P查看CPU占用率的排名
- 按M查看内存占用率排名
[root@test2 jason]# top
top - 17:54:23 up 4:39, 4 users, load average: 0.01, 0.00, 0.00
Tasks: 209 total, 1 running, 208 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.1%us, 0.1%sy, 0.0%ni, 99.8%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
10020 root 20 0 15136 1316 948 R 0.7 0.0 0:00.06 top
1 root 20 0 19364 1536 1228 S 0.0 0.0 0:10.02 init
2 root 20 0 0 0 0 S 0.0 0.0 0:00.05 kthreadd
3 root RT 0 0 0 0 S 0.0 0.0 0:00.05 migration/0
4 root 20 0 0 0 0 S 0.0 0.0 0:00.09 ksoftirqd/0
5 root RT 0 0 0 0 S 0.0 0.0 0:00.00 migration/0
6 root RT 0 0 0 0 S 0.0 0.0 0:00.03 watchdog/0
7 root RT 0 0 0 0 S 0.0 0.0 0:00.08 migration/1
8 root RT 0 0 0 0 S 0.0 0.0 0:00.00 migration/1
9 root 20 0 0 0 0 S 0.0 0.0 0:00.09 ksoftirqd/1
10 root RT 0 0 0 0 S 0.0 0.0 0:00.03 watchdog/1
11 root RT 0 0 0 0 S 0.0 0.0 0:00.39 migration/2
12 root RT 0 0 0 0 S 0.0 0.0 0:00.00 migration/2
13 root 20 0 0 0 0 S 0.0 0.0 0:00.16 ksoftirqd/2
14 root RT 0 0 0 0 S 0.0 0.0 0:00.09 watchdog/2
15 root RT 0 0 0 0 S 0.0 0.0 0:00.09 migration/3
16 root RT 0 0 0 0 S 0.0 0.0 0:00.00 migration/3
17 root 20 0 0 0 0 S 0.0 0.0 0:00.09 ksoftirqd/3
18 root RT 0 0 0 0 S 0.0 0.0 0:00.23 watchdog/3
字段 | 含义 |
---|---|
top | 程序名 |
17:54:23 | 一天中的当前时间 |
up 4:39 | 正常运行时间(uptime)。从机器最后一次启动开始计算的时间总数。在这个例子中,系统已经运行约4.5小时。 |
4 users | 有四个用户 |
load average: | 负载均值(load average)指的是等待运行的进程数;即共享CPU资源的处于可运行状态的进程数。显示的三个值分别对应不同的时间段第一个对应的是前60秒的均值,下一个对应的是前5分钟的均值,最后一个对应的是前15分钟的均值。该指小于1.0表示该机器并不忙。 |
tasks | 统计进程数及各个进程的状态信息 |
0.1%us | 0.1%的CPU时间被用户进程占用,这里指的是处于内核外的进程 |
0.1%sy | 0.1%的CPU时间被系统进程(内核进程)占用 |
0.0%ni | 0.0%的CPU时间被友好进程(nice)(低优先级进程)占用 |
99.8%id | 99.8%的CPU时间是空闲的 |
0.0%wa | 0.0%的CPU时间用来等待I/O操作 |
英文版对照
Field | Meaning |
---|---|
17:54:23 | Current time of day. |
up 4:39 | This is called uptime. It is the amount of time since the machine was last booted. In this example, the system has been up for 4.5hours. |
4 users | Four users are logged in. |
load average: | Load average refers to the number of processes that are waiting to run; that is, the number of pro-cesses that are in a runnable state and are sharing the CPU. Three values are shown, each for a differ- ent period of time. The first is the average for the last 60 seconds, the next the previous 5 minutes, and finally the previous 15 minutes. Values under 1.0 indicate that the machine is not busy. |
Tasks: | This summarizes the number of processes and their various process states. |
0.1%us | 0.1% of the CPU is being used for user processes. This means processes outside of the kernel itself. |
0.1%sy | 0.1% of the CPU is being used for system(kernel) processes. |
0.0%ni | 0.0% of the CPU is being used by nice (low-priority) processes. |
98.3%id | 98.3% of the CPU is idle. |
0.0%wa | 0.0% of the CPU is waiting for I/O. |
Mem: | Shows how physical RAM is being used. |
Swap: | Shows how swap space (virtual memory) is being used. |
查看进程信息 pgrep:
- 用途
- 根据特定条件查询进程PID信息
- 常用选项
- -l:显示进程
- -U:指定特定用户
- -t:指定终端
- 实例
- pgrep -l "log”
- pgrep
[root@test2 jason]# pgrep -l log
2129 rsyslogd
7729 login
9982 login
[root@test2 jason]# pgrep -l -U root -t tty1
2782 Xorg
[root@test2 jason]#
查看进程信息pstree:
用途
- 以树形结构列出进程信息
常用选项
- -a:显示完整信息
- -u:列出对应用户名
- -p:列出对应PID号
示例
- pstree -aup
- pstree -ap
pstree -aup
[root@test2 jason]# pstree -aup
init,1
├─ManagementAgent,1924
│ ├─{ManagementAgen},1940
│ └─{ManagementAgen},1941
├─NetworkManager,2283 --pid-file=/var/run/NetworkManager/NetworkManager.pid
├─VGAuthService,1812 -s
├─abrtd,2704
├─acpid,2374
├─atd,2723
├─auditd,2104
│ └─{auditd},2105
├─automount,2477 --pid-file /var/run/autofs.pid
│ ├─{automount},2478
│ ├─{automount},2479
│ ├─{automount},2482
│ └─{automount},2485
├─bluetoothd,3667 --udev
├─bonobo-activati,3165 --ac-activate --ior-output-fd=18
│ └─{bonobo-activat},3166
├─certmonger,2735 -S -p /var/run/certmonger.pid
├─clock-applet,3413 --oaf-activate-iid=OAFIID:GNOME_ClockApplet_Factory --oaf-ior-fd=34
├─console-kit-dae,2826 --no-daemon
│ ├─{console-kit-da},2827
│ ├─{console-kit-da},2828
│ ├─{console-kit-da},2829
│ ├─{console-kit-da},2830
│ ├─{console-kit-da},2831
│ ├─{console-kit-da},2832
│ ├─{console-kit-da},2833
│ ├─{console-kit-da},2834
│ ├─{console-kit-da},2835
│ ├─{console-kit-da},2836
│ ├─{console-kit-da},2837
│ ├─{console-kit-da},2838
│ ├─{console-kit-da},2839
│ ├─{console-kit-da},2840
│ ├─{console-kit-da},2841
│ ├─{console-kit-da},2842
│ ├─{console-kit-da},2843
│ ├─{console-kit-da},2844
│ ├─{console-kit-da},2845
│ ├─{console-kit-da},2846
│ ├─{console-kit-da},2847
│ ├─{console-kit-da},2848
│ ├─{console-kit-da},2849
│ ├─{console-kit-da},2850
│ ├─{console-kit-da},2851
│ ├─{console-kit-da},2852
│ ├─{console-kit-da},2853
│ ├─{console-kit-da},2854
│ ├─{console-kit-da},2855
│ ├─{console-kit-da},2856
│ ├─{console-kit-da},2857
│ ├─{console-kit-da},2858
│ ├─{console-kit-da},2859
│ ├─{console-kit-da},2860
│ ├─{console-kit-da},2861
│ ├─{console-kit-da},2862
│ ├─{console-kit-da},2863
│ ├─{console-kit-da},2864
│ ├─{console-kit-da},2865
│ ├─{console-kit-da},2866
│ ├─{console-kit-da},2867
│ ├─{console-kit-da},2868
│ ├─{console-kit-da},2869
│ ├─{console-kit-da},2870
│ ├─{console-kit-da},2871
│ ├─{console-kit-da},2872
│ ├─{console-kit-da},2873
│ ├─{console-kit-da},2874
│ ├─{console-kit-da},2875
│ ├─{console-kit-da},2876
│ ├─{console-kit-da},2877
│ ├─{console-kit-da},2878
│ ├─{console-kit-da},2879
│ ├─{console-kit-da},2880
│ ├─{console-kit-da},2881
│ ├─{console-kit-da},2882
│ ├─{console-kit-da},2883
│ ├─{console-kit-da},2884
│ ├─{console-kit-da},2885
│ ├─{console-kit-da},2886
│ ├─{console-kit-da},2887
│ ├─{console-kit-da},2888
│ └─{console-kit-da},2890
├─crond,2712
├─cupsd,2347 -C /etc/cups/cupsd.conf
├─dbus-daemon,3055 --fork --print-pid 5 --print-address 7 --session
├─dbus-daemon,2273,dbus --system
├─dbus-launch,3054 --sh-syntax --exit-with-session
├─dbus-launch,2896,gdm --exit-with-session
├─devkit-power-da,2901
├─fprintd,10664
├─gconf-im-settin,3402
├─gconfd-2,3125
├─gdm-binary,2750 -nodaemon
│ ├─gdm-simple-slav,2779 --display-id /org/gnome/DisplayManager/Display1 --force-active-vt
│ │ ├─Xorg,2782 :0 -nr -verbose -audit 4 -auth /var/run/gdm/auth-for-gdm-ZXpbdO/database ...
│ │ ├─gdm-session-wor,2958
│ │ │ ├─gnome-session,3046
│ │ │ │ ├─abrt-applet,3276
│ │ │ │ ├─bluetooth-apple,3232
│ │ │ │ ├─evolution-alarm,3252
│ │ │ │ ├─gdu-notificatio,3190
│ │ │ │ ├─gnome-panel,3156
│ │ │ │ ├─gnome-power-man,3215
│ │ │ │ ├─gnome-volume-co,3312
│ │ │ │ ├─gpk-update-icon,3226
│ │ │ │ ├─metacity,3151
│ │ │ │ ├─nautilus,3163
│ │ │ │ ├─nm-applet,3219 --sm-disable
│ │ │ │ ├─polkit-gnome-au,3228
│ │ │ │ ├─python,3217 /usr/share/system-config-printer/applet.py
│ │ │ │ └─{gnome-session},3127
│ │ │ └─{gdm-session-wo},3047
│ │ └─{gdm-simple-sla},2783
│ └─{gdm-binary},2780
├─gdm-user-switch,3409 --oaf-activate-iid=OAFIID:GNOME_FastUserSwitchApplet_Factory--oaf-i
├─gnome-keyring-d,3036 --daemonize --login
│ ├─{gnome-keyring-},3037
│ └─{gnome-keyring-},3134
├─gnome-screensav,3362
├─gnome-settings-,3133
│ └─{gnome-settings},3136
├─gnome-terminal,3881 -x /bin/sh -c cd\040'/root/Desktop'\040&&\040exec\040$SHELL
│ ├─bash,3883
│ ├─gnome-pty-helpe,3882
│ └─{gnome-terminal},3884
├─gnote,3415 --panel-applet --oaf-activate-iid=OAFIID:GnoteApplet_Factory --oaf-ior-fd=40
├─gvfs-afc-volume,3184
│ └─{gvfs-afc-volum},3185
├─gvfs-fuse-daemo,3144 /root/.gvfs
│ ├─{gvfs-fuse-daem},3145
│ ├─{gvfs-fuse-daem},3146
│ └─{gvfs-fuse-daem},3147
├─gvfs-gdu-volume,3175
├─gvfs-gphoto2-vo,3187
├─gvfsd,3140
├─gvfsd-burn,3464 --spawner :1.7 /org/gtk/gvfs/exec_spaw/1
├─gvfsd-metadata,3460
├─gvfsd-trash,3179 --spawner :1.7 /org/gtk/gvfs/exec_spaw/0
├─hald,2383,haldaemon
│ ├─hald-runner,2384,root
│ │ ├─hald-addon-acpi,2454,haldaemon
│ │ ├─hald-addon-inpu,2435
│ │ └─hald-addon-rfki,10493
│ └─{hald},2385
├─im-settings-dae,3231
├─irqbalance,2158 --pid=/var/run/irqbalance.pid
├─master,2680
│ ├─pickup,10631,postfix -l -t fifo -u
│ └─qmgr,2696,postfix -l -t fifo -u
├─mingetty,2755 /dev/tty2
├─mingetty,2757 /dev/tty3
├─mingetty,2759 /dev/tty4
├─mingetty,2761 /dev/tty5
├─mingetty,2763 /dev/tty6
├─miniserv.pl,2747 /usr/libexec/webmin/miniserv.pl /etc/webmin/miniserv.conf
├─modem-manager,2287
├─notification-ar,3411 --oaf-activate-iid=OAFIID:GNOME_NotificationAreaApplet_Factory--o
├─polkitd,2942
├─pulseaudio,3222 --start
│ ├─gconf-helper,3403
│ ├─{pulseaudio},3317
│ └─{pulseaudio},3323
├─rpc.statd,2302,rpcuser
├─rpcbind,2172,rpc
├─rsyslogd,2129 -i /var/run/syslogd.pid -c 5
│ ├─{rsyslogd},2130
│ ├─{rsyslogd},2131
│ └─{rsyslogd},2132
├─rtkit-daemon,2953,rtkit
│ ├─{rtkit-daemon},2954
│ └─{rtkit-daemon},2955
├─seahorse-daemon,3137
├─trashapplet,3173 --oaf-activate-iid=OAFIID:GNOME_Panel_TrashApplet_Factory --oaf-ior-fd=25
├─udevd,538 -d
│ ├─udevd,3660 -d
│ └─udevd,10598 -d
├─udisks-daemon,3181
│ ├─udisks-daemon,3182
│ └─{udisks-daemon},3443
├─vmtoolsd,1757
│ └─{vmtoolsd},3473
├─vmtoolsd,3191 -n vmusr --blockFd 3
├─vmware-vmblock-,1721 -o subtype=vmware-vmblock,default_permissions,allow_other/var/run/vmblock
│ ├─{vmware-vmblock},1722
│ └─{vmware-vmblock},1723
├─wnck-applet,3171 --oaf-activate-iid=OAFIID:GNOME_Wncklet_Factory --oaf-ior-fd=18
├─wpa_supplicant,2348 -c /etc/wpa_supplicant/wpa_supplicant.conf -B -u -f/var/log/wpa_supplicant.log
└─xinetd,2510 -stayalive -pidfile /var/run/xinetd.pid
├─in.telnetd,7728
│ └─login,7729
│ └─bash,7730,jason
│ └─su,8748,root
│ └─bash,8752
└─in.telnetd,10637
└─login,10638
└─bash,10639,jason
└─su,10662,root
└─bash,10666
└─pstree,10679 -aup
[root@test2 jason]#
pstree -ap jason
[root@test2 jason]# pstree -ap jason
bash,7730
└─su,8748
└─bash,8752
bash,10639
└─su,10662
└─bash,10666
└─pstree,10681 -ap jason
[root@test2 jason]#