估計linux下常用的命令裡ps算是使用率較高的了,今天來翻譯一下這篇ps相關的文章
Linux ps command
The ps command on linux is one of the most basic commands for viewing the processes running on the system. It provides a snapshot of the current processes along with detailed information like user id, cpu usage, memory usage, command name etc. It does not display data in real time like top or htop commands. But even though being simpler in features and output it is still an essential process management/monitoring tool that every linux newbie should know about and learn well.
PS命令是linux下最基本的命令,它用來查看系統中正在啟動並執行進程。它提供了當前的線程以及相關的詳細資料,例如使用者id,cpu佔用,記憶體佔用,啟動的命令等。它和top或者htop不一樣的地方在於它不是即時的。但它的輸出簡單,所以它是每一個營運人員需要掌握的命令
In this post we are going to revise the basics of using the ps command to check the processes and filter and sort them in different ways to suit better.
在本文中,我們將講一下如何使用PS來查看進城以及相關的過濾以及排序
Note on syntax
PS的文法:
The ps command comes with an unusual set of 2 syntax styles. That is BSD and UNIX both. New users are often confused with and mis-interpret the two styles. So here is some basic info to get it clear before moving on.
Ps命令又兩種文法格式,分別是BSD風格和UNIX風格,新使用者常常感覺到困擾。所以這裡我們簡單說一下基本的知識然後繼續
Note : "ps aux" is not the same as "ps -aux". For example "-u" is used to show process of that user. But "u" means show detailed information.
1
Note : "ps aux" is not the same as "ps -aux". For example "-u" is used to show process of that user. But "u" means show detailed information.
注意: ps aux 和ps -aux 並不相同,舉例來說 -u 是顯示進程的擁有著,但是u 表示顯示詳細資料
BSD style – The options in bsd style syntax are not preceded with a dash.
BSD: 就是後邊沒有-,直接跟選項
ps aux
UNIX/LINUX style – The options in linux style syntax are preceded by a dash as usual.
UNIX/LINUX:就是後邊跟著 – 然後跟選項
ps -ef
It is okay to mix both the syntax styles on linux systems. For example "ps ax -f".
But in this post we shall mostly focus on the unix style syntax.
同時使用兩個風格事可以的,例如 ps ax -f 但是我們本文章還是主要來說unix風格
1
2
3
It is okay to mix both the syntax styles on linux systems. For example "ps ax -f".
But in this post we shall mostly focus on the unix style syntax.
同時使用兩個風格事可以的,例如 ps ax -f 但是我們本文章還是主要來說unix風格
How to use ps command
1. Display all processes
顯示所有進程
The following command will give a full list of processes
如下的兩個命令會顯示所有的進程:
$ ps ax
$ ps -ef
Pipe the output to “less” to make it scrollable.
使用 管道連結 less命令後我們可以使用捲軸來查看具體資訊
Use the “u” option or “-f” option to display detailed information about the processes
使用u 或者-f 來顯示詳細資料
$ ps aux
$ ps -ef -f
Why is the USER column not displaying my username, but showing others like root, www-data etc ?
為什麼user 欄沒有顯示我的使用者名稱,但是卻顯示了別人的,例如root ,www-data
For all usernames (including yours) if the length is greater than 8 characters then ps will fall back to show only the UID instead of username.
對於所有的使用者,如果字串>8,那麼ps就只會顯示你的UID
1
2
3
4
5
Why is the USER column not displaying my username, but showing others like root, www-data etc ?
為什麼user 欄沒有顯示我的使用者名稱,但是卻顯示了別人的,例如root ,www-data
For all usernames (including yours) if the length is greater than 8 characters then ps will fall back to show only the UID instead of username.
對於所有的使用者,如果字串>8,那麼ps就只會顯示你的UID
2. Display process by user
顯示某使用者的所有進程
To filter the processes by the owning user use the “-u” option followed by the username. Multiple usernames can be provided separated by a comma.
來單獨顯示某使用者擁有的進程 -u “username”,可以輸入過個使用者名稱,通過”,”分割
$ ps -f -u www-data
UID PID PPID C STIME TTY TIME CMD
www-data 1329 1328 0 09:32 ? 00:00:00 nginx: worker process
www-data 1330 1328 0 09:32 ? 00:00:00 nginx: worker process
www-data 1332 1328 0 09:32 ? 00:00:00 nginx: worker process
www-data 1377 1372 0 09:32 ? 00:00:00 php-fpm: pool a.localhost
www-data 1378 1372 0 09:32 ? 00:00:00 php-fpm: pool a.localhost
www-data 4524 2359 0 10:03 ? 00:00:00 /usr/sbin/apache2 -k start
www-data 4527 2359 0 10:03 ? 00:00:00 /usr/sbin/apache2 -k start
www-data 4528 2359 0 10:03 ? 00:00:00 /usr/sbin/apache2 -k start
3. Show process by name or process id
顯示指定名稱的進程(或者進程id)
To search the processes by their name or command use the “-C” option followed by the search term.
我們可以通過-C 來尋找具體名稱的進程
$ ps -C apache2
PID TTY TIME CMD
2359 ? 00:00:00 apache2
4524 ? 00:00:00 apache2
4525 ? 00:00:00 apache2
...
To display processes by process id, use the “-p” option and provides the process ids separated by comma.
可以通過-p 選項來顯示具體id大進程資訊,也可以通過“,”來一次傳入多個
$ ps -f -p 3150,7298,6544
The “-C” must be provided with the exact process name and it cannot actually search with a partial name or wildcard. To search the process list more flexibly, the usual grep command has to be used
-C 選項要求我們要輸入完全符合的進程名稱,所以我們更傾向於使用grep來進行篩選
$ ps -ef | grep apache
4. Sort process by cpu or memory usage
按照cpu或者記憶體進行排序
System administrators often want to find out processes that are consuming lots of memory or CPU. The sort option will sort the process list based on a particular field or parameter.
系統管理員常常需要尋找到消耗CPU或者記憶體的罪魁禍首,排序選項讓我們可以按照指定的參數進行排序
Multiple fields can be specified with the “–sort” option separated by a comma. Additionally the fields can be prefixed with a “-” or “+” symbol indicating descending or ascending sort respectively. There are lots of parameters on which the process list can be sorted. Check the man page for the complete list.
多個參數的時候我們可以使用 –sort來進行排序,通過“,”來輸入,我們可以在排序的參數前邊家“-”或者“+”,表示整序或者倒序進行排序,更多的選項可以通過man手冊來尋找
$ ps aux --sort=-pcpu,+pmem
Display the top 5 processes consuming most of the cpu.
顯示佔用cpu最高的5個進程
$ ps aux --sort=-pcpu | head -5
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 2.6 0.7 51396 7644 ? Ss 02:02 0:03 /usr/lib/systemd/systemd --switched-root --system --deserialize 23
root 1249 2.6 3.0 355800 30896 tty1 Rsl+ 02:02 0:02 /usr/bin/X -background none :0 vt01 -nolisten tcp
root 508 2.4 1.6 248488 16776 ? Ss 02:02 0:03 /usr/bin/python /usr/sbin/firewalld --nofork
silver 1525 2.1 2.3 448568 24392 ? S 02:03 0:01 /usr/bin/python /usr/share/system-config-printer/applet.py
5. Display process hierarchy in a tree style
通過進程樹的形式來顯示進程
Many processes are actually forked out of some parent process, and knowing this parent child relationship is often helpful. The ‘–forest’ option will construct an ascii art style tree view of the process hierarchy.
許多進程其實通過許多父進程fork出來的,並且很多時候知道這些父子關係很又用, –forest 可以實現這個目的
The following command will search for processes by the name apache2 and construct a tree and display detailed information.
如下的命令會尋找apache2進程,並且通過tree的形式來顯示資訊
$ ps -f --forest -C apache2
UID PID PPID C STIME TTY TIME CMD
root 2359 1 0 09:32 ? 00:00:00 /usr/sbin/apache2 -k start
www-data 4524 2359 0 10:03 ? 00:00:00 \_ /usr/sbin/apache2 -k start
www-data 4525 2359 0 10:03 ? 00:00:00 \_ /usr/sbin/apache2 -k start
www-data 4526 2359 0 10:03 ? 00:00:00 \_ /usr/sbin/apache2 -k start
www-data 4527 2359 0 10:03 ? 00:00:00 \_ /usr/sbin/apache2 -k start
www-data 4528 2359 0 10:03 ? 00:00:00 \_ /usr/sbin/apache2 -k start
Try not to use any sorting with the tree style display, as they both effect the order of display in different ways.
注意:不用在使用tree形式顯示資訊的時候使用排序選項,因為這樣會亂套
Try not to use any sorting with the tree style display, as they both effect the order of display in different ways.
注意:不用在使用tree形式顯示資訊的時候使用排序選項,因為這樣會亂套
6. Display child processes of a parent process
顯示自進程:
Here is an example of finding all forked apache processes.
下面的例子是尋找所有apache的子進程:
$ ps -o pid,uname,comm -C apache2
PID USER COMMAND
2359 root apache2
4524 www-data apache2
4525 www-data apache2
4526 www-data apache2
4527 www-data apache2
4528 www-data apache2
The first process that is owned by root is the main apache2 process and all other apache2 processes have been forked out of this main process. The next command lists all child apache2 processes using the pid of the main apache2 process
第一個進程是root擁有的,所以事你主進程,然後所有其它的apache2進程都是通過這個進程fork出來的,下面的命令顯示了所有apache2的進程通過主進程的id
$ ps --ppid 2359
PID TTY TIME CMD
4524 ? 00:00:00 apache2
4525 ? 00:00:00 apache2
4526 ? 00:00:00 apache2
4527 ? 00:00:00 apache2
4528 ? 00:00:00 apache2
7. Display threads of a process
顯示進程下的所有線程
The “-L” option will display the threads along with the processes. It can be used to display all threads of a particular process or all processes.
-L選項可以顯示所有的線程,可以用來顯示指定進程下的線程
The following command shall display all the threads owned by the process with id 3150.
下面的命令會顯示進程3150下所有的線程
$ ps -p 3150 -L
8. Change the columns to display
指定要顯示的選項
The ps command can be configured to show a selected list of columns only. There are a large number of columns to to show and the full list is available in the man pages.
ps命令可以指定我們要顯示的資料欄,ps有很多欄目可以顯示,具體的可以參考man 手冊
The following command shows only the pid, username, cpu, memory and command columns.
下面的命令只會顯示 pid ,username,cpu,memory ,和comm
$ ps -e -o pid,uname,pcpu,pmem,comm
It is possible to rename the column labels
我們也可以重新命名這些欄目
$ ps -e -o pid,uname=USERNAME,pcpu=CPU_USAGE,pmem,comm
PID USERNAME CPU_USAGE %MEM COMMAND
1 root 0.0 0.0 init
2 root 0.0 0.0 kthreadd
3 root 0.0 0.0 ksoftirqd/0
4 root 0.0 0.0 kworker/0:0
5 root 0.0 0.0 kworker/0:0H
7 root 0.0 0.0 migration/0
8 root 0.0 0.0 rcu_bh
9 root 0.0 0.0 rcuob/0
10 root 0.0 0.0 rcuob/1
9. Display elapsed time of processes
顯示進程的已耗用時間
The elapsed time indicates, how long the process has been running for. The column for elapsed time is not shown by default, and has to be brought in using the “-o” option
預設,進程的已耗用時間是預設不顯示的,如果想查看,必須使用-o選項
$ ps -e -o pid,comm,etime
10. Turn ps into an realtime process viewer
將ps轉換成即時顯示的進程監控器
As usual, the watch command can be used to turn ps into a realtime process reporter. Simple example is like this
通常情況下,我們可以使用watch命令來讓我們的ps資訊即時顯示,簡單的例如下:
$ watch -n 1 'ps -e -o pid,uname,cmd,pmem,pcpu --sort=-pmem,-pcpu | head -15'
輸出結果如下:
Every 1.0s: ps -e -o pid,uname,cmd,pmem,pcpu --... Sun Dec 1 18:16:08 2013
PID USER CMD %MEM %CPU
3800 1000 /opt/google/chrome/chrome - 4.6 1.4
7492 1000 /opt/google/chrome/chrome - 2.7 1.4
3150 1000 /opt/google/chrome/chrome 2.7 2.5
3824 1000 /opt/google/chrome/chrome - 2.6 0.6
3936 1000 /opt/google/chrome/chrome - 2.4 1.6
2936 1000 /usr/bin/plasma-desktop 2.3 0.2
9666 1000 /opt/google/chrome/chrome - 2.1 0.8
3842 1000 /opt/google/chrome/chrome - 2.1 0.8
4739 1000 /opt/google/chrome/chrome - 1.8 1.0
3930 1000 /opt/google/chrome/chrome - 1.7 1.0
3911 1000 /opt/google/chrome/chrome - 1.6 0.6
3645 1000 /opt/google/chrome/chrome - 1.5 0.4
3677 1000 /opt/google/chrome/chrome - 1.5 0.4
3639 1000 /opt/google/chrome/chrome - 1.4 0.4
The output would be updated every 1 second to refresh the stats. However do not think that this is similar to top.
輸出結果每1妙更新一次,這個和top還是有區別的
you would notice that the output of top/htop command changes much more frequently compared to the above ps command.
This is because the top output sorts on a value that is a mix of cpu usage and memory usage. But the above ps command sorts in a more simpler manner, taking 1 column at a time (like school maths). So it would not update rapidly like top.
你會發現 top/htop命令的重新整理頻率比ps的高很多
這是因為top命令的輸出是按照cpu和記憶體綜合排序進行輸出的,但是我們上面的ps命令是一個簡單的輸出,所以它不會和top一樣