Introduction

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:

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

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 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.

Some ‘PS’ Options

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: To show all processes by the current user: 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). To show all processes by the current user, in a 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”.

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

Conclusion

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.