Monitoring Linux Processes
Effective monitoring and inspection of Linux processes is crucial for system administrators and developers to understand the state of their systems and troubleshoot issues. Linux provides a variety of tools and commands for this purpose. In this section, we will explore some of the commonly used process monitoring utilities.
The ps
Command
The ps
(process status) command is a fundamental tool for inspecting running processes. It allows you to view information about the current state of processes, such as their process ID (PID), user, CPU and memory usage, and more. Here's an example of using the ps
command:
$ ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.4 18240 4424 ? Ss Apr04 0:01 /sbin/init
root 2 0.0 0.0 0 0 ? S Apr04 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? I< Apr04 0:00 [rcu_gp]
The ps
command can be customized with various options to display specific process information.
The top
Command
The top
command provides a real-time, dynamic view of the running processes on the system. It displays information such as CPU and memory utilization, as well as detailed process-level metrics. The top
command updates the display at regular intervals, allowing you to monitor system activity in real-time.
top - 12:34:56 up 123 days, 12:34, 1 user, load average: 0.15, 0.20, 0.18
Tasks: 295 total, 1 running, 294 sleeping, 0 stopped, 0 zombie
%Cpu(s): 2.0 us, 1.0 sy, 0.0 ni, 97.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 3921224 total, 378164 free, 1509596 used, 2033464 buff/cache
KiB Swap: 2097148 total, 2097148 free, 0 used. 1728312 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 118240 4424 2876 S 0.0 0.1 0:01.20 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd
The pstree
Command
The pstree
command displays a hierarchical view of the running processes, showing the parent-child relationships between them. This can be useful for understanding the process structure and dependencies within the system.
$ pstree
systemd─┬─accounts-daemon───2*[{accounts-daemon}]
├─agetty
├─atd
├─cron
├─dbus-daemon
├─dockerd─┬─containerd─┬─containerd-shim─┬─nginx───2*[nginx]
│ │ └─2*[containerd-shim]
│ └─7*[{dockerd}]
├─irqbalance
├─networkd-dispat
├─rsyslogd───2*[{rsyslogd}]
├─snapd───14*[{snapd}]
├─sshd───sshd───bash───pstree
├─systemd-journal
├─systemd-logind
├─systemd-network
├─systemd-resolve
└─systemd-timesyn
These process monitoring tools provide valuable insights into the state and behavior of your Linux system, enabling you to identify and troubleshoot issues more effectively.