ps command is a simple use

Ten simple uses of ps command

Annotation

ps commands have two different syntax styles - BSD and UNIX. Novices often misunderstand these two forms, so we need to make a simple explanation here:

ps aux is different from ps aux. For example, the - U parameter indicates the uid of the printing process, while the parameter u indicates the details of the display process.

  • BSD format - parameters are not preceded by-
$ ps aux
  • UNIX/LINUX format -- parameters are usually preceded by-
$ ps -aux

The two formats are allowed to be mixed, such as PS ax-f, but this article focuses on UNIX style.

1. View all processes

$ ps ax
$ ps -e

Add the - f or u parameter to display the process details

$ ps aux
$ ps -ef

Note: when the user name exceeds 8 characters, it is displayed in uid; when it is less than or equal to 8 characters, it is displayed in user name.

2. Display the process of a user

$ ps -f -u nova,neutron

3. Display process by process name or process id

$ ps -C neutron-server
# -C can only search for processes named neutron server (exact match), and cannot use partial search or wildcards. For example, ps -C neutron will not display neutron server processes. So ps-ef | grep neutron is used for partial matching

$ ps -f -p 3150,7298,6544

4. Using cpu or memory usage to sort processes

$ ps aux --sort=-pcpu,+pmem
# First, sort by cpu usage. If the cpu usage is the same, sort by memory usage.
$ ps aux --sort=-pcpu,+pmem | head 5

5. Display ASCII process tree

$ ps -ef --forest
$ ps -f --forest -C neutron-server

6. Show child processes of parent process

$ ps -o pid,uname,comm -C neutron-server
28208 hebin    neutron-server
28221 hebin    neutron-server
28222 hebin    neutron-server
28223 hebin    neutron-server
28224 hebin    neutron-server
28225 hebin    neutron-server

$ ps --ppid 28208
 PID TTY          TIME CMD
28221 pts/12   00:00:06 neutron-server
28222 pts/12   00:00:06 neutron-server
28223 pts/12   00:01:02 neutron-server
28224 pts/12   00:00:52 neutron-server
28225 pts/12   00:00:28 neutron-server

7. Show threads of the process

$ ps -ef | grep rock-mon
root     14447     1  0 12:47 ?        00:02:01 /usr/bin/python /usr/bin/rock-mon
root     32279 31077  0 23:03 pts/0    00:00:00 grep --color=auto rock-mon

$ ps -p 14447 -L
  PID   LWP TTY          TIME CMD
14447 14447 ?        00:00:00 rock-mon
14447 14480 ?        00:00:09 rock-mon
14447 14483 ?        00:00:00 rock-mon
14447   597 ?        00:00:00 rock-mon
14447   759 ?        00:00:00 rock-mon

8. Change the displayed field

$ ps -e -o pid,uname,pcpu,pmem,comm
$ ps -e -o pid,uname=USERNAME,pcpu=CPU_USAGE,pmem,comm

9. Display the running time of the process

$ ps -e -o pid,comm,etime

10. Display process information in real time with ps command

$ watch -n 1 'ps -e -o pid,uname,cmd,pmem,pcpu --sort=-pmem,-pcpu | head -15'

Tags: Unix Linux less ascii

Posted on Thu, 02 Apr 2020 05:41:13 -0700 by Prodigal Son