PS Command in Linux (List Processes)
Introduction
Before we discuss ps command in Linux (List Processes), let's understand-What is ps
command ?
The ps
command in Linux is used to provide information about currently running processes on a system. It helps users monitor system activity, identify resource-intensive processes, and manage running processes efficiently.
A process is a running instance of a program in Linux. When working on a Linux machine, you may occasionally need to know what processes are currently running.
You may use a variety of commands to get information about running processes, with ps
, pstree
, and top
being the most often used.
This tutorial demonstrates how to use the ps
command to list and display information about presently active processes. We will also address a few FAQs on ps
command in Linux (List Processes).
How to Use ps
Command
The ps
command has the following general syntax:
ps [OPTIONS]
The ps
command takes several distinct types of options for historical and compatibility reasons:
- UNIX style options, preceded by a single dash.
- BSD style options, used without a dash.
- GNU long options, preceded by two dashes.
Different option types can be mixed, however, conflicts can arise in some instances, therefore it's recommended to stick with one option type.
Options for BSD and UNIX can be grouped together.
In its most basic form, ps
will print four columns of information for at least two processes running in the current shell, the shell itself, and the processes running in the shell when the command was invoked when used without any options.
ps
The output contains information about the shell (bash
) and the process (ps
, the command you typed) that is running in this shell:
Output
PID TTY TIME CMD
1809 pts/0 00:00:00 bash
2043 pts/0 00:00:00 ps
PID
, TTY
, TIME
, and CMD
are the names of the four columns.
PID
stands for Process ID. When using theps
command, the process PID is usually the most significant piece of information that the user is looking for. Knowing the PID allows you to kill a faulty process.TTY
- The name of the process's controlling terminal.TIME
- The process's total CPU time, is expressed in minutes and seconds.CMD
- This is the name of the command that started the process.
Because there isn't much information in the output above, it isn't particularly useful. When used with additional parameters, the ps
command becomes quite powerful.
The ps
command supports many options for displaying a specified set of processes and other information about them, although only a few are needed in day-to-day use.
The command ps
is most commonly used with the following options:
BSD form:
ps aux
- The
a
option instructsps
to show all users' processes. Only processes that aren't linked to a terminal and group leader processes aren't displayed. - The letter
u
stands for a user-friendly structure that provides detailed information about processes. - Without a controlling terminal, the
x
option tellsps
to list the processes. Those are primarily background processes that begin at boot time and run in the background.
USER
, PID
, %CPU
, %MEM
, VSZ
, RSS
, STAT
, START
, TTY
, TIME
, and CMD
are among the eleven columns displayed by the command.
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.8 77616 8604 ? Ss 19:47 0:01 /sbin/init
root 2 0.0 0.0 0 0 ? S 19:47 0:00 [kthreadd]
...
PID
, TTY
, TIME
, and CMD
labels have already been discussed. Other labels are explained as follows:
USER
- This is the user who is in charge of the process.%CPU
- The process's CPU utilization.MEM
- The percentage of the process's resident set the size to the machine's physical memory.VSZ
is the process's virtual memory size in KiB.RSS
- The amount of physical memory used by the process.STAT
- The process state code, such asZ
(zombie),S
(sleeping), andR
(running).START
- The start time of the command.
ps
displays a tree view of parent to child processes when the f
option is used:
ps auxf
You can sort the output with the ps
command. To arrange the output by memory consumption, for example, you would use:
ps aux --sort=-%mem
UNIX form:
ps -ef
- The
-e
option tellsps
to show all processes. - The
-f
option stands for full-format listing, which gives you a lot more information about the processes.
UID
, PID
, PPID
, C
, STIME
, TIME
, and CMD
are the eight columns that the command displays.
Output
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 19:47 ? 00:00:01 /sbin/init
root 2 0 0 19:47 ? 00:00:00 [kthreadd]
...
Let's discuss the meaning of the labels that have not yet been explained:
UID
- Same asUSER
, the user who runs the process.PPID
- The parent process's ID.C
- The process CPU utilization, which is the same as%CPU
.STIME
- The same asSTART
, this is the time the command began.
Type the following command, where vega
is the user's name, to see only the processes running as that user:
ps -f -U vega -u vega
User-defined Format
When using the ps
command, the o
option allows you to define which columns are displayed.
For example, if you only want to output information about the PID
and COMMAND
, use one of the following commands:
ps -efo pid,comm
ps auxo pid,comm
Using ps
with Other Commands
Using piping, ps
can be used in conjunction with other commands.
Pipe the output of the ps
command to the less
command one page at a time to show it:
ps -ef | less
grep
can be used to filter the output of the ps
command. To reveal only the processes belonging to the root user, for example, run:
ps -ef | grep root
FAQs on PS Command in Linux
What information does the ps
command display?
The ps
command displays information such as process ID (PID), terminal associated with the process, CPU and memory usage, time of process initiation, and more.
How do I list all processes running in Linux using the ps
command?
To list all processes running on a Linux system, use the ps
command without any options: ps -e
or ps -A
.
What is the difference between ps -e
and ps -A
?
ps -e
and ps -A
are equivalent and both display all processes running on the system. These options ensure that no processes are hidden from the output.
How can I display detailed information about processes using the ps
command?
To display detailed information about processes, use the ps -f
option. It provides a full-format listing that includes additional details such as the parent process, execution time, and more.
How can I sort processes by CPU usage with the ps
command?
To sort processes by CPU usage, use the ps
command along with the --sort
option. For example, to sort by CPU usage in descending order, use ps --sort=-%cpu
.
Can the ps
command display processes in a tree-like structure?
Yes, the ps
command can display processes in a tree-like structure using the ps -f --forest
or ps f
option. This format displays the parent-child relationships between processes.
How can I continuously monitor process activity with the ps
command?
To continuously monitor process activity, use the ps
command along with the watch
command. For example, watch -n 1 ps aux
displays an updated view of all processes every second.
Conclusion
When troubleshooting difficulties on Linux systems, the ps
command is one of the most regularly used commands. It offers a lot of options, but most people use ps aux
or ps -ef
to collect information on running processes.
Type man ps
in your terminal to learn more about ps
.
If you have any queries, please leave a comment below and we’ll be happy to respond to them.