How To Use The "top" Command
On a linux/unix system, using the "top" command can be very useful. Simply open a terminal (or ssh into your server) and run `top`
**This is similar to running "htop" → top is the older command and it comes preinstalled on all Linux distros. htop is newer and it adds color and gives a more interactive user interface than top**
For this example, Im running a "stress" process to show the process of stopping (or killing) a process.
You can see that the process "stress" is taking up quite a bit of resources, causing the CPU to be higher than expected, using more memory than expected, and we can even see in the "%CPU" column how much cpu each process is using.
Lets pretend that this process is something you are unaware of, or maybe this is a stuck process, or even a long running query that you are not able to cancel. You are able to restart the server to reload the processes and (most of the time) fix the issue.
However, we can use some quick troubleshooting to correct this without a restart.
PS AUX
The ps aux command is a tool to monitor processes running on your Linux system
Running "ps aux" will show us the processes that are running, and provide a "PID" that we can use.
**It is worth noting that the "top" command will show your the PID all the way on the left hand side, however you will not always have the exact process appearing in top and will need to do some further investigating**
Running "ps aux" on its own is useful, but we know that the process we are looking for is called "stress" so lets grep for that and make things a bit simpler:
ps aux | grep "stress"
From here, we can see the PID and the CPU % that is being used for each process.
(This example will have multiple, but most of the time you will only be searching for one process)
Using the "Kill" Command
The "kill" command is used to send a signal to a process, which can be used to kill the process.
There are several options in which to use "kill" and for this example we are going to focus on "kill -9" specifically:
kill -9 → This signal is used to immediately terminate a process, without allowing it to clean up or save any data
**PLEASE NOTE - This is to fully stop a process, without saving any data, and can be dangerous if ran incorrectly or on the wrong process - Please use caution when using this command and always be sure the process is one that you want to stop immediately**
Now that we can have the PID (From either "top" or "ps aux"), we can use this to go ahead and stop that process:
sudo kill -9 <PID>
So for this example we would run:
sudo kill -9 2247848
which will stop that process using 49.9% CPU
Repeat this process until you have found all of the problem processes
Comments
0 comments
Article is closed for comments.