ps
Practical ps command examples for Linux: list running processes, display process trees, find processes by user, analyze CPU and memory usage, sort output, and inspect process start times
This ps supports AIX format descriptors, which work somewhat like the formatting codes of printf(1) and printf(3). The NORMAL codes are described in the next section.
| CODE | NORMAL | HEADER |
|---|---|---|
| %C | pcpu | %CPU |
| %G | group | GROUP |
| %P | ppid | PPID |
| %U | user | USER |
| %a | args | COMMAND |
| %c | comm | COMMAND |
| %g | rgroup | RGROUP |
| %n | nice | NI |
| %p | pid | PID |
| %r | pgid | PGID |
| %t | etime | ELAPSED |
| %u | ruser | RUSER |
| %x | time | TIME |
| %y | tty | TTY |
| %z | vsz | VSZ |
Top 20 processes by memory usage (human-readable)
ps -eo size,pid,user,comm --sort -size | awk '{ hr=$1/1024 ; printf("%13.2f Mb ",hr) } { for ( x=4 ; x<=NF ; x++ ) { printf("%s ",$x) } print "" }'|head -n20
Display elapsed time of running proceses
ps -e -o pid,comm,etime
List the process tree
ps -e --forest
ps -eo user,pid,%cpu,%mem,comm --sort=-%mem,-%cpu|head -n10
ps -eo user,pid,%cpu,%mem,comm --sort=-%mem,-%cpu
ps -o pid,%mem,%cpu,user,group --sort=-%mem
Show mem and cpu usage in percentage only
ps -eo pmem,pcpu,comm --sort=-pmem | head -25
Show mem usage in percentage only
ps -eo comm,pmem --sort=-pmem | head -25
Show cpu usage in percentage only
ps -eo comm,pcpu --sort -pcpu | head -5
Show the full process tree with resource usage
ps uaxf
Show all processes
ps -A
Show all processes with tty, exempt session leaders
ps -a
Display all processes without controlling ttys
ps -T
Show the environment after command
ps -e
Tree view of processes
ps -ejH
Display a detailed process tree
ps -e --forest
Find all processes by a specific user and sort by memory usage
ps -u <username> -o pid,ppid,cmd,%mem,%cpu --sort=-%mem|head -n20
Find Processes Using More Than 1GB of Memory
ps -e -o pid,ppid,cmd,%mem,%cpu --sort=-%mem | awk '$4 > 10.0'
How much memory and cpy package consuming
ps -C <package> -o %mem=,%cpu=,comm= |
awk '{sub(/\..*/,"%",$1); sub(/\..*/,"%",$2);
printf "%s %s %s\n",$1,$2,$3}' |
sort -Vr
How much memory a package consuming
ps -C chrome -o %mem=,pid=,comm= |
awk '{sub(/\..*/,"%",$1); printf "%s\t%s\t%s\n",$1,$2,$3}' |
sort -Vr
How much cpu a package consuming
ps -C chrome -o %cpu=,pid=,comm= |
awk '{sub(/\..*/,"%",$1);
printf "%s\t%s\t%s\n",$1,$2,$3}' |
sort -Vr
Find the process you are looking for
ps -C package_name
Display the top ten running processes - sorted by memory usage
ps aux | sort -nk +4 | tail
Processes per user counter
ps hax -o user | sort | uniq -c
Find processes running longer than 7 days
ps -eo pid,user,etimes,cmd | awk '$3 > 604800 {print}'
Discover the process start time
ps -eo pid,lstart,cmd
Show a 4-way scrollable process tree with full details
ps awwfux | less -S
Processes per user counter
ps hax -o user | sort | uniq -c
Grep processes list avoiding the grep itself
ps axu | grep [c]hrome
Sort all running processes by their memory and cpu usage
ps aux --sort=%mem,%cpu
Discover the process start time
bash ps -eo pid,lstart,cmd
Display the top ten running processes - sorted by memory usage
ps aux --sort -rss | head
Listing running X server pocesses
ps -fC X
Print how long time a command has been running
ps -o etime= -p 82486
A more simple way to see how long time a command has running
ps -o etime= -p $(pgrep -f command_name)
See elapsed time by command name
This means the command has been running for 18 minutes and 40 seconds
ps -eo pid,etime,cmd | grep '[m]ake'
60300 18:40 make -j5 menuconfig
List processes by top memory usage
ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head
Kill all process of a program
kill -9 $(ps aux | grep 'program_name' | awk '{print $2}')
Open as many programs as we’d normally use simultaneously and then count them in the terminal
ps aux -L | cut --delimiter=" " --fields=1 | sort | uniq --count | sort --numeric-sort | tail --lines=1
To find out what´s causing a high load in /proc/loadavg or uptime we can use
$ ps -eo stat,comm | grep '^D'
D< kworker/u17:5+i915_flip
D< kworker/u17:0+i915_flip
D< kworker/u17:3+i915_flip
Counting running processes by commandn name
ps -eo comm --sort=comm | uniq -c | sort -nr
Analyzing chrome process types and counts
ps -C chrome -o pid,ppid,cmd --sort=ppid | grep -- '--type=' | sort | uniq -c