In Linux, a process is a running instance of a program. It is the execution of a program’s instructions by the computer’s central processing unit (CPU). Each process is assigned a unique process ID (PID) for identification and management by the operating system.
List Linux processes using the ps
command
The ps
command is a command-line utility in Unix-like operating systems, including Linux. It is used to display information about currently running processes. The ps
command provides a snapshot of the current processes and their attributes. Here are some common ways to use the ps
command:
Using the ps command to list all processes
To use the ps
command to ls all the running processes use the following command:
ps aux
This command shows a detailed list of all processes running on the system as seen below in the sample output:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 12345 6789 ? Ss Feb10 0:02 /sbin/init
user1 1234 1.2 3.4 567890 123456 pts/0 Sl+ Feb10 1:23 /usr/bin/example-command
user2 5678 0.0 0.0 1234 567 pts/1 R+ Feb10 0:00 ps aux
In this example:
USER
: The owner of the process.PID
: Process ID.%CPU
: CPU usage.%MEM
: Memory usage.VSZ
: Virtual memory size.RSS
: Resident Set Size (non-swapped physical memory used by the process).TTY
: Terminal type associated with the process.STAT
: Process status.START
: Start time of the process.TIME
: Total accumulated CPU time.COMMAND
: The command or program associated with the process.
Using the ps command to list Processes of a Specific User
You can use the ps command to list the processes of a specific Linux user by implementing the following command;
ps -u username
Replace “username” with the actual username. This command displays processes owned by the specified user.
Using the ps command to list Processes with a Specific PID
In Linux, a Process ID (PID) is a unique numerical identifier assigned to each running process. It is used to distinguish one process from another. The PID is crucial for various system administration and monitoring tasks.
If you want to use the ps
command to list a process based on its PID, then you can run the following command;
ps -p PID
Replace “PID” with the actual process ID. This command shows information about the process with the specified PID.
Using the ps command with custom Output Format
The ps command can also allow you to define a custom output format on the terminal. Consider the following command below;
ps -o pid,ppid,cmd
This command allows you to specify the columns you want to see. In this example, it shows the process ID, parent process ID, and the command.
Using the ps command to display a more user-friendly format
To display clean output display results in a user-friendly format using the ps command, run the following command:
bash ps aux | less
List Linux processes using the top
command
The top
command is a Unix/Linux command-line utility that provides a dynamic, real-time view of the processes running on a system. It offers a continuously updated display of system resource usage, including information about CPU utilization, memory usage, and the top-consuming processes.
When you run the top
command in the terminal, it presents a live, interactive table that shows various system statistics. For example, consider the following command:
top
Here’s a simplified example of what the top command output might look like this:
top - 10:45:30 up 1 day, 2:30, 1 user, load average: 0.25, 0.15, 0.10
Tasks: 203 total, 1 running, 202 sleeping, 0 stopped, 0 zombie
%Cpu(s): 3.0 us, 1.5 sy, 0.0 ni, 94.9 id, 0.5 wa, 0.0 hi, 0.1 si, 0.0 st
MiB Mem : 7955.0 total, 1257.1 free, 3685.5 used, 2012.4 buff/cache
MiB Swap: 4096.0 total, 4096.0 free, 0.0 used. 4161.9 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1234 user 20 0 350.7m 45.2m 15.3m S 5.0 0.6 0:05.12 example_process
5678 another 20 0 520.3m 78.4m 27.9m R 12.3 1.0 0:08.34 another_process
9876 user 20 0 1.2g 85.6m 21.4m S 2.6 1.1 1:20.43 important_app
42 root 20 0 0 0 0 I 0.0 0.0 0:00.01 kernel_thread
...
The default view typically includes details such as:
- PID (Process ID): The unique identifier for each running process.
- USER: The username of the process owner.
- PR (Priority): The priority of the process.
- NI (Nice Value): The “niceness” value, which affects process priority.
- VIRT (Virtual Memory): The total virtual memory used by the process.
- RES (Resident Memory): The non-swapped physical memory used by the process.
- SHR (Shared Memory): The amount of shared memory used by the process.
- S (%CPU): The percentage of CPU utilization by the process.
- MEM (%MEM): The percentage of physical memory (RAM) used by the process.
- TIME+: The total accumulated CPU time used by the process.
Interacting with the top command
Here’s a table summarizing the common keys used in the top
command:
Key | Description |
---|---|
q | Quit top and exit. |
h | Display help (show a summary of available commands). |
P | Sort processes by CPU usage (default). |
M | Sort processes by memory usage. |
F | Enter the Fields Management screen to customize displayed fields. |
Space | Toggle fields on/off in the Fields Management screen. |
Enter | Return to the main top display from the Fields Management screen. |
k | Kill a process. Enter the PID when prompted. |
r | Renice a process. Enter the PID and the new priority. |
s | Change the update interval. Enter the desired interval in seconds. |
A | Toggle between showing all users’ processes and only the current user’s processes. |
c | Toggle between showing command line and program name. |
Arrow keys | Navigate through the process list. |
1 | Toggle between single and multiple processors view. |
i | Toggle between highlighting running tasks and updating non-running tasks. |
You can use these keys interactively while top
is running to customize the display and perform actions based on your monitoring needs.
List Linux processes using the htop
command
The htop
command is an interactive and user-friendly process viewer for Linux. It provides a color-coded, customizable, and dynamic view of processes, making it easier to monitor system resources. Here’s how you can use htop
to list Linux processes:
htop
Once htop
is running, you’ll see an interactive and colorful display showing a list of processes. The default view includes information such as process ID (PID), user, CPU usage, memory usage, and more:
1 [||||||||||||||||||||||||| 24.0%] Tasks: 196, 161 thr; 1 running
2 [|||||||||||||||||||| 18.2%] Load average: 0.75 0.55 0.30
Mem[||||||||||||||||| 2.01G/7.77G [-----] Uptime: 1 day, 3:45:27
Swp[ 0K/4.00G [-------]
PID USER PRI NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1234 user 20 0 350.7m 45.2m 15.3m S 5.0 0.6 0:05.12 example_process
5678 another 20 0 520.3m 78.4m 27.9m R 12.3 1.0 0:08.34 another_process
9876 user 20 0 1.2g 85.6m 21.4m S 2.6 1.1 1:20.43 important_app
... (more processes)
How to interact with the htop command
Here’s a table summarizing some common keys used in the htop
command:
Key | Description |
---|---|
q | Quit htop . |
F2 | Enter setup menu to configure settings. |
F3 | Search by process name. |
F4 | Search by command line. |
F5 | Toggle tree view, showing parent-child relationships. |
F6 | Change the sorting column. |
F9 | Kill a process; prompts for PID. |
F10 | Quit htop . |
Up/Down Arrows | Navigate through the process list. |
Space | Tag or untag a process for action. |
Enter | View detailed information about a process. |
These keys can be used interactively while htop
is running to customize the display, navigate through processes, and perform various actions. The function keys (F2
, F3
, etc.) are particularly useful for accessing different features and settings within the htop
interface. Keep in mind that htop
is designed to be user-friendly and provides a rich set of features for monitoring and managing processes on a Linux system.
List Linux processes using the pgrep
command
The pgrep
command is used to search for processes based on their names or other attributes and print the process IDs (PIDs) of matching processes. Here’s how you can use pgrep
to list Linux processes:
Using the pgrep command to list Processes by Name
To list processes based on their names, you can use the following syntax:
pgrep process_name
Replace “process_name” with the actual name or part of the name of the process you are looking for.
For example, the following command will print the PIDs of all processes containing the name “firefox.”
pgrep firefox
Using the pgrep command to list Processes by User
If you want to list processes by User using the pgrep command, you can run the following command:
pgrep -u username
Replace “username” with the actual username.
#### Example:
pgrep -u john
This command will print the PIDs of all processes owned by the user “john.”
Using the pgrep command to list Processes by Group
You can use the pgrep command to list processes based on the group. Use the following command:
pgrep -g groupname
Replace “groupname” with the actual group name.
#### Example:
pgrep -g developers
This command will print the PIDs of all processes that belong to the group “developers.”
Additional attributes associated with pgrep command
If you want to further customize the pgrep process attributions, let’s further break them down
- Case-insensitive search
In the context of the pgrep
command, a case-insensitive search means that the search for processes based on their names is not sensitive to the case of the characters. This means that uppercase and lowercase letters are considered equivalent during the search.
By default, pgrep
performs a case-sensitive search, meaning that it distinguishes between uppercase and lowercase letters in the process name. However, when you use the -i
option with pgrep
, it enables case-insensitive matching.
Here’s the syntax for a case-insensitive search with pgrep
:
pgrep -i process_name
Replace “process_name” with the actual name or part of the name of the process you are looking for. The -i
option tells pgrep
to ignore case while searching.
Consider the following command:
pgrep -i firefox
This command will list the process IDs (PIDs) of all processes containing the name “firefox,” regardless of the case (e.g., “firefox,” “FireFox,” “FIREFOX,” etc.).
Using the -i
option with pgrep
makes the search case insensitive, allowing you to find processes without being concerned about the specific capitalization of the letters in the process name.
- Negate the match
This means excluding processes that match the specified criteria. When you use the -v
option with pgrep
, it negates or inverts the match, meaning that the command will print the process IDs (PIDs) of processes that do not match the specified criteria
Here’s the syntax for negating the match with pgrep
pgrep -v process_name
Replace “process_name” with the actual name or part of the name of the process you want to exclude.
For example, the following command will list the PIDs of all processes that do not have “firefox” in their name. It excludes processes with names matching the specified criteria.
pgrep -v firefox