Linux 常用命令之 PS

常用参数

1
2
3
4
5
6
7
8
9
ps #当前shell下的进程
ps -e 运行的守护进程
ps -f process with full option
ps -l 显示更多进程信息
ps -ef 显示所有终端的进程和守护进程
ps a 显示所有运行在终端的进程
ps x 显示所有不运行在终端的进程
ps aux 和ps -l 相似
ps -u 显示指定用户的进程

ps 命令是 Process Status 的缩写,列出执行 ps 命令那个时刻时系统内进程的快照。是静态的信息,如果想要显示动态的信息,需要使用 top 命令。

直接使用 ps 命令,不加任何参数,显示当前 shell 的进程。

1
2
3
4
gax@ubuntu:~$ ps
PID TTY TIME CMD
2184 pts/4 00:00:00 bash
57019 pts/4 00:00:00 ps

如果想要更多的信息可以使用 ps -l ,或者 ps -f

1
2
3
4
gax@ubuntu:~$ ps -l
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
0 S 1000 2184 2178 0 80 0 - 7362 wait pts/4 00:00:00 bash
0 R 1000 57021 2184 0 80 0 - 7229 - pts/4 00:00:00 ps
1
2
3
4
gax@ubuntu:~$ ps -f
UID PID PPID C STIME TTY TIME CMD
gax 2184 2178 0 13:45 pts/4 00:00:00 bash
gax 57020 2184 0 15:41 pts/4 00:00:00 ps -f

ps -efps aux 命令相似,ps -ef 属于标准语法, ps aux 属于 BSD 风格语法。两者都是查看系统内的所有进程,因为显示比较多,所以可以使用 ps aux | less 查看。当想要查找某一进程时,可以使用 grep 命令。

1
2
3
4
5
gax@ubuntu:~$ ps -ef | grep bash
gax 2184 2178 0 13:45 pts/4 00:00:00 bash
gax 56952 2178 0 15:36 pts/17 00:00:00 bash
gax 56973 2178 0 15:36 pts/18 00:00:00 bash
gax 56994 2184 0 15:37 pts/4 00:00:00 grep --color=auto bash
1
2
3
4
5
gax@ubuntu:~$ ps aux | grep bash
gax 2184 0.0 0.2 29448 2836 pts/4 Ss 13:45 0:00 bash
gax 56952 0.0 0.4 22376 4856 pts/17 Ss+ 15:36 0:00 bash
gax 56973 0.0 0.4 22376 4860 pts/18 Ss+ 15:36 0:00 bash
gax 56998 0.0 0.1 14224 1020 pts/4 S+ 15:38 0:00 grep --color=auto bash

但是 grep 查找的是包含进程名字的所有进程,我们也可以使用 -C 查找确定的进程。因为我同时开启了 3 个 terminal ,所以会显示 3 个 bash

1
2
3
4
5
gax@ubuntu:~$ ps -C bash
PID TTY TIME CMD
2184 pts/4 00:00:00 bash
56952 pts/17 00:00:00 bash
56973 pts/18 00:00:00 bash

需要更多的信息,可以添加 -f

1
2
3
4
5
gax@ubuntu:~$ ps -f -C bash
UID PID PPID C STIME TTY TIME CMD
gax 2184 2178 0 13:45 pts/4 00:00:00 bash
gax 56952 2178 0 15:36 pts/17 00:00:00 bash
gax 56973 2178 0 15:36 pts/18 00:00:00 bash

按 CPU 使用率来排序(降序),升序使用ps aux --sort +pcpu|head

1
2
3
4
5
6
7
8
9
10
11
gax@ubuntu:~$ ps aux --sort -pcpu|head
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 997 0.3 5.6 379584 55928 tty7 Ss+ 13:40 0:34 /usr/lib/xorg/Xorg -core :0 -seat seat0 -auth /var/run/lightdm/root/:0 -nolisten tcp vt7 -novtswitch
gax 1745 0.3 5.2 1172964 51880 ? Ssl 13:40 0:34 compiz
root 861 0.1 0.1 192800 1616 ? Ssl 13:40 0:11 /usr/bin/vmtoolsd
gax 1819 0.1 0.6 509076 6304 ? Sl 13:40 0:13 /usr/bin/vmtoolsd -n vmusr --blockFd 3
gax 2178 0.1 2.7 665736 27816 ? Sl 13:45 0:14 /usr/lib/gnome-terminal/gnome-terminal-server
root 1 0.0 0.5 185168 5204 ? Ss 13:40 0:04 /lib/systemd/systemd --system --deserialize 22
root 2 0.0 0.0 0 0 ? S 13:40 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? S 13:40 0:03 [ksoftirqd/0]
root 5 0.0 0.0 0 0 ? S< 13:40 0:00 [kworker/0:0H]

按内存的使用来排序

1
2
3
4
5
6
7
8
9
10
11
gax@ubuntu:~$ ps aux --sort -pmem|head
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
gax 1829 0.0 6.7 986408 66972 ? Sl 13:40 0:06 /usr/bin/gnome-software --gapplication-service
root 997 0.3 5.6 379584 55928 tty7 Ss+ 13:40 0:34 /usr/lib/xorg/Xorg -core :0 -seat seat0 -auth /var/run/lightdm/root/:0 -nolisten tcp vt7 -novtswitch
gax 1745 0.3 5.2 1172964 51880 ? Ssl 13:40 0:34 compiz
gax 1924 0.0 3.6 814172 36556 ? Sl 13:41 0:00 /usr/lib/evolution/evolution-calendar-factory-subprocess --factory local --bus-name org.gnome.evolution.dataserver.Subprocess.Backend.Calendarx1891x3 --own-path /org/gnome/evolution/dataserver/Subprocess/Backend/Calendar/1891/3
gax 1820 0.0 2.8 867168 28008 ? Sl 13:40 0:05 nautilus -n
gax 2178 0.1 2.7 665736 27816 ? Sl 13:45 0:14 /usr/lib/gnome-terminal/gnome-terminal-server
gax 1891 0.0 2.4 869692 24536 ? Sl 13:41 0:00 /usr/lib/evolution/evolution-calendar-factory
gax 1911 0.0 2.1 821724 21064 ? Sl 13:41 0:00 /usr/lib/evolution/evolution-calendar-factory-subprocess --factory contacts --bus-name org.gnome.evolution.dataserver.Subprocess.Backend.Calendarx1891x2 --own-path /org/gnome/evolution/dataserver/Subprocess/Backend/Calendar/1891/2
root 1894 0.0 1.7 635288 17860 ? Sl 13:41 0:00 /usr/lib/x86_64-linux-gnu/fwupd/fwupd

查看特定进程的线程,使用 -L 参数。L 参数后面是进程的 PID

1
2
3
4
5
6
gax@ubuntu:~$ ps -L 1829
PID LWP TTY STAT TIME COMMAND
1829 1829 ? Sl 0:01 /usr/bin/gnome-software --gapplication-servic
1829 1860 ? Sl 0:00 /usr/bin/gnome-software --gapplication-servic
1829 1861 ? Sl 0:00 /usr/bin/gnome-software --gapplication-servic
1829 1862 ? Sl 0:00 /usr/bin/gnome-software --gapplication-servic

杀死某个进程,强行终止某进程可以使用 kill -9 PID

1
2
3
4
5
6
7
8
9
10
gax@ubuntu:~$ ps -C bash
PID TTY TIME CMD
2184 pts/4 00:00:00 bash
56952 pts/17 00:00:00 bash
56973 pts/18 00:00:00 bash
gax@ubuntu:~$ kill 56952
gax@ubuntu:~$ ps -C bash
PID TTY TIME CMD
2184 pts/4 00:00:00 bash
56973 pts/18 00:00:00 bash

使用 -l 列出全部信号的名称,默认使用 SIGTERM 15

1
2
3
4
5
6
7
8
9
10
11
12
13
14
gax@ubuntu:~$ kill -l
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP
6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1
11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ
26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR
31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3
38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8
43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7
58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2
63) SIGRTMAX-1 64) SIGRTMAX

参考资料

技术|10个重要的Linux ps命令实战

赞赏是对作者最大的支持!
0%