Home Linux 6 Examples of Basic Use of PS Command in Linux

6 Examples of Basic Use of PS Command in Linux

There are a bunch of tools available in Linux to see the running processes. One of which is ps. It is a standard tool used by most system administrators to check the running processes on Linux. Actually, ps is not only available for Linux. BSD also uses the same tool as a the standard tool to check the running processes. The use of ps is a bit different between Linux and BSD. This article will focus on Linux.

ps itself can be used to check the running processes, along with detailed information regarding each process like process ID, user ID, memory usage, CPU usage and so on. The output of the ps command in Linux can be vary depending on the options you use.

In Linux, you can use the command of ps with following syntax:

ps -[option]

As usual, you can also read the manual page of ps by typing man pson the terminal. But for now, you can learn how to use the command of ps from the following examples.

1. Display all processes

For the very basic use, you will probably want to use ps to check all of the current processes on your Linux system. If so, you can use the option of ef

ps -ef

You will probably have a long output. If you work with a small computer monitor, you can combine the ps command with more so you can see all of the running processes from top to bottom more comfortably.

ps -ef | more

2. Sort process by CPU usage

If you are a Linux administrator, you might always wondering which processes on your Linux system that use most CPU resource. The command above will display the running processes by process ID. You can sort the output by CPU usage by adding the --sortparameter. Here is the example.

ps -ef --sort=-pcpu

Or, if you want to see the percentage use of the CPU you can also use the option combination of auxinstead of ef, which is basically a BSD style.

ps aux --sort=-pcpu

3. Sort processes by memory usage

You can modify the command above by changing the parameter of -pcupwith -pmemto sort the out of ps by memory usage. It would like this one.

ps -ef --sort=-pmem

Or the following if you want to use the BSD style.

ps aux --sort=-pmem

4. Display processes by user

Your Linux system consists of several users, including the ones provided by system like root, apache2 or mysql. To filter the output of ps by username, you can use the option of -ufollowed by the available usernames. You can check the running processes of multiple users at once by separating them with a comma.

ps -f -u root

For multiple users.

ps -f -u cap,root

5. Display threads of a process

A process can consists of threads. To display the threads of the a process you can use the option of L. Here is the example.

ps -p 2604 -L

On the command above, the digits of 2604 is the process ID.

6. Set the custom columns

If you run the ps command with the option of efto display all of the running processes, you will get the output with the following column structure.

You can modify the column structure if you want it. For instance, you probably want to display the memory usage and remove the PPID. To do so, you can run the ps command followed by the option of o. There are several parameters you can add after the option of o. Here is the example use of this option.

ps -e -o pid,uname,pcpu,pmem,comm

The examples above are just basic use of the ps command in Linux. There are many other combinations you can use. Simply read the manual page of the command by typing man pson the terminal.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

MOST-READ THIS WEEK