ps、top、htop、pgrep、pidof等命令用于查看Linux进程信息:ps提供静态快照,top和htop实现动态监控,pgrep和pidof按名称查找进程ID;其中ps的STAT列显示进程状态,如R(运行)、S(睡眠)、Z(僵尸)等;top默认按CPU使用率排序,可结合P、M、N键切换排序方式,并用k键终止高耗进程;pgrep结合kill可精准结束指定进程,如pgrep firefox获取PID后执行kill或kill -9强制终止。

查看Linux命令行中的进程信息,主要依靠
ps
top
htop
pgrep
pidof
ps命令:静态快照 top命令:动态实时视图 pgrep命令:根据名称查找进程ID pidof命令:查找指定程序运行的进程ID
ps命令是最基础的,它能提供进程的静态快照。但如果你想实时监控进程动态,top命令就更合适了。而当你只想找到某个特定进程的ID时,pgrep或者pidof会更便捷。
解决方案
1. 使用 ps
ps
ps aux
a
u
x
ps -ef
e
f
ps -u <用户名>
ps -u john
例如,
ps aux | grep firefox
2. 使用 top
top
top
P
M
N
top -u <用户名>
3. 使用 htop
htop
top
htop
sudo apt install htop
sudo yum install htop
4. 使用 pgrep
pgrep
pgrep <进程名>
pgrep firefox
pgrep -u <用户名> <进程名>
5. 使用 pidof
pidof
pidof <程序名>
pidof firefox
pidof
pgrep
6. 使用 pstree
pstree
pstree
pstree -p
pstree <用户名>
如何理解ps命令输出结果中的STAT列?
ps
STAT
R
S
D
T
Z
<
N
L
S
L
+
W
例如,
Ss
R+
如何使用top命令监控CPU使用率高的进程?
top
top
top
top
%CPU
top
%CPU
k
k
top
SIGTERM
SIGKILL
SIGKILL
在
top
P
M
N
h
q
top
如何使用pgrep和kill命令杀死指定进程?
pgrep
kill
使用 pgrep
pgrep <进程名>
例如,要查找所有名为 "firefox" 的进程的 PID,可以运行
pgrep firefox
使用 kill
kill <进程ID>
例如,如果
pgrep firefox
kill 1234
kill -9 <进程ID>
SIGKILL
示例:
# 查找 firefox 进程的 PID
pids=$(pgrep firefox)
# 检查是否找到了进程
if [ -n "$pids" ]; then
  # 循环结束所有找到的进程
  for pid in $pids; do
    echo "正在结束进程 $pid"
    kill $pid
  done
else
  echo "没有找到 firefox 进程"
fi这个脚本首先使用
pgrep
pgrep
以上就是Linux命令行中如何查看进程信息的详细内容,更多请关注php中文网其它相关文章!
                        
                        每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
                Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号