有刚毕业进入测试岗位的小伙伴问我,linux下查看进程的命令有哪些,怎么使用的,我在这里总结一下。

一、ps 命令

ps (Process Status) 是最基本的进程查看命令。

语法:

ps [选项]      #仅显示当前终端下的进程

基本用法:

ps -aux # 显示所有用户的进程
ps -ef # 以完整格式显示所有进程
ps -u username # 显示指定用户的进程

常见选项功能说明:
a:显示所有终端上的进程
u:显示进程的详细状态
x:显示没有控制终端的进程
-e:显示所有进程
-f:显示完整格式的输出
-l:显示长格式

案例实战:

[root@liuguohua ~]# ps -aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.1 0.3 170440 12672 ? Ss 12:35 0:00 /usr/lib/systemd/systemd --switched-root --system --deserialize 31
root 2 0.0 0.0 0 0 ? S 12:35 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? S 12:35 0:00 [pool_workqueue_]
root 4 0.0 0.0 0 0 ? I< 12:35 0:00 [kworker/R-rcu_g]
root 5 0.0 0.0 0 0 ? I< 12:35 0:00 [kworker/R-rcu_p]
root 6 0.0 0.0 0 0 ? I< 12:35 0:00 [kworker/R-slub_]
root 7 0.0 0.0 0 0 ? I< 12:35 0:00 [kworker/R-netns]
root 10 0.0 0.0 0 0 ? I 12:35 0:00 [kworker/u512:0-events_unbound]
root 819 0.0 1.0 129620 40056 ? S 12:36 0:00 /www/server/panel/pyenv/bin/python3 /www/server/panel/BT-Panel
root 842 0.1 1.5 1478708 62476 ? Sl 12:36 0:00 /www/server/panel/pyenv/bin/python3 /www/server/panel/BT-Task

二、top 命令

top 提供动态实时的系统状态监控,包括进程信息。

语法:

top

交互命令(在 top 运行时使用):

M:按内存使用排序
P:按 CPU 使用排序
c:输入命令的完整路径
q:退出
k:终止进程(输入 PID)

案例实战:

输入top,系统会显示一个资源管理的容器,接着输入c,COMMAND栏会显示该命令的详细路径,最后按q,退出top命令。

三、pstree 命令

以树状图显示进程及其子进程。

语法:
pstree

常见选项功能说明:
-p:显示 PID
-u:显示用户名
-a:显示完整命令

案例实战:

[root@liuguohua /]# pstree nginx
nginx

nginx

nginx

nginx
[root@liuguohua /]# pstree -pua nginx
nginx,1171

nginx,1172

nginx,1173

nginx,1174

四、pgrep 命令

根据名称查找进程 ID。

pgrep sshd # 查找 sshd 进程的 PID

实例实战:

[root@liuguohua ~]# ps aux |grep sshd
root 704 0.0 0.2 16668 9216 ? Ss 12:36 0:00 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups
root 998 0.0 0.2 20008 11520 ? Ss 12:47 0:00 sshd: root [priv]
root 1011 0.0 0.1 20008 6784 ? S 12:47 0:00 sshd: root@pts/0
root 1081 0.1 0.2 20012 11392 ? Ss 12:51 0:00 sshd: root [priv]
root 1083 0.0 0.1 20012 6676 ? S 12:51 0:00 sshd: root@pts/1
root 1118 0.0 0.0 6408 2304 pts/1 R+ 12:51 0:00 grep --color=auto sshd

[root@liuguohua ~]# pgrep sshd
704
998
1011
1081
1083

五、pidof 命令

查找指定程序的进程 ID。

pidof nginx #查找nginx 进程的 PID

案例实战:

[root@liuguohua ~]# pidof sshd
1083 1081 1011 998 704
声明:欢迎大家光临本站,学习IT运维技术,转载本站内容,请注明内容出处”来源刘国华教育“。如若本站内容侵犯了原著者的合法权益,请联系我们进行处理。