2024-04-19
top OutputWhen you execute top (usually as sudo top for full privileges), you’ll be presented with a screen displaying various system metrics. Let’s break down the key columns:
toptop isn’t just a passive viewer; it’s interactive. Here are some key interactions:
q: Exits top.h or ?: Displays help information.1: Toggles between single-user mode (only shows processes for the currently logged-in user) and all users.k: Allows you to kill a process by entering its PID. You will be prompted to confirm.r: Renices a process (changes its priority). This requires the PID and the new nice value.Example: Killing a Process
Let’s say you want to kill a process with PID 1234.
sudo top.k.1234 and press Enter.Example: Renicing a Process
To change the nice value of process 5678 to 10:
sudo top.r.5678 10 and press Enter. (This assumes the process exists and you have the necessary permissions).top’s Displaytop offers many options for customizing its output. You can specify these options directly when launching top:
top -u <username>: Shows only processes owned by the specified username. For example: sudo top -u johntop -p <pid1>,<pid2>,...: Shows only processes with the specified PIDs. Example: sudo top -p 1234,5678top -b: Runs top in batch mode, writing the output to standard output instead of the screen. This is useful for scripting.top -n <number>: Specifies the number of iterations top will run before exiting in batch mode.Example: Batch Mode Output to a File
To run top in batch mode for 10 iterations and save the output to a file:
sudo top -bn10 > top_output.txttop Output for Performance TuningThe top command is for performance analysis. By regularly monitoring CPU usage (%CPU), memory usage (%MEM), and the processes consuming the most resources, you can pinpoint bottlenecks and address them effectively. Identifying consistently high CPU or memory usage from specific processes often indicates a need for investigation, such as code optimization, resource leaks, or potential issues with the application itself. You can use this information to adjust resource allocation, upgrade hardware, or optimize software.