操作系统命令 [Linux 性能监控]() top ps aux kill df ss lsof top Linux 的 top 命令是一个性能监控程序,许多系统管理员经常使用它来监控 Linux 性能。该命令在许多类 Linux/Unix 操作系统中都可用。top 命令用于以有序列表显示所有正在运行和活跃的实时进程,并定期更新这些信息。它显示 CPU 使用率、内存使用率、交换内存、缓存大小、缓冲区大小、进程 PID、用户、命令等信息。它还显示了运行过程中占用高内存和 CPU 的进程。
Linux 的 top 命令是一个性能监控程序,许多系统管理员经常使用它来监控 Linux 性能。该命令在许多类 Linux/Unix 操作系统中都可用。top 命令用于以有序列表显示所有正在运行和活跃的实时进程,并定期更新这些信息。它显示 CPU 使用率、内存使用率、交换内存、缓存大小、缓冲区大小、进程 PID、用户、命令等信息。它还显示了运行过程中占用高内存和 CPU 的进程。
显示特定用户的进程
$ top -u root
ps 命令显示有关选择的活动进程的信息。
它提供了一个当前进程的快照,包括用户 ID、CPU 使用率、内存使用率、命令名称等详细信息。它不像 top 或 htop 命令那样实时显示数据。
打印系统中所有正在运行的进程
$ps -A
或者
$ps -e
$ps -ef
或者
$ps -eF
ps aux 命令是 Linux 管理员最常用的命令。
ps aux 命令选项
a:- 此选项打印所有用户正在运行的进程。
u:- 此选项在输出中显示用户或所有者列。
x:- 此选项打印未从终端执行的进程。
综合来看,选项 "aux" 打印系统中所有正在运行的进程,无论它们是从哪里执行的。
ps aux 输出的意义
要显示由 root 用户账户运行的所有进程,请使用以下命令。
$ps -U root -u root
$ps -U [UserName] -u [UserName]
$ps -G [Group Name]
$ps –A --forest
$ps –eo pid,user,cmd
$ps –eo pid,user,%mem,cmd --sort=-%mem
在 Linux 和 Unix 中,如果你想停止一个正在运行的进程,可以通过命令行界面使用 kill 命令。kill 命令的基本形式非常简单,如果你想要终止一个进程,只需要知道进程的 ID 号。
查找正在运行的进程的 PID
要查找正在运行的进程的进程 ID 或 PID,我们将使用 ps 命令。此命令将列出正在运行的进程及其相关信息。ps 命令有许多选项和显示进程的方法;我甚至可以写一篇文章专门介绍 ps 命令。为了这个例子,我将只使用带有 -C 标志的 ps 命令,此标志可以用来通过要运行的命令的名称来查找进程。
$ps -C <command>
# ps -C nginx PID TTY TIME CMD 566 ? 00:00:00 nginx 567 ? 00:00:00 nginx 568 ? 00:00:06 nginx 570 ? 00:00:06 nginx 571 ? 00:00:06 nginx
使用 kill 终止进程
现在我们已经找到了要停止的进程的 PID,我们可以使用 kill 命令来终止该进程。
语法:
$ kill <pid>
root:/# ps -C nginx PID TTY TIME CMD 566 ? 00:00:00 nginx 567 ? 00:00:00 nginx 568 ? 00:00:08 nginx 570 ? 00:00:09 nginx 571 ? 00:00:08 nginx root:/# kill 571 root:/# ps -C nginx PID TTY TIME CMD 566 ? 00:00:00 nginx 567 ? 00:00:00 nginx 568 ? 00:00:08 nginx 570 ? 00:00:09 nginx 8347 ? 00:00:00 nginx
df (abbreviated disk disk free) is a standard Unix command used to display the amount of available disk space for file systems on which the invoking user has appropriate write access.df 对于 Unix-like 和 Linux 系统来说,自 1987 年的第 2 版以来就是 X/Open 可移植性指南的一部分。$ df
Filesystem 1024-blocks Free %Used Iused %Iused Mounted on /dev/hd4 32768 16016 52% 2271 14% / /dev/hd2 4587520 1889420 59% 37791 4% /usr /dev/hd9var 65536 12032 82% 518 4% /var /dev/hd3 819200 637832 23% 1829 1% /tmp /dev/hd1 524288 395848 25% 421 1% /home /proc - - - - - /proc /dev/hd10opt 65536 26004 61% 654 4% /opt
-h argument which displays in more human readable formats like KB, MB, GB, 或 TB$ df -h 或 df --human-readable
Filesystem Size Used Avail Use% Mounted on /dev/root 3.0G 2.3G 720M 77% / devtmpfs 123M 0 123M 0% /dev
-a 或 --all: List all dummy, duplicate, or inaccessible files which are ommited by default.-k: Use 1024-byte units, instead of default 512-byte units, when writing large figures.-P 或 --portability: Use a POSIX portable output format, when writing to files.-t fstype 或 --type=fstype: Limit the listing of the fstype. By default nothing is ommited.ss command is a tool used to dump socket statistics and display information in a similar fashion as netstat.ss command can display stats for PACKETS, TCP, UDP, DCCP, RAW and Unix 域套接字.$ ss will list all the connections regardless of the state they are in.$ ss -a will list all the listening and not listening ports.$ ss -l will display only the listening ports.$ ss -t will display only the TCP connections.$ ss -lt will display the listening TCP ports.$ ss -ua will display all the UDP ports.$ ss -lu will display all the listening UDP ports.$ ss -p will display all the PID (Process IDs) of the sockets.$ ss -s will display all the summary statistics.$ ss -4 and $ ss -6 will display the ipv4 and ipv6 connections respectively.$ ss -at '(dport = :22 或 sport = :22)', will filter all socket connections with a given destination port (dport) and source port (sport).$ ss -at '(dport = :ssh 或 sport = :ssh)', will filter all socket connections with a given destination port (dport) and source port (sport), but here the port is given as a service.列出打开的文件, which is used in many Unix-like systems to report a list of all open files and the processes that opened them.lsof is very handy tool in analyzing these resources.$ lsof will list all open files.$ lsof -u 用户名 will list files opened by specific user.$ lsof -i TCP:22 will list processes running on specific port.$ lsof -i 4 or $ lsof -i 6 will list IPv4 and IPv6 network files.$ lsof -i TCP:1-1024 will list processes running on specific range of port.$ lsof -i -u^用户名 will list open files not owned by specific user.$ lsof -i will shows the list of all network connections 监听和已建立连接.$ lsof -p 1 will show open files owned by specific process id.$ lsof -c 进程名 将列出特定进程打开的所有文件。声明:
本文件灏天文库团队进行了翻译。尽管我们力求准确,但请注意,翻译可能包含错误或不准确之处。原文档以其原始语言为准。我们不对因使用此翻译而产生的任何误解或误译负责。