Using ps to list processes (advanced)

nas

The ps program can be used to identify problematic processes, for example you can use it to identify hung processes, cpu busy processes and memory hungry processes. We've already taken a quick look at some basic usage of the ps command. In this tutorial, we'll cover some advanced usage.

Reading time:
2 min

Environment variables

To show all running processes along with environment variables, use:

ps auxe

this can come in handy when trying to determine why your application runs ok on boxA but not boxB (that is env variable affects the running or behaviour of the application).

Custom output using keywords

You can specify which columns to include in the output list.

For example to show the user, virtual memory size,resident physical memory size, overall memory usage and cpu usage for each process on the system, you could use the following:

ps axo user,vsize,rss,pmem,cp

Another example to display program name, command line args, and virtual memory size would look like:

ps axo comm,args,vsize

See the manpage for full list of useful keywords.

Custom output using format specifiers

If you would like to add your own informational text along with the results you can use format specifiers.

Various format specifiers are available for use inside a user defined string. For example, to display the user, pid, command line args, and elapsed time you can use:

ps axo "user=%u , pid=%p , command line args=%a , elapsed time=%t"

See manpage for full list of useful format specifiers.

Sorting by keyword

You can sort the results by keyword if required. For example to sort by resident set size (physical ram being used) use --sort rss:

ps axo comm,args,vsize,rss --sort rss

To sort by start time, you could use:

ps axo comm,args,pmem,pcpu,stackp,stat,state,sz,thcount,cputime,ni,etime,vsize --sort start_time

To sort all processes by elapsed time (without using any custom display format):

ps aux --sort etime

To sort all processes by nice value:

ps aux --sort nice To show in your own format with description, sorted by virtual memory size:

ps axo "Application: %c | CommandlineArgs: %a | PercentageCpu: %C | User: %U | VirtualMemory: %z" --sort vsize

To display showing stack pointer:

ps axo comm,vsize,rss,stackp

Listing thread information

Each process consists of one or more "threads". The ps command can show us information about threads in various ways.

To list all related threads after each process along with signal format information, use:

ps axms

In Linux, each thread is called a ligh weight process (LWP). To list columns for LWP and NLWP (number of threads), use:

ps aux -L

or

ps -eLf

Thank you for reading this article.
Please share if you liked it.