# ps

##### 以 PID 查詢

```shell
ps -fp <PID>
ps -fp <PID#1>,<PID#2>,<PID#3> 
```

##### 分析 CPU/Memory 使用

CPU 使用率最高前10排名

```shell
# for CentOS
ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%cpu | head -11

ps aux | head -1; ps aux | sort -rn +2 | head -10
ps -auxf | sort -nr -k 3 | head -10

# for AIX 7.2
ps aux | head -1; ps aux | sort -rn -k 3 | head -10
```

Memory 使用率最高前10排名

```shell
# for CentOS
ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head -11

ps aux | head -1; ps aux | sort -rn +3 | head
ps aux | head -1; ps aux | sort -rn -k 4 | head
ps -auxf | sort -nr -k 4 | head -10

ps -eo cmd,pid,ppid,%mem,%cpu --sort=-%mem | head -n 6

ps aux  | awk '{print $6/1024 " MB\t\t" $11}'  | sort -rn | head -25

```

##### 優先權最高的程序

```shell
ps -eakl | sort -n +6 | head
# or
ps -eal | sort -n -k 7 | head
```

##### 程序以 nice 值排序

```shell
ps -eakl | sort -n +7

ps -eal | sort -n -k 8
```

##### 程序以執行時間排序

```shell
ps vx | head -1;ps vx | grep -v PID | sort -rn +3 | head -10

ps vx | head -1;ps vx | grep -v PID | sort -rn -k 4 | head -10
```

##### 程序以 I/O 排序

```shell
ps vx | head -1; ps vx | grep -v PID | sort -rn +4 | head -10

ps vx | head -1; ps vx | grep -v PID | sort -rn -k 5 | head -10
```

##### 等待中的程序

```shell
ps vg | head -1; ps vg | grep -w wait
```

##### 指定 PID 的啟動時間與總執行時間

```shell
ps -p <PID> -o etime,start
ps -p <PID> -o etimes
```

##### 每個用戶的程序數量

```shell
ps -ef | awk '{print$1}' | sort | uniq -c | sort -nr
```

##### 顯示用戶 vivek 的所有程序

```shell
ps -U vivek -u vivek u
```

##### 指定要顯示的欄位

```shell
ps -eo pid,tid,class,rtprio,ni,pri,psr,pcpu,stat,wchan:14,comm 
ps axo stat,euid,ruid,tty,tpgid,sess,pgrp,ppid,pid,pcpu,comm 
ps -eopid,tt,user,fname,tmout,f,wchan
```

##### 顯示程序 lighttpd 的 pid

```shell
ps -C lighttpd -o pid=
```

##### 顯示 pid XXX 的程序名稱

```shell
ps -p 55977 -o comm=
```