Whenever your Linux server is running slowly, and you need to take a look at what's going on, the tool top
will come to your aid. Windows / OSX users: This is the Linux command-line equivalent of the task manager / activity monitor (Windows Control-Alt-Delete screen).
What Is Top?
top
is a dynamic, real-time view of your system, and it comes pre-installed on almost all major distributions. To get started, open top by typing top
at the prompt.
top
displays system information and processes, but it can do more than just that.
No top? Install top with a package manager, e.g. apt-get install top
.
Renice? PID?
- In *nix computing the nice value is used to declare the priority of the process for the kernel. The values can range from -20 (the highest priority) to 19 (the lowest priority).
- The default niceness for processes is inherited from its parent process, and usually set to 0.
- The PID (Process ID) is a unique identifier *nix systems give processes. If you need to stop a process (kill a process) then you will do so via the PID and
kill
tool.
Using Top to Renice Processes
Important: Your user must have root privileges. Use sudo top
.
- At the prompt, type
sudo top
and press enter. - You are now seeing a real-time view of your system. To increase the refresh rate, type '
d
' and enter a numerical value (0.1 is as fast as you can go). Press enter to save your changes. - You will be able to see the slow processes hanging at the top of the list.
- The PID (Process ID) is in the first column on the far left; we will use this number to renice the slow process.
- Type '
r
' and now enter the PID and press enter. - Enter your value to adjust CPU consumption (-20 = Highest Priority, 19 = Lowest Priority). Press enter to save.
top
to Linux and need to use the renice tool at the terminal prompt instead.Using Top to Kill Processes
Important: Your user must have root privileges. Use sudo top
.
- At the prompt, type
sudo top
and press enter. - Type '
k
' and press enter. - Type the PID in you wish to kill (PID can be retrieved from the top list or via running ps aux | grep process-name-here, e.g.
ps aux | grep mysqld
). - SIGTERM sent—bye bye slow process!
Note: You can also kill a process at command line via the kill
command. But you will require the PID to do so first. There is also the pkill
command, which takes the process name, e.g. pkill -HUP mysqld
. You must know the process name to use this method.
What Else Can Top Do?
At the top
screen, try entering the following case-sensitive keystrokes:
- '
1
' (numerical 1): Toggle between total values and displaying all your CPU cores - '
l
' (lowercase L): Show load averages - '
u
': Display tasks for just one username - '
A
': Change to active display mode (lower case a to cycle views, memory usage, jobs, etc.) - '
V
': Known as the 'Forest View' aka tree view (basically ps aux for top!) - '
i
': Show idle tasks - '
H
': Show threads (similar to F) - '
S
': Enable cumulative time mode - '
W
': Save your top configuration, so your personalisations persist! (Only available in active mode) - '
h
': Display detailed help to learn more cool things to do with top!
Conclusion
top
is an easy-to-use task manager and has a lot of power for filtering, viewing and modifying running tasks. If you liked this and want more, try out top's big brother htop
for an even more advanced view.
Comments