The ps
command, short for Process Status, is a command line utility that is used to display or view information related to the processes running in a Linux system.
As we all know, Linux is a multitasking and multiprocessing system. Therefore, multiple processes can run concurrently without affecting each other.
The ps command lists current running processes alongside their PIDs and other attributes. In this guide, we are going to focus on ps command usage.
It retrieves information about the processes from virtual files which are located in the /proc file system
ps command without arguments
The ps command without arguments lists the running processes in the current shell
1 |
ps |
Output
The output consists of four columns
PID
– This is the unique process ID
TTY
– This is the typeof terminal that the user is logged in to
TIME
– This is the time in minutes and seconds that the process has been running
CMD
– The command that launched the process
Viewing all the running processes in different formats
To have a glance at all the running processes, execute the command below
ps -A
Output
or
ps -e
Output
View processes associated with the terminal
To view processes associated with the terminal
View processes not associated with terminal
To view all processes with the exception of processes associated with the terminal and session leaders
execute
ps -a
A session leader is a process that starts other processes
Output
Show all current running processes
To view all current processes execute
1 |
ps -ax |
Output
-a
flag stands for all processes
-x
will display all processes even those not associated with the current tty
Display all processes in BSD format
If you wish to display processes in BSD format, execute
1 |
ps au |
OR
1 |
ps aux |
To perform full format listing
To view a full format listing run
1 |
ps -ef |
OR
1 |
ps -eF |
Output
Filter processes according to the user
If you wish to list processes associated with a specific user, use the -u
flag as shown
1 |
ps -u user |
For example
1 |
ps -u jamie |
Output
Filter process by thread process
If you wish to know the thread of a particular process, make use of the -L
flag followed by the PID
For example
1 |
ps -L 4264 |
Show every process running as root
Sometimes, you may want to reveal all processes run by the root user. To achieve this run
1 |
ps -U root -u root |
Output
Display group processes
If you wish to list all processes associated by a certain group run
1 |
ps -fG group_name |
Or
1 |
ps -fG groupID |
For example
1 |
ps -fG root |
Output
Search Process PID
Chances are that usually don’t know the PID to a process. You can search the PID of a process by
running
1 |
ps -C process_name |
For example
1 |
ps -C bash |
Output
Listing processes by PID
You can display processes by their PID as shown
1 |
ps -fp PID |
For example
1 |
ps -fp 1294 |
Output
To display process hierarchy in a tree diagram
Usually, most processes are forked from parent processes. Getting to know this parent-child relationship
can come in handy. The command below searches for processes going by the name apache2
1 |
ps -f --forest -C bash |
Output
Display child processes of a parent process</spa n>
For example, If you wish to display all forked processes belonging to apache, execute
1 |
ps -o pid,uname,comm -C bash |
Output
The first process, which is owned by root is the main apache2 process and the rest of the processes
have been forked from this main process
To display all the child apache2 processes using the pid of the main apache2 process execute
1 |
ps --ppid PID no. |
For example
1 |
ps --ppid 1294 |
Display process threads
The ps command can be used to view threads along with the processes.
The command below displays all the threads owned by the process with PID pid_no
1 |
ps -p pid_no -L |
For example
1 |
ps -p 1294 -L |
Display a selected list of columns
You can use the ps command to display only the columns you need.
For example ,
1 |
ps -e -o pid,uname,pcpu,pmem,comm |
The command above will only display the PID, Username, CPU, memory and command columns
Output
Renaming column labels
To rename column labels execute the command below
1 |
ps -e -o pid=PID,uname=USERNAME,pcpu=CPU_USAGE,pmem=%MEM,comm=COMMAND |
Output
Display elapsed time of processes
Elapsed time refers to how long the process has been running for
1 |
ps -e -o pid,comm,etime |
Output
The -o option enables the column for elapsed time
Using ps command with grep
the ps command can be used with grep command to search for a particular process
For example
1 |
ps -ef | grep systemd |
Output