Click on each book below to review & buy on Amazon.
As an Amazon Associate, I earn from qualifying purchases.
RHCSA - Operate Running Systems: Identify CPU/Memory Intensive Processes & Kill Processes
In this guide we will explore how to kill and identify CPU & memory intensive processes, and how to kill multiple processes of the same name.
Lesson Setup
The lesson setup will ensure that you have some processes to work with throughout this guide.
Setup high CPU and Memory processes, and setup up multiple processes of the same command:
# Install stress-ng
sudo yum install stress-ng
Enter sudo
password when prompted.
# Run stress-ng in the background to create a high CPU process
stress-ng -c 1 &
# Run stress-ng in the background to create a high CPU process
stress-ng -m 1 --vm-bytes 1024M &
# Setup multiple processes of the same command
for i in {1..10}
do
sleep 100000$i &
done
kill Command
The kill
command in Linux is utilized to send signals to processes, providing the ability to terminate or control their behavior. It becomes especially valuable when dealing with unresponsive programs or when you wish to interact gracefully with a running process.
To execute the kill
command, you need to provide a Process ID (PID) as its argument. By default, it sends the SIGTERM
signal to terminate the process. However, you have the option to specify different signals using flags to perform various actions on the target process. The table below outlines the most common flags and their respective functionalities:
Flag (Numeric) | Flag (Word) | Description |
---|---|---|
-1 | -SIGHUP | Hangup: Terminate a process after disconnecting from a terminal. |
-2 | -SIGINT | Interrupt: Terminate a process with a keyboard interrupt (Ctrl+C). |
-3 | -SIGQUIT | Quit: Terminate a process and generate a core dump. |
-9 | -SIGKILL | Kill: Forcefully terminate a process (cannot be caught or ignored). |
-15 | -SIGTERM | Terminate: Default signal to terminate a process gracefully. |
-17 | -SIGSTOP | Stop: Pause a process (cannot be caught or ignored). |
-18 | -SIGCONT | Continue: Resume a paused process, often after SIGSTOP. |
-19 | -SIGSTOP | Terminal Stop: Pause a process using job control. |
-20 | -SIGTSTP | Terminal Stop: Pause a process (Ctrl+Z). |
There are many other signals available, just run kill -l
to list them all.
Identify & Kill Memory Intensive Processes
To identify memory intensive processes you can use ps
or top
commands.
To quit out of top
, press Q.
In either of the following 2 exercises, note down the PID of the high memory process.
Identifying memory intensive process using ps
:
ps -eo pid,%mem,comm --sort=-%mem | head
The output should show the PID and memory usage for our stress-ng-vm
process created in the lesson setup.
Identifying memory intensive process using top
:
Running top with the -o %MEM
arguments will launch top
sorted by memory usage.
top -o %MEM
The stress-ng-vm
process should be the first process in the top
display.
You can launch top
on its own and then sort on %MEM also.
top
Now press Shift+M.
In the following exercise you will use the PID obtained earlier.
Kill high memory process:
Attempt to kill the high memory process.
kill <PID>
Check if the process still exists.
ps <PID>
If the process still exists, forcefully kill it.
kill -9 <PID>
Identify & Kill CPU Intensive Processes
To identify CPU intensive processes you can use ps
or top
commands.
In either of the following 2 exercises, note down the PID of the high memory process.
Identifying CPU intensive process using ps
:
ps -eo pid,%cpu,comm --sort=-%cpu | head
The output should show the PID and CPU usage for our stress-ng-vm
process created in the lesson setup.
Identifying CPU intensive process using top
:
top
The stress-ng-cpu
process should be the first process in the top
display.
In the following exercise you will use the PID obtained earlier for the high CPU process.
Kill high CPU process:
Attempt to kill the high CPU process.
kill <PID>
Check if the process still exists.
ps <PID>
If the process still exists, forcefully kill it.
kill -9 <PID>
kill Multiple of the Same Process
There may be occasions where multiple of the same process are spawned and it is not practical to locate each PID to kill them individually. In this scenario you can use the killall
command and pass it the name of the process.
Kill all of the sleep
processes:
Confirm that our 10 sleep
processes created in the lesson setup still exist.
ps -ef | grep sleep
You should have output similar to:
user 3664 3064 0 08:11 pts/0 00:00:00 sleep 1000001
user 3665 3064 0 08:11 pts/0 00:00:00 sleep 1000002
user 3666 3064 0 08:11 pts/0 00:00:00 sleep 1000003
user 3667 3064 0 08:11 pts/0 00:00:00 sleep 1000004
user 3668 3064 0 08:11 pts/0 00:00:00 sleep 1000005
user 3669 3064 0 08:11 pts/0 00:00:00 sleep 1000006
user 3670 3064 0 08:11 pts/0 00:00:00 sleep 1000007
user 3671 3064 0 08:11 pts/0 00:00:00 sleep 1000008
user 3672 3064 0 08:11 pts/0 00:00:00 sleep 1000009
user 3673 3064 0 08:11 pts/0 00:00:00 sleep 10000010
To kill all the sleep commands, run:
killall sleep
Support DTV Linux
Click on each book below to review & buy on Amazon. As an Amazon Associate, I earn from qualifying purchases.
NordVPN ®: Elevate your online privacy and security. Grab our Special Offer to safeguard your data on public Wi-Fi and secure your devices. I may earn a commission on purchases made through this link.