Check Running Processes Like a Power User

Is your Linux system running slow? Or are you just curious about what goes on under the hood? There is a powerful built-in program called “ps” that enables users to view a snapshot of the processes running on a system. We discuss some of the options available to ps, and on conclusion, you should be able to check running processes on your linux computer like an experienced nerd.

Most versions of ps accept BSD style options (which may be grouped and MUST NOT be used with a dash), UNIX style options (which may be grouped and MUST be used with a dash), and GNU long options (which are preceded by two dashes). For this article, we will mix and use multiple option types, with a bias for UNIX styles. For example:

ps -u root u –sort=pid
  • UNIX STYLE -u root specifies user = root
  • BSD STYLE u specifies display of user-oriented format. This is a favourite option since the output format produced by using this contains some very useful information.
  • GNU LONG OPTION –sort specifies process sorting order. In this case, sort by process id (pid)

This article would prefer UNIX options, and uses BSD only for brevity. For example, the command

ps u

Show processes by the current user that are attached to Terminal.

shows the processes by the current user that are attached to a terminal, with a “user-oriented” output format. To get the same output format using UNIX options requires the option

ps -o "user,pid,pcpu,pmem,vsz,rss,tty,stat,bsdstart,bsdtime,args"

This ‘-o’ option’s argument explanations are:

  • user – the effective userid or username of the process
  • pid – the process id
  • pcpu – the cpu time used by the process divided by the time the process has been running
  • pmem – the percentage of the physical memory used by the process
  • vsz – the virtual memory size of the process
  • rss – the non-swapped physical memory the process has used
  • tty – the controlling terminal of the process
  • stat – the process state, determined using process state codes (read the man pages for more info)
  • start_time – the time the command started
  • time – the cumulative cpu time used by the process
  • args – the command used to run the process, along with its arguments. (This is one reason why it’s never a good idea to run a command and enter your password as a command line argument. Any other user on the system can run ps and see the commands you have running, along with all the options and arguments passed).

The convention used henceforth will be the command (ps) followed by the BSD ‘u’ option to specify output formatting, and then the UNIX and GNU options as required.

Running ‘ps’ with no arguments shows a list of processes with the same user ID as the invoker and associated with the same terminal as the invoker.

To show all running processes:

ps u -e

To show all processes by the current user:

ps u -u `whoami`

To show all processes by the current user, but sorted by cpu usage (descending).

NOTE: in the command below, we use ‘-pcpu’ to achieve percentage cpu usage descending. For ascending, use ‘+pcpu’ or just ‘pcpu.’ Consult the man pages for additional sort specifiers. Some common ones are pmem (memory usage), start_time (time the command was run), and time (amount of cpu time used by the command).

ps u -u `whoami` --sort=-pcpu

To show all processes by the current user, in a tree format:

ps u -jH -u `whoami`

Show all processes by current user in tree format.

Using pipes, ps can be combined with head to show the top processes. The next command sorts by memory usage first, then pipes through head to show the processes using the most memory. Head shows the first ten lines by default, so this command would show the top nine processes (The output header is the first line).

Note: “%mem” and “pmem” are synonymous, as are “%cpu” and “pcpu”.

ps u -u `whoami` --sort=-%mem | head

 This command would show the top 9 processes.

To show the same for all processes, regardless of user, use the -e option:

ps u -e --sort=-%mem | head

There are literally hundreds of options (although some are aliases), and we would love to hear about, and see, your favourite usage methods. Please share in the comments below.