How to Use the ps Command in Linux?

Ps Command Linux

Ps command in Linux is used to list the processes that are running. Ps command can be used in a lot of ways to get various kinds of information regarding the processes that are running. We will take a look at some of the important ones.

What is a process in Linux?

A process is simply a program in execution. Every instance of a program is a process. Your browser, music player, text editor are all examples of processes that are running at the same time.

ps is short for ‘Process Status’

Simple ps

ps without any arguments will show the processes that are running in the current session.

$ ps 
Ps Command
Ps Command

We can see four parameters displayed about the running processes :

PIDprocess ID
TTYterminal type
TIMEtotal time the process has been running
CMDname of the command that launches the process

Do you notice something interesting?

The second process is actually the ps command itself. Every command that we run in the terminal is seen as a process by Linux.

Getting more information about the processes

ps command can be used with -u option to show more information about the processes.

$ ps -u
Ps U Command
ps -u Command

Now along with PID, TTY, TIME, COMMAND we have some other information about the processes, such as USER, %CPU,%MEM.

  • %CPU represents the amount of computing power the process is taking.
  • %MEM represents the amount of memory the process is taking up.
  • STAT represents process state.

This format of displaying is known as the BSD style.

Show processes owned by the current user

This command will show all the processes that are owned by the user that is running the ps command.

$ ps -x
Ps X Command
ps -x Command

These are the processes owned by the user (root in this case), these processes need not be running. There is a way to list processes for other users and groups as well, we will cover that later in the article.

Using -u flag with -x flag

ps-ux combines the previous two commands and as you could have guessed, displays more information about all the processes owned by the user.

$ ps -ux
Ps Ux Command
ps ux Command

In fact, -u flag is almost always combined with some other flag to get more information.

Listing all the processes on the system

This command will show all the processes on the system. This will not limit the list of processes to the current user or the running processes.

$ ps -A

or

$ ps -e
Ps A Command
ps -A Command

Print a process tree

The command to print a process tree is

 $ ps -eH 
ps -eH
ps -eH

or

 $ ps -e --forest
ps -e --forest
ps -e –forest

ps -eH shows the processes according to their hierarchy, whereas ps-e –forest displays the processes in ASCII format which prints them out in a tree-like fashion.

Threads

In Linux, a thread is an instance of a program that is under under execution. A single process can have multiple threads.

Using ps you can list out the threads as well.

$ ps -H

This command will display threads as if they were processes.

Ps H Command
ps H Command
$ ps -m

Shows threads after processes.

Ps M Command
ps m Command
$ ps -T

Shows threads with their SPID, which is the thread id and can be same as PID in case there is only one thread.

Ps T Command
ps -T Command

Full format listing and extra full format listing

$ ps -f 

This command lists information about processes in a full format listing manner. Full format listing displays UID, PPID, C and STIME along with TIME, CMD and PID.

ps -f command
ps -f command
$ ps -F

This command lists information about processes in an extra full format listing manner. We can compare this to the previous output and see the additional information that extra full format listing is giving.

ps -F command
ps -F command

As you can see that we are now getting PPID, which is the PID of the parent.

RSS is the real memory usage.

  • RSS is the real memory usage.
  • SZ is the virtual memory usage.
  • STIME is the start time of the process.

This command can be combined with -e and used as ps -eF to get full format information about all the processes.

It’s easy to mistake ps -f and ps -F for the same command.

Identifying process based on PID

You can display process corresponding to a particular PID using :

$ ps -fp [pid] 

This will display the processes with full format information. You can also use ps -Fp [pid] to display processes with extra full format information.

Ps Fp Pid 1
ps -fp pid

You can get multiple processes by mentioning multiple PIDs separated by comma.

$ ps -fp [pid1],[pid2],[pid3] 

Identifying process based on parent PID (PPID)

You can display processes corresponding to a particular PPID using :

$ ps -f --ppid [ppid]
Ps F Ppid 1
Ps -f ppid 1

This shows all the processes with parent ID as 1.

Display process by the command name

Processes can be selected based on command names :

$ ps -C [cmd name]
Ps C
ps -C

Display all processes for a user or a group

To display all processes for a particular user :

$ ps -U [user_name]

To display all processes for a particular group :

$ ps -G [group_name] 

Conclusion :

ps is a very useful command in Linux that can be used to administer processes in Linux. There’s a lot more to learn about ps command, this was just some of the use-case. More learning material can be found on the man page for ps.