Click on each book below to review & buy on Amazon.
As an Amazon Associate, I earn from qualifying purchases.
CompTIA Linux+ XK0-005 - 1.4 - Process Management: Listing Processes & Open Files
n Linux, it's essential to be able to monitor and manage processes effectively. This involves listing running processes and gaining insights into their resource usage, as well as identifying open files associated with these processes. Let's explore some commonly used commands for process management:
top
The top
command provides a real-time overview of system resource usage and displays information about running processes. It continuously updates the information on the screen. Run the following command to launch top
:
top
Here's an example of the output:
top - 09:15:30 up 2 days, 5:27, 1 user, load average: 0.75, 0.86, 0.92
Tasks: 126 total, 1 running, 125 sleeping, 0 stopped, 0 zombie
%Cpu(s): 4.8 us, 2.1 sy, 0.0 ni, 92.9 id, 0.2 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 4096000 total, 224816 free, 3322920 used, 548264 buff/cache
KiB Swap: 2097152 total, 1997820 free, 99332 used. 719248 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1125 user1 20 0 1354600 424080 79820 S 5.2 10.4 2:48.34 firefox
2243 user2 20 0 717572 123056 71244 S 1.8 3.0 0:32.16 code
982 user3 20 0 583632 81320 53948 S 0.9 2.0 0:18.82 gnome-terminal
3781 user4 20 0 468324 52396 44560 S 0.4 1.3 0:04.11 nautilus
1679 user5 20 0 294432 37748 32048 S 0.2 0.9 0:03.65 panel-9-mate
In this example, the top
command provides a real-time view of the system's processes and resource utilization. The output starts with system information, including the current time, system uptime, the number of logged-in users, and the load average.
The subsequent lines provide details about the processes running on the system. Each line represents a separate process and displays information such as the process ID (PID), user, priority (PR), nice value (NI), virtual memory (VIRT), resident memory (RES), shared memory (SHR), process state (S), CPU usage (%CPU), memory usage (%MEM), and the accumulated CPU time (TIME+) for each process. The rightmost column shows the command or program associated with each process.
In the example output, you can observe several processes, including firefox
, code
, gnome-terminal
, nautilus
, and panel-9-mate
. The %CPU
and %MEM
columns indicate the CPU and memory usage percentages, respectively. Additionally, the memory usage and swap space information are displayed.
The top
command updates the displayed information periodically, allowing you to monitor the system's resource usage and identify any processes consuming significant CPU or memory resources. You can navigate through the top
interface to sort processes, adjust the display, and take various actions such as sending signals or killing processes.
ps
The ps
command provides a snapshot of currently running processes. It allows you to view process information such as process ID (PID), parent process ID (PPID), CPU and memory usage, and more. Run the following command to display a list of processes:
ps -e
Here's an example of the output:
PID TTY TIME CMD
1854 pts/0 00:00:01 bash
2237 pts/0 00:00:00 ps
2569 ? 00:00:00 systemd-journal
2774 ? 00:00:01 apache2
In this example, the ps
command provides a snapshot of the active processes on the system. Each line represents a separate process and displays information such as the process ID (PID), terminal associated with the process (TTY), CPU time consumed by the process (TIME), and the command or program associated with each process (CMD).
The first line shows a process with PID 1854 running under the pts/0
terminal, which is a bash shell. It has been running for 1 second.
The second line displays the ps
command itself (PID 2237) that was executed to generate the process listing.
The third line represents the systemd-journal
process (PID 2569), which is responsible for managing system logs.
The fourth line corresponds to the apache2
process (PID 2774), which is an HTTP server.
The ps
command provides a static view of the processes at the moment it is executed. It is useful for inspecting the currently running processes and obtaining basic information about them.
Keep in mind that the output may vary depending on the system's activity and the options provided to the ps
command. Exploring different options, such as ps aux
or ps -ef
, allows you to view additional details about processes, such as their ownership, memory usage, and command arguments.
lsof
The lsof
command lists open files and the processes that have them open. It provides valuable insights into which processes are accessing certain files. Run the following command to display open files:
lsof
Here's an example of the output:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 1234 root 3u IPv6 12345 0t0 TCP *:22 (LISTEN)
sshd 1234 root 4u IPv4 67890 0t0 TCP *:22 (LISTEN)
httpd 2345 apache 5u REG 123456 0t0 TCP 192.168.1.100:80->192.168.1.200:54321
httpd 2345 apache 6u REG 789012 0t0 TCP 192.168.1.100:80->192.168.1.201:12345
In this example, the lsof
command provides a list of open files and the processes that have them open. Each line represents a file or socket and displays information such as the process COMMAND, process ID (PID), user, file descriptor (FD), type of file (TYPE), device number (DEVICE), file size/offset (SIZE/OFF), node number (NODE), and the file or socket name (NAME).
The first two lines show the sshd
process (PID 1234) with file descriptors 3 and 4, which represent IPv6 and IPv4 listening sockets respectively. The sshd
process is the SSH daemon listening on port 22 for incoming connections.
The next two lines represent the httpd
process (PID 2345), which is an Apache HTTP server. It has file descriptors 5 and 6, which are open network connections (TCP). The NAME
column shows the local and remote IP addresses and ports associated with the connection.
The lsof
command provides a dynamic view of open files and network connections on the system at the moment it is executed. It is useful for troubleshooting issues related to file access, identifying processes that have certain files open, and examining network connections.
By using the lsof
command, you can gain insights into the open files and network connections on your system, helping you diagnose issues, identify resource usage, and understand the interactions between processes and files.
Please note that the output of lsof
can be extensive, especially on a busy system with numerous processes and open files. You can use various options and filters with lsof
to narrow down the output and focus on specific files, processes, or network connections of interest.
htop
The htop
command is an interactive process viewer that provides a more user-friendly and feature-rich interface compared to top
. It allows you to monitor and manage processes interactively. Run the following command to launch htop
:
htop
Here's an example of the output:
1 [||||||||||||||||||||| 44.0%] Tasks: 123, 27 thr; 1 running
2 [|||||||||||||||||||||||||||||||||||||||||||||| 95.0%] Load average: 0.85 0.78 0.92
3 [|||||||||||||||||||||||||||||||||||||||||| 77.0%] Uptime: 2 days, 4:18:33
PID USER PRI NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1234 user1 20 0 3.5G 2.1G 150M R 60.0 4.5 1:23:12 firefox
5678 user2 19 0 2.1G 1.2G 120M S 20.0 2.5 0:45:32 code
9876 user3 15 0 1.8G 1.1G 100M S 10.0 2.3 0:32:18 chrome
4321 user4 10 0 1.5G 900M 80M S 5.0 1.9 0:18:40 terminal
2468 root 5 - 120M 80M 20M S 2.0 0.4 0:04:23 sshd
In this example, the htop command displays a dynamic, interactive view of the system's processes. The columns provide information about the process ID (PID), user, priority, virtual memory (VIRT), resident memory (RES), shared memory (SHR), process state (S), CPU usage (%CPU), memory usage (%MEM), and the accumulated CPU time (TIME+) for each process. The right side of the screen shows a visual representation of CPU and memory usage.
The example output illustrates several processes, such as firefox, code, chrome, terminal, and sshd. You can observe the CPU and memory usage percentages, as well as the running state of each process. This information is valuable for identifying resource-intensive processes and monitoring system performance.
As you navigate through the htop interface, you can interactively sort processes based on different columns, kill or send signals to specific processes, and adjust the update frequency to suit your needs. This makes htop a powerful tool for process management and monitoring in Linux.
Conclusion
In this guide, we have explored the essential concepts and commands related to process management and listing processes and open files in Linux. Understanding these concepts and tools is crucial for effectively managing and monitoring the system's resources and processes.
The top command provides real-time information about system resource usage and running processes, allowing you to identify resource-intensive processes. The ps command offers a snapshot of currently running processes, displaying detailed information such as process ID, CPU usage, and memory consumption.
To gain insights into open files and the processes accessing them, the lsof command proves invaluable. It provides a comprehensive list of open files, their associated processes, and other related details.
Additionally, the htop command offers an interactive and visually appealing process viewer, allowing you to monitor and manage processes efficiently.
Support DTV Linux
Click on each book below to review & buy on Amazon. As an Amazon Associate, I earn from qualifying purchases.
NordVPN ®: Elevate your online privacy and security. Grab our Special Offer to safeguard your data on public Wi-Fi and secure your devices. I may earn a commission on purchases made through this link.