Linux中的进程和网络

一、进程和网络

Linux下一切皆文件。

每个正在运行的进程都有一个数值子目录,子目录以进程ID命名。

查看其中一个进程1301的目录就可以看到对应的进程信息、网络信息等

查看进程状态

查看进程网络

为了方便查看可以使用ps、netstat命令查看进程、网络的相关信息。

二、ps命令

2.1、命令解释

ps命令极其强大,使用help参数可以查看对应的使用方法

ubuntu@ubuntu:~$ ps --help

Usage:
 ps [options]

 Try 'ps --help <simple|list|output|threads|misc|all>'
  or 'ps --help <s|l|o|t|m|a>'
 for additional help text.

ubuntu@ubuntu:~$ ps --help all

Usage:
 ps [options]

Basic options:
 -A, -e               all processes
 -a                   all with tty, except session leaders
  a                   all with tty, including other users
 -d                   all except session leaders
 -N, --deselect       negate selection
  r                   only running processes
  T                   all processes on this terminal
  x                   processes without controlling ttys

Selection by list:
 -C <command>         command name
 -G, --Group <gid>    real group id or name
 -g, --group <group>  session or effective group name
 -p, --pid <pid>      process id
     --ppid <pid>     select by parent process id
 -s, --sid <session>  session id
 -t, t, --tty <tty>   terminal
 -u, U, --user <uid>  effective user id or name
 -U, --User <uid>     real user id or name

  selection <arguments> take either:
    comma-separated list e.g. '-u root,nobody' or
    blank-separated list e.g. '-p 123 4567'

Output formats:
 -F                   extra full
 -f                   full-format, including command lines
  f, --forest         ascii art process tree
 -H                   show process hierarchy
 -j                   jobs format
  j                   BSD job control format
 -l                   long format
  l                   BSD long format
 -M, Z                add security data (for SELinux)
 -O <format>          preloaded with default columns
  O <format>          as -O, with BSD personality
 -o, o, --format <format>
                      user defined format
  s                   signal format
  u                   user-oriented format
  v                   virtual memory format
  X                   register format
 -y                   do not show flags, show rrs vs. addr (used with -l)
     --context        display security context (for SELinux)
     --headers        repeat header lines, one per page
     --no-headers     do not print header at all
     --cols, --columns, --width <num>
                      set screen width
     --rows, --lines <num>
                      set screen height

Show threads:
  H                   as if they where processes
 -L                   possibly with LWP and NLWP columns
 -m, m                after processes
 -T                   possibly with SPID column

Miscellaneous options:
 -c                   show scheduling class with -l option
  c                   show true command name
  e                   show the environment after command
  k,    --sort        specify sort order as: [+|-]key[,[+|-]key[,...]]
  L                   list format specifiers
  n                   display numeric uid and wchan
  S,    --cumulative  include some dead child process data
 -y                   do not show flags, show rss (only with -l)
 -V, V, --version     display version information and exit
 -w, w                unlimited output width

2.2、常用的命令

1、查看所有进程

(1)ps -ef

字段解释:

  • UID:用户ID
  • PID:进程ID
  • PPID:父进程ID
  • C:cpu的占用率,百分比
  • STIME:进程启动时间
  • TTY:终端设备,发起该进程的设备识别符号,如果显示“?”说明该进程不是由终端发起
  • TIME:进程的执行时间
  • CMD:进程名称

(2)ps aux

字段解释:

  • USER:创建进程的用户
  • PID:进程ID
  • %CPU:进程占用CPU的百分比
  • %MEM:进程占用物理内存的百分比
  • VSZ:进程占用虚拟内存的大小(单位KB)
  • RSS:进程占用实际物理内存的大小(单位KB)
  • TTY:进程在哪个终端运行
  • STAT:进程状态
  • START:进程开始启动的时间
  • TIME:进程的执行时间
  • COMMAND:调用进程的命令

2、进程参数筛选

(1)想要查看包含python的进程:ps aux|grep python

(2)以内存占用降序的方式查看进程列表:ps aux --sort=-pmem(默认是升序,前面加负号是降序)

(3)以内存占用降序的方式查看前5行进程列表:ps -aux | sort -nk 4 -r | head -5

(4)筛选ubuntu用户的进程列表

3、进程状态

STAT字段表示进程的状态,常见的状态有以下几种:
D:睡眠状态(不可被唤醒),常用于I/O情况。
R:进程正在运行
S:睡眠状态(可被唤醒)
T:停止状态
W:内存交互状态
Z:僵尸进程(进程由于非正常停止或程序编写错误,导致子进程比父进程先结束,而父进程又没有正常回收子进程,使子进程一直在内存中,导致资源浪费。这种情况就是僵尸进程。正常情况下应该是父进程先结束,然后子进程由init接管,init 结束子进程并回收对应的资源。)
<:高优先级
N:低优先级
L:被锁入内存
s:包含子进程
l:多线程
+:位于后台

三、netstat命令

3.1 命令解释

netstat命令极其强大,使用help参数可以查看对应的使用方法

ubuntu@ubuntu:~$ netstat --help
usage: netstat [-vWeenNcCF] [<Af>] -r         netstat {-V|--version|-h|--help}
       netstat [-vWnNcaeol] [<Socket> ...]
       netstat { [-vWeenNac] -i | [-cWnNe] -M | -s }

        -r, --route              display routing table
        -i, --interfaces         display interface table
        -g, --groups             display multicast group memberships
        -s, --statistics         display networking statistics (like SNMP)
        -M, --masquerade         display masqueraded connections

        -v, --verbose            be verbose
        -W, --wide               don't truncate IP addresses
        -n, --numeric            don't resolve names
        --numeric-hosts          don't resolve host names
        --numeric-ports          don't resolve port names
        --numeric-users          don't resolve user names
        -N, --symbolic           resolve hardware names
        -e, --extend             display other/more information
        -p, --programs           display PID/Program name for sockets
        -c, --continuous         continuous listing

        -l, --listening          display listening server sockets
        -a, --all, --listening   display all sockets (default: connected)
        -o, --timers             display timers
        -F, --fib                display Forwarding Information Base (default)
        -C, --cache              display routing cache instead of FIB

  <Socket>={-t|--tcp} {-u|--udp} {-w|--raw} {-x|--unix} --ax25 --ipx --netrom
  <AF>=Use '-6|-4' or '-A <af>' or '--<af>'; default: inet
  List of possible address families (which support routing):
    inet (DARPA Internet) inet6 (IPv6) ax25 (AMPR AX.25) 
    netrom (AMPR NET/ROM) ipx (Novell IPX) ddp (Appletalk DDP) 
    x25 (CCITT X.25)

3.2、常用命令

1、查看所有连接

netstat -antup(t表示TCP、u表示UDP、x表示UNIX,p表示进程信息)

2、查看监听TCP的进程

netstat -anltp(l表示监听)

3、查看路由表

netstat -r

4、筛选端口

辅助使用grep

5、查看UDP协议统计信息

netstat -us(s表示统计信息)

3.3、连接状态

  • LISTEN: 侦听来自远方的TCP端口的连接请求
  • SYN-SENT: 再发送连接请求后等待匹配的连接请求
  • SYN-RECEIVED:再收到和发送一个连接请求后等待对方对连接请求的确认
  • ESTABLISHED: 代表一个打开的连接
  • FIN-WAIT-1: 等待远程TCP连接中断请求,或先前的连接中断请求的确认
  • FIN-WAIT-2: 从远程TCP等待连接中断请求
  • CLOSE-WAIT: 等待从本地用户发来的连接中断请求
  • CLOSING: 等待远程TCP对连接中断的确认
  • LAST-ACK: 等待原来的发向远程TCP的连接中断请求的确认
  • TIME-WAIT: 等待足够的时间以确保远程TCP接收到连接中断请求的确认
  • CLOSED: 没有任何连接状态
  • SYN_RECV表示正在等待处理的请求数;
  • ESTABLISHED表示正常数据传输状态;
  • TIME_WAIT表示处理完毕,等待超时结束的请求数。

四、参考链接

https://blog.csdn.net/xijinno1/article/details/129904487

https://blog.csdn.net/qq_40907977/article/details/89174679

留下评论

您的电子邮箱地址不会被公开。 必填项已用*标注