The ps command in Linux is used to display a snapshot of the currently running processes on the system. It stands for “process status”. It provides detailed information about the active processes, including their process IDs (PIDs), terminal associations, and resource usage.
The ps command accepts several types of options:
- UNIX options: These options must be preceded by a dash and may be grouped together.
- BSD options: These options must not be used with a dash and may be grouped together.
- GNU long options: These options must be preceded by two dashes.
The ps command displays the process ID (PID), the terminal associated with the process (TTY), the cumulated CPU time in [DD-]hh:mm:ss format (TIME), and the executable name (CMD). The output of the ps command is unsorted by default.
Syntax:
The syntax for the ps command is as follows:
ps [options]
Common Usage and Options:
- -a: Provides information on all processes except for process group leaders and those not associated with a terminal.
- -A: Lists details for all processes; similar to -e.
- -c: Prints data in a format reflecting scheduler properties, influencing the output when used with -f and -l.
- -d: Shows information for all processes except session leaders.
- -e: Displays information for every currently running process.
- -f: Generates a full listing of processes.
- -j: Prints session ID and process group ID details.
- -l: Generates a long-format listing with detailed process information.
- -L: Presents information about each light weight process (lwp) within selected processes.
- -P: Includes the processor number (if bound) under an additional column header ‘PSR’.
- -y: The -y option modifies the -l option to omit the F and ADDR columns and include an RSS column showing the resident set size in kilobytes instead of pages.
- -g grplist: Lists data for processes associated with group leader IDs in ‘grplist’.
- -n namelist: Specify the name of an alternative system namelist file in place of the default. This option is accepted for compatibility, but is ignored.
- -o format: Prints information according to the specified format. Multiple -o options can be used to concatenate format specifications.
- -p proclist: Displays information for processes whose IDs are listed in ‘proclist’.
- -s sidlist: Shows data on session leaders with IDs in ‘sidlist’.
- -t term: Lists process information associated with a specified terminal identifier.
- -u uidlist: Displays data for processes associated with effective user ID numbers or login names in ‘uidlist’.
- -U uidlist: Lists information for processes associated with real user ID numbers or login names in ‘uidlist’.
- -G gidlist: Displays details for processes with real group ID numbers in ‘gidlist’.
ps Command Examples in Linux:
Example 1: List processes in the current shell (ps)
$ ps
This command displays a snapshot of the active processes within the current shell. The output includes columns indicating the Process ID (PID), associated terminal (TTY), cumulative CPU time used (TIME), and command executed (CMD).
Output:
PID TTY TIME CMD 1747 pts/0 00:00:00 bash 2793 pts/0 00:00:00 ps
Example 2: List every process running currently on the system (ps -ef OR ps -aux)
$ ps -ef
This command provides details for all currently active processes within the system.
Output:
UID PID PPID C STIME TTY TIME CMD root 1 0 0 13:28 ? 00:00:01 /sbin/init root 2 0 0 13:28 ? 00:00:00 [kthreadd] root 3 2 0 13:28 ? 00:00:00 [ksoftirqd/0] root 4 2 0 13:28 ? 00:00:00 [kworker/0:0] root 6 2 0 13:28 ? 00:00:00 [migration/0] root 7 2 0 13:28 ? 00:00:00 [watchdog/0] root 8 2 0 13:28 ? 00:00:00 [migration/1] root 9 2 0 13:28 ? 00:00:00 [kworker/1:0] root 10 2 0 13:28 ? 00:00:00 [ksoftirqd/1] root 12 2 0 13:28 ? 00:00:00 [watchdog/1] whoopsie 1096 1 0 13:29 ? 00:00:00 whoopsie mysql 1103 1 0 13:29 ? 00:00:00 /usr/sbin/mysqld root 1117 1 0 13:29 ? 00:00:00 cron daemon 1118 1 0 13:29 ? 00:00:00 atd root 1195 2 0 13:29 ? 00:00:00 [krfcommd] ...... user_name 4857 4676 0 11:58 pts/3 00:00:00 ps -ef
- -e: Displays all current processes.
- -f: Presents a full format output with a comprehensive set of attributes.
- ps -aux: Alternate command primarily used for BSD machines, similar to ps -ef.
- ps -e: Lists all processes with fewer attributes.
- Ps -eF: Provides extra full format details for processes.
Example 3: List every process on the system using BSD syntax (ps aux)
$ ps ax
This command display lists of all processes on the system using BSD syntax. It showcases columns for Process ID (PID), Terminal (TTY), Status (STAT), CPU Time (TIME), and Command (COMMAND).
Output:
PID TTY STAT TIME COMMAND 1 ? Ss 0:01 /sbin/init 2 ? S 0:00 [kthreadd] ....... 4883 pts/3 R+ 0:00 ps ax
STAT Codes:
- S: Interruptible sleep
- <: High-priority
- N: Low-priority
- s: Session leader
- l: Multi-threaded
- +: In the foreground process group
For instance, “STAT Ss” represents a sleep process that is a session leader and is the first in the queue for execution.
Example 4: Print a process tree (ps -ejH)
$ ps -ejH
This command displays a process tree, depicting the relationship among processes. The output shows columns for Process ID (PID), Process Group ID (PGID), Session ID (SID), Terminal (TTY), CPU Time (TIME), and Command (CMD).
Output:
PID PGID SID TTY TIME CMD 2 0 0 ? 00:00:00 kthreadd 3 0 0 ? 00:00:00 ksoftirqd/0 ....
Example 5: Print every process running as root (ps -U root -u root u)
$ ps -U root -u root u
This command shows processes running as the ‘root’ user. The output includes columns for User (USER), Process ID (PID), CPU usage (%CPU), Memory usage (%MEM), Virtual Memory Size (VSZ), Resident Set Size (RSS), Terminal (TTY), Status (STAT), Start Time (START), CPU Time (TIME), and Command (COMMAND).
Output:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.0 24576 2484 ? Ss 11:05 0:01 /sbin/init root 2 0.0 0.0 0 0 ? S 11:05 0:00 [kthreadd]
Additional Information:
- RSS (Resident Set Size): Non-swapped physical memory used by a task.
- VSZ (Virtual Memory Size): Total virtual memory usage by the entire process, which includes RSS.
Example 6: List name of command with a specific pid (ps -p X -o comm=)
$ ps -p 4983 -o comm=
This command specifically retrieves and displays the name of the command associated with a particular Process ID (PID), in this case, PID 4983.
Output:
bash
This command isolates and prints the command name that corresponds to the specified PID.
Example 7: List all processes of a specific user (ps -u user_name1,user_name2…)
$ ps -u user_name
This command displays all processes associated with the specified user, showcasing details such as Process ID (PID), Terminal (TTY), Time (TIME), and Command (CMD).
Output:
PID TTY TIME CMD 2174 ? 00:00:00 gnome-keyring-d 2186 ? 00:00:00 gnome-session 2223 ? 00:00:00 ssh-agent 2226 ? 00:00:00 dbus-launch ....... 3201 pts/2 00:00:00 ps
Example 8: List Processes in a Hierarchy tree (ps -e … –forest)
$ ps -e --forest
This command produces a structured output displaying parent and child processes in a tree-like format, providing a clearer understanding of process relationships.
Output:
PID TTY TIME CMD 2 ? 00:00:00 kthreadd 3 ? 00:00:00 \_ ksoftirqd/0 6 ? 00:00:00 \_ migration/0 7 ? 00:00:00 \_ watchdog/0 8 ? 00:00:00 \_ migration/1 9 ? 00:00:00 \_ kworker/1:0 10 ? 00:00:00 \_ ksoftirqd/1 11 ? 00:00:00 \_ kworker/0:1 12 ? 00:00:00 \_ watchdog/1 13 ? 00:00:00 \_ migration/2
Example 9: Finding memory Leakage (ps aux –sort pmem)
$ ps aux --sort pmem
This command displays a list of all processes along with their details, sorted by the percentage of memory (%MEM) used. This can aid in identifying potential memory leaks. If a process consistently shows a rise in %MEM or RSS (Resident Set Size) over time, it might indicate a memory leak.
Output:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 2 0.0 0.0 0 0 ? S 23:04 0:00 [kthreadd] root 3 0.0 0.0 0 0 ? S 23:04 0:00 [ksoftirqd/0] root 6 0.0 0.0 0 0 ? S 23:04 0:00 [migration/0] root 7 0.0 0.0 0 0 ? S 23:04 0:00 [watchdog/0] root 8 0.0 0.0 0 0 ? S 23:04 0:00 [migration/1] root 9 0.0 0.0 0 0 ? S 23:04 0:00 [kworker/1:0] root 10 0.0 0.0 0 0 ? S 23:04 0:00 [ksoftirqd/1] root 11 0.0 0.0 0 0 ? S 23:04 0:01 [kworker/0:1] root 12 0.0 0.0 0 0 ? S 23:04 0:00 [watchdog/1] root 13 0.0 0.0 0 0 ? S 23:04 0:00 [migration/2]
Example 10: List of threads of a Process/All-Processes (ps …-L…pid , tid)
Lists the threads running for a particular process :
sanfoundry->$ ps -L PID LWP TTY TIME CMD 2892 2892 pts/2 00:00:00 bash 3398 3398 pts/2 00:00:00 ps sanfoundry-> ps -L 2892 PID LWP TTY STAT TIME COMMAND 2892 2892 pts/2 Ss 0:00 bash
Example 11: List elapsed wall time for processes (ps ….. -o pid,etime=)
This command provides etime which provides the elapsed time since the process was started, in the form dd-hh:mm:ss.
$ ps -p 1 -o pid,etime=
This command specifically fetches and displays the elapsed time since the mentioned process started in the form dd-hh:mm:ss.
Output:
PID 1123 1-11:38:37
Example 12: Customizing PS Output Format (-o option)
$ ps -eo uname,ppid,pid,nlwp,pmem,time,args
This command configures the output format of ps to display specific information about each process, such as the user, process hierarchy, memory usage, CPU time, and the associated command.
Output:
USER PPID PID NLWP %MEM TIME COMMAND root 0 1 1 0.0 00:00:01 /sbin/init root 0 2 1 0.0 00:00:00 [kthreadd] root 2 3 1 0.0 00:00:00 [ksoftirqd/0] root 2 6 1 0.0 00:00:00 [migration/0] root 2 7 1 0.0 00:00:00 [watchdog/0] root 2 8 1 0.0 00:00:00 [migration/1] root 2 10 1 0.0 00:00:00 [ksoftirqd/1] whoopsie 1 1223 2 0.1 00:00:00 whoopsie daemon 1 1226 1 0.0 00:00:00 atd root 2 1236 1 0.0 00:00:00 [kvm-irqfd-clean] root 2 1251 1 0.0 00:00:00 [hci0] ...
Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.
- Check Linux Books
- Check Information Technology Books
- Apply for Programming Internship
- Practice Programming MCQs