2024-07-06
top DisplayWhen you execute top (simply type top in your terminal and press Enter), you’re presented with a constantly updating display showing various system processes. The default view typically includes:
Example:
A typical top output might look like this (though the specifics will vary greatly depending on your system):
top - 11:30:12 up 1:23, 1 user, load average: 0.80, 0.72, 0.65
Tasks: 120 total, 1 running, 119 sleeping, 0 stopped, 0 zombie
%Cpu(s): 1.6 us, 0.8 sy, 0.0 ni, 97.6 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 7962.2 total, 6249.1 free, 674.4 used, 1038.7 buff/cache
MiB Swap: 7999.9 total, 7999.9 free, 0.0 used. 6895.7 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
2928 root 20 0 120752 8720 5668 R 5.8 0.1 0:00.23 top
1 root 20 0 266864 3388 2680 S 0.0 0.0 0:02.96 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.07 kthreadd
toptop offers many interactive features:
o (lowercase ‘o’) command, followed by the filtering criteria. This is less common and requires more advanced syntax.k command, you can specify a PID to send a signal to a process (often SIGTERM, which requests termination). This can be helpful for stopping runaway processes.h or ? will display a list of interactive commands available within top. These commands offer significant control over the displayed information and behavior.top: Press q to exit the top command and return to the terminal prompt.Example: Killing a Process
Let’s say you want to kill process with PID 12345. You would follow these steps:
topk12345 and press Enter.TERM signal is acceptable.top Command-Line Optionstop also accepts various command-line options to customize its behavior:
-b (batch mode): Runs top in batch mode, suitable for scripting and redirecting output.-d (delay): Specifies the delay between updates (in seconds). For instance, top -d 2 updates every 2 seconds.-n (iterations): Specifies the number of iterations top runs before exiting. top -n 10 will display 10 iterations.-p <PID> (PID): Displays only processes with the specified PIDs. top -p 1234,5678 will display processes with IDs 1234 and 5678.-u <user> (user): Displays only processes belonging to the specified user. top -u john shows only processes owned by user john.Example: Customizing top
To display top output every 5 seconds for 10 iterations:
top -d 5 -n 10To view only processes belonging to user alice:
top -u aliceThis guide provides a solid foundation for using the top command. Experiment with the different options and interactive commands to gain a deeper understanding of your system’s performance characteristics. Remember to use top responsibly, as indiscriminately killing processes can have unintended consequences.