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’
ps without any arguments will show the processes that are running in the current session.
We can see four parameters displayed about the running processes :
|TIME||total time the process has been running|
|CMD||name 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
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
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
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
$ ps -e
Print a process tree
The command to print a process tree is
$ ps -eH
$ 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.
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 -m
Shows threads after processes.
$ 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.
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
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.
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.
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]
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]
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]
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.