有次一個在使用ps命令時,發現部分用戶顯示的是用戶名,有些用戶顯示的是UID,那是因為用戶名長度超過8位的;也就是說ps命令用戶名列默認只能顯示8位(含8位)的用戶名,超過8位就顯示UID,如何讓長度變大呢?
創建了兩個賬戶,分別是12345678(UID:505)、123456789(UID:506),用ps命令查看進程
- [root@server2 ~]# ps -ef
- UID PID PPID C STIME TTY TIME CMD
- root 1 0 0 02:34 ? 00:00:00 init [3]
- root 2 1 0 02:34 ? 00:00:03 [migration/0]
- root 3 1 0 02:34 ? 00:00:00 [ksoftirqd/0]
- root 4 1 0 02:34 ? 00:00:03 [migration/1]
- root 5 1 0 02:34 ? 00:00:00 [ksoftirqd/1]
- root 6 1 0 02:34 ? 00:00:09 [events/0]
- root 2380 17 0 02:35 ? 00:00:00 [kmpath_handlerd]
- root 2402 17 0 02:35 ? 00:00:00 [kjournald]
- root 2425 4238 0 08:54 ? 00:00:00 sshd: root@pts/2
- root 2429 2425 0 08:54 pts/2 00:00:00 -bash
- oracle 2505 1 0 08:55 ? 00:00:00 ora_cjq0_lanv
- root 2580 4238 0 08:56 ? 00:00:00 sshd: 123456789 [priv]
- 506 2585 2580 0 08:56 ? 00:00:00 sshd: 123456789@pts/3
- 506 2590 2585 0 08:56 pts/3 00:00:00 -bash
- root 2716 4238 0 09:09 ? 00:00:00 sshd: 12345678 [priv]
- 12345678 2723 2716 0 09:09 ? 00:00:00 sshd: 12345678@pts/4
- 12345678 2730 2723 0 09:09 pts/4 00:00:00 -bash
8位用戶名的賬戶能夠顯示完整的用戶名,9位用戶名的賬戶就顯示了UID
通過修改UID列名來調整UID列顯示寬度,列名是9位,那么用戶名就可以是9位,以此類推……
- [root@server2 ~]# ps -o ruser=user9wei9 -e -o pid,ppid,c,stime,tty,time,cmd
- user9wei9 PID PPID C STIME TT TIME CMD
- root 1 0 0 02:34 ? 00:00:00 init [3]
- root 2 1 0 02:34 ? 00:00:03 [migration/0]
- oracle 588 1 0 05:25 ? 00:00:00 ora_q001_lanv
- root 596 17 0 02:35 ? 00:00:00 [kauditd]
- root 2363 4238 0 08:53 ? 00:00:00 sshd: root@pts/1
- root 2425 4238 0 08:54 ? 00:00:00 sshd: root@pts/2
- root 2429 2425 0 08:54 pts/2 00:00:00 -bash
- root 2580 4238 0 08:56 ? 00:00:00 sshd: 123456789 [priv]
- 123456789 2585 2580 0 08:56 ? 00:00:00 sshd: 123456789@pts/3
- 123456789 2590 2585 0 08:56 pts/3 00:00:00 -bash
- root 2716 4238 0 09:09 ? 00:00:00 sshd: 12345678 [priv]
- 12345678 2723 2716 0 09:09 ? 00:00:00 sshd: 12345678@pts/4
- 12345678 2730 2723 0 09:09 pts/4 00:00:00 -bash
- root 2807 2429 0 09:15 pts/2 00:00:00 ps -o ruser=user9wei9 -e -o pid,ppid,c,stime,tty,time,cmd
UID列名被改為user9wei9,長度是9位,這樣就能顯示9位的用戶名了。
如果你需要查看用戶uid,-o uid即可。
ps [選項]
下面對命令選項進行說明:
-e 顯示所有進程。
-f 全格式。
-h 不顯示標題。
-l 長格式。
-w 寬輸出。
a 顯示終端上的所有進程,包括其他用戶的進程。
r 只顯示正在運行的進程。
u 以用戶為主的格式來顯示程序狀況。
x 顯示所有程序,不以終端機來區分。
更多命令:
ps -e -o pid,uname=USERNAME,pcpu=CPU_USAGE,pmem,comm
$ ps -e -o pid,uname,pcpu,pmem,comm
$ ps --ppid 28208
$ ps aux --sort=-pcpu,+pmem
$ ps aux --sort=-pcpu,+pmem | head 5
$ ps -f -u nova,neutron
