Linux 查看系统进程命令ps,这个命令很重要。有点类似于windows的任务管理器的意思。
ps命令用来列出系统中当前运行的那些进程。
ps命令列出的是当前那些进程的快照。
就是执行ps命令的那个时刻的那些进程,如果想要动态的显示进程信息,就可以使用top命令。
1 :参数
a 显示所有进程
-a 显示同一终端下的所有程序
-A 显示所有进程
c 显示进程的真实名称
-N 反向选择
-e 等于“-A”
e 显示环境变量
f 显示程序间的关系
-H 显示树状结构
r 显示当前终端的进程
T 显示当前终端的所有程序
u 指定用户的所有进程
-au 显示较详细的资讯
-aux 显示所有包含其他使用者的行程
-C<命令> 列出指定命令的状况
--lines<行数> 每页显示的行数
--width<字符数> 每页显示的字符数
--help 显示帮助信息
--version 显示版本显示
Ps的参数比较多,这里基本上就是常用的参数。
2 :示例
(1):显示所有当前进程
[root@iZuf60ynur81p6k0ysvtneZ /]# ps -ax
PID TTY STAT TIME COMMAND
1 ? Ss 1:37 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
2 ? S 0:00 [kthreadd]
4 ? S< 0:00 [kworker/0:0H]
5 ? S 0:07 [kworker/u2:0]
6 ? S 0:05 [ksoftirqd/0]
7 ? S 0:00 [migration/0]
8 ? S 0:00 [rcu_bh]
9 ? R 2:25 [rcu_sched]
10 ? S< 0:00 [lru-add-drain]
11 ? S 0:09 [watchdog/0]
13 ? S 0:00 [kdevtmpfs]
14 ? S< 0:00 [netns]
15 ? S 0:00 [khungtaskd]
16 ? S< 0:00 [writeback]
17 ? S< 0:00 [kintegrityd]
…………………………………………………………
(2):根据用户过滤进程
在需要查看特定用户进程的情况下,我们可以使用 -u 参数。比如我们要查看用户apache的进程,可以通过下面的命令:
[root@iZuf60ynur81p6k0ysvtneZ /]# ps -u apache
PID TTY TIME CMD
990 ? 00:00:07 php-fpm
991 ? 00:00:07 php-fpm
992 ? 00:00:09 php-fpm
993 ? 00:00:08 php-fpm
994 ? 00:00:07 php-fpm
1170 ? 00:00:09 php-fpm
1202 ? 00:00:08 php-fpm
(3):通过cpu和内存使用来过滤进程
[root@iZuf60ynur81p6k0ysvtneZ /]# ps -aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.2 51728 3956 ? Ss Aug12 1:37 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
root 2 0.0 0.0 0 0 ? S Aug12 0:00 [kthreadd]
root 4 0.0 0.0 0 0 ? S< Aug12 0:00 [kworker/0:0H]
root 5 0.0 0.0 0 0 ? S Aug12 0:07 [kworker/u2:0]
root 6 0.0 0.0 0 0 ? S Aug12 0:05 [ksoftirqd/0]
root 7 0.0 0.0 0 0 ? S Aug12 0:00 [migration/0]
root 8 0.0 0.0 0 0 ? S Aug12 0:00 [rcu_bh]
root 9 0.0 0.0 0 0 ? R Aug12 2:25 [rcu_sched]
…………………………………………………………
(4):查找具体进程
我们来查找nginx的进程
[root@iZuf60ynur81p6k0ysvtneZ /]# ps -aux | grep nginx
root 8709 0.0 0.0 46604 1208 ? Ss Aug14 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx 8710 0.0 0.1 47600 2940 ? S Aug14 0:01 nginx: worker process
root 22387 0.0 0.0 112812 968 pts/0 R+ 16:51 0:00 grep --color=auto nginx
**
**
(5):通过进程名和PID过滤
我们来查找nginx
[root@iZuf60ynur81p6k0ysvtneZ /]# ps -C nginx
PID TTY TIME CMD
8709 ? 00:00:00 nginx
8710 ? 00:00:01 nginx
我们查看详细信息:
[root@iZuf60ynur81p6k0ysvtneZ /]# ps -f -C nginx
UID PID PPID C STIME TTY TIME CMD
root 8709 1 0 Aug14 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx 8710 8709 0 Aug14 ? 00:00:01 nginx: worker process
(6):根据线程来过滤进程
[root@iZuf60ynur81p6k0ysvtneZ /]# ps -L 8710
PID LWP TTY STAT TIME COMMAND
8710 8710 ? S 0:01 nginx: worker process
(7):树形显示进程
[root@iZuf60ynur81p6k0ysvtneZ /]# ps -axjf
PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND
1 852 852 852 ? -1 Ssl 995 15:56 /usr/sbin/mysqld --basedir=/usr
1 1042 1042 1042 ? -1 Ss 0 0:00 /usr/sbin/sshd -D
1042 20537 20537 20537 ? -1 Ss 0 0:00 _ sshd: root@pts/0
20537 20539 20539 20539 pts/0 24360 Ss 0 0:00 _ -bash
20539 22155 22154 20539 pts/0 24360 T 0 0:00 _ less
20539 22297 22296 20539 pts/0 24360 T 0 0:00 _ less
20539 24360 24360 20539 pts/0 24360 R+ 0 0:00 _ ps -axjf
1 8709 8709 8709 ? -1 Ss 0 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
8709 8710 8709 8709 ? -1 S 997 0:01 _ nginx: worker process
1 10039 10039 10039 ? -1 Ssl 0 79:39 /usr/sbin/aliyun-service
1 5599 5599 5599 ? -1 S<sl 0 4:38 /usr/local/aegis/aegis_update/AliYunDunUpdate
1 5644 5644 5644 ? -1 S<sl 0 78:15 /usr/local/aegis/aegis_client/aegis_10_85/AliYunDun
进程显示树形还有另一个命令pstree
[root@iZuf60ynur81p6k0ysvtneZ /]# pstree
systemd─┬─AliYunDun───22*[{AliYunDun}]
├─AliYunDunUpdate───3*[{AliYunDunUpdate}]
├─2*[agetty]
├─aliyun-service───2*[{aliyun-service}]
├─atd
├─auditd───{auditd}
├─chronyd
├─crond
├─dbus-daemon
├─dhclient
├─mysqld───29*[{mysqld}]
├─nginx───nginx
├─php-fpm───7*[php-fpm]
├─polkitd───6*[{polkitd}]
├─rsyslogd───2*[{rsyslogd}]
├─sshd───sshd───bash─┬─2*[less]
│ └─pstree
├─systemd-journal
├─systemd-logind
├─systemd-udevd
└─tuned───4*[{tuned}]
(9):显示安全信息
如果想要查看现在有谁登入了你的服务器。可以使用ps命令加上相关参数:
[root@iZuf60ynur81p6k0ysvtneZ /]# ps -eo pid,user,args
PID USER COMMAND
471 polkitd /usr/lib/polkit-1/polkitd --no-debug
472 dbus /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation
487 root /usr/lib/systemd/systemd-logind
498 chrony /usr/sbin/chronyd
504 root /usr/sbin/atd -f
512 root /usr/sbin/crond -n
990 apache php-fpm: pool www
991 apache php-fpm: pool www
992 apache php-fpm: pool www
993 apache php-fpm: pool www
994 apache php-fpm: pool www
1042 root /usr/sbin/sshd -D
1170 apache php-fpm: pool www
1202 apache php-fpm: pool www
8710 nginx nginx: worker process
(10):把所有进程显示出来,并输出到ps001.txt文件
ps -aux > ps001.txt
(11):输出指定的字段
[root@iZuf60ynur81p6k0ysvtneZ /]# ps -o pid,ppid,pgrp,session,tpgid,comm
PID PPID PGRP SESS TPGID COMMAND
20539 20537 20539 20539 24823 bash
22155 20539 22154 20539 24823 less
22297 20539 22296 20539 24823 less
24602 20539 24602 20539 24823 watch
24643 20539 24643 20539 24823 watch
24823 20539 24823 20539 24823 ps
我这里对ps aux命令输出的每一列都代表什么解释一下:
USER:该进程是由哪个用户产生的
PD:进程的D号
%CPU:该进程占用CPU资源的百分比,占用越高,进程越耗费资源;
%MEM:该进程占用物理内存的百分比,占用越高,进程越耗费资源;
VSZ:该进程占用虚拟内存的大小,单位KB;
RSS:该进程占用实际物理内存的大小,单位KB;
TTY:该进程是在哪个终端中运行的。对于CentOS来说,y1是图形化终端,
ty2-y6是本地的字符界面终端。pts/0-255代表虚拟终端。
STAT:进程状态。常见的状态有:R:运行状态、S:睡眠状态、T:暂停状态、Z:僵尸状态、S:包含子进程、1:多线程、+:前台显示
START:该进程的启动时间~
最后,这里再扩展一个查看进程树的命令:pstree,运行实例如下
root@iZijvdp1z0m5q4Z:~# pstree
systemd─┬─AliSecGuard───6*[{AliSecGuard}]
├─AliYunDun───25*[{AliYunDun}]
├─AliYunDunUpdate───5*[{AliYunDunUpdate}]
├─CmsGoAgent.linu─┬─exe───12*[{exe}]
│ └─5*[{CmsGoAgent.linu}]
├─accounts-daemon───2*[{accounts-daemon}]
├─2*[agetty]
├─aliyun-service───8*[{aliyun-service}]
├─assist_daemon───7*[{assist_daemon}]
├─atd
├─chronyd
├─cron───6*[cron───sh───lynx───lynx]
├─dbus-daemon
├─mysqld───62*[{mysqld}]
├─networkd-dispat───{networkd-dispat}
├─nginx───2*[nginx]
├─php-fpm7.3───3*[php-fpm7.3]
├─redis-server───3*[{redis-server}]
├─rsyslogd───3*[{rsyslogd}]
├─sshd───sshd───bash───pstree
├─systemd───(sd-pam)
├─systemd-journal
├─systemd-logind
├─systemd-network
├─systemd-resolve
├─systemd-udevd
└─unattended-upgr───{unattended-upgr}
pstree命令将所有进程以树状图显示,树状图将会以 pid (如果有指定) 或是以 init 这个基本进程为根 (root),如果有指定使用者 id,则树状图会只显示该使用者所拥有的进程。
有好的建议,请在下方输入你的评论。