Linux命令之ps/netstat

870 阅读4分钟

日常工作中经常要用到一些Linux命令,该系列来记录linux常见命令
在我们需要查看一个运行中程序的相关状态时,经常看到大佬会用到netstat/ps这两个命令
那么这两个命令到底是用于什么场景呢?
我们一起来了解一下吧

应用场景

  • ps 命令用于显示当前进程 (process) 的状态
  • netstat 命令用于显示网络 (networt) 状态; 很好记的,毕竟netstat的前缀是net(网)

ps

ps用于查看当前运行的进程,也可以结合grep查找进程里符合条件进程信息
它的参数有很多,这里挑些常见的说明一下:

-a: 显示所有的进程,包含每个命令的完整路径
-x: 显示所有系统程序,包括那些没有终端的程序
-u: 显示使用者的名称和起始时间
-w: 显示加宽可以显示较多的资讯
-au: 显示较详细的资讯
-aux:显示所有包含其他使用者的行程
USER: 行程拥有者
PID: pid
%CPU: 占用的 CPU 使用率
%MEM: 占用的记忆体使用率

ps中最常用的选项组合就是ps auxps -ef

两者的输出结果差别不大,但展示风格不同 aux是BSD风格,-ef是System V风格这是次要的区别,主要影响使用的区别是aux会截断command列,而-ef不会 当结合grep时这种区别会影响到结果

sunjiaying@macbookpro-6d5b:~$ --> ps aux
USER               PID  %CPU %MEM      VSZ    RSS   TT  STAT STARTED      TIME COMMAND
_windowserver      152  21.5  0.8  8002848  71096   ??  Ss   六08下午 249:46.38 /System/Library/PrivateFrameworks/SkyLight
sunjiaying         272  10.8  2.8  5696336 236504   ??  S    六08下午  85:48.34 /Applications/Google Chrome.app/Contents/M
sunjiaying         302   8.9  0.9  5418416  72704   ??  S    六08下午   2:48.62 /Applications/iTerm.app/Contents/MacOS/iTe
sunjiaying         365   8.9  1.3  6424540 110784   ??  S    六08下午  91:31.12 /Applications/Google Chrome.app/Contents/F
sunjiaying         286   6.2  4.2 179654552 350308   ??  S    六08下午 372:34.07 /Applications/GoLand.app/Contents/MacOS/go
root              9982   3.5  0.3  4389308  24588   ??  Ss   10:30上午   1:20.82 /usr/sbin/bluetoothaudiod
sunjiaying        3629   3.3  1.8 93798812 148580   ??  Ss   日12下午  44:08.70 /System/Library/Frameworks/WebKit.framewor
sunjiaying       15953   2.2  1.5  9123500 123428   ??  S    一10上午   6:38.44 /Applications/Google Chrome.app/Contents/F
sunjiaying       10460   1.8  1.6  9032300 132368   ??  S    11:12上午   0:03.48 /Applications/Google Chrome.app/Contents/F
...
sunjiaying@macbookpro-6d5b:~$ --> ps -ef
  UID   PID  PPID   C STIME   TTY           TIME CMD
    0     1     0   0 六08下午 ??        19:11.19 /sbin/launchd
    0    39     1   0 六08下午 ??         0:08.82 /usr/sbin/syslogd
    0    40     1   0 六08下午 ??         0:35.09 /usr/libexec/UserEventAgent (System)
    0    43     1   0 六08下午 ??         0:16.71 /System/Library/PrivateFrameworks/Uninstall.framework/Resources/uninstalld
    0    44     1   0 六08下午 ??         0:09.93 /usr/libexec/kextd
    0    45     1   0 六08下午 ??         3:03.37 /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/FSEvents.framework/Versions/A/Support/fseventsd
    0    47     1   0 六08下午 ??         0:12.73 /System/Library/PrivateFrameworks/MediaRemote.framework/Support/mediaremoted
   55    50     1   0 六08下午 ??         0:01.51 /System/Library/CoreServices/appleeventsd --server
    0    51     1   0 六08下午 ??        25:20.50 /usr/sbin/systemstats --daemon
    0    53     1   0 六08下午 ??         0:42.14 /usr/libexec/configd
    0    54     1   0 六08下午 ??         1:46.19 /System/Library/CoreServices/powerd.bundle/powerd
    0    57     1   0 六08下午 ??         2:42.02 /usr/libexec/logd
    0    64     1   0 六08下午 ??         4:23.27 /System/Library/Frameworks/CoreServices.framework/Frameworks/Metadata.framework/Support/mds
sunjiaying@macbookpro-6d5b:~$ --> ps aux | grep redis
sunjiaying       10172   0.3  0.0  4347036   1048   ??  Ss   10:47上午   0:11.98 redis-server 127.0.0.1:6379
sunjiaying       10879   0.0  0.0  4288536    876 s011  S+   11:53上午   0:00.00 grep redis
sunjiaying@macbookpro-6d5b:~$ --> ps -ef | grep redis
  501 10172     1   0 10:47上午 ??         0:12.00 redis-server 127.0.0.1:6379
  501 10881 99122   0 11:53上午 ttys011    0:00.00 grep redis

netstat

netstat是一个基于命令行界面的网络实用工具,可显示当前的网络状态,包括传输控制协议层的连线状况、路由表、网络接口状态和网络协议的统计信息等。

它的参数也有很多,这里挑些常见的说明一下:

-a:显示所有连线中的Socket
-A:列出该网络类型连线中的相关地址
-c:持续列出网络状态
-C:显示路由器配置的快取信息
-e:显示网络其他相关信息
-F:显示FIB
-g:显示多重广播功能群组组员名单
-h:在线帮助
-i:显示网络界面信息表单
-l:显示监控中的服务器的Socket
-M:显示伪装的网络连线
-n:直接使用ip地址,而不通过域名服务器
-N:显示网络硬件外围设备的符号连接名称
-o:显示计时器
-p:显示正在使用Socket的程序识别码和程序名称
-r:显示Routing Table
-s:显示网络工作信息统计表
-t:显示TCP传输协议的连线状况
-u:显示UDP传输协议的连线状况
-v:显示指令执行过程
-V:显示版本信息
-w:显示RAW传输协议的连线状况
-x:此参数的效果和指定"-A unix"参数相同
--ip:此参数的效果和指定"-A inet"参数相同

# netstat -apu
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address        Foreign Address       State    PID/Program name  
udp    0   0 *:32768           *:*                   -          
udp    0   0 *:nfs            *:*                   -          
udp    0   0 *:641            *:*                   3006/rpc.statd   
udp    0   0 192.168.0.3:netbios-ns   *:*                   3537/nmbd      
udp    0   0 *:netbios-ns        *:*                   3537/nmbd      
udp    0   0 192.168.0.3:netbios-dgm   *:*                   3537/nmbd      
udp    0   0 *:netbios-dgm        *:*                   3537/nmbd      
udp    0   0 *:tftp           *:*                   3346/xinetd     
udp    0   0 *:999            *:*                   3366/rpc.rquotad  
udp    0   0 *:sunrpc          *:*                   2986/portmap    
udp    0   0 *:ipp            *:*                   6938/cupsd     
udp    0   0 *:1022           *:*                   3392/rpc.mountd   
udp    0   0 *:638            *:*                   3006/rpc.statd

这里要注意 mac上好些参数都不能用,help一下看看

sunjiaying@macbookpro-6d5b:~$ --> netstat -an | grep 6379
tcp4       0      0  127.0.0.1.6379         *.*                    LISTEN