Step-by-Step Guide to Managing CPU and Memory on Linux

Optimize Linux Performance: Mastering CPU and Memory Management on Linux
Are you ready to take control of your Linux system’s performance? With our step-by-step guide, you’ll learn how to dynamically manage CPU and memory usage, ensuring smooth operation and optimal efficiency.
Whether you’re troubleshooting performance issues or simply aiming to enhance system responsiveness, our comprehensive instructions cater to users of all levels. By identifying resource-intensive processes and understanding their behavior, you’ll be equipped to gracefully restart them, reclaiming valuable system resources along the way.
Our user-friendly guide empowers you to proactively manage CPU and memory usage, navigating runtime challenges with ease. By following our instructions, you’ll gain the skills needed to address resource-intensive processes effectively, promoting a stable and responsive computing environment.
Ready to elevate your Linux experience? Let’s dive in and optimize your system’s performance today!
Prerequisite: Ubuntu
Get started on your journey to Linux optimization now. Master CPU and memory management with our comprehensive guide tailored for Ubuntu users.
I am using PHP-Pool restarting for this task If the threshold is greater than 80 %
nano /etc/systemd/system/php-fpm-monitor.service
Add the following snippet for creating the service for restarting the php-pool
# /etc/systemd/system/php-fpm-monitor.service
[Unit]
Description=PHP FPM Monitor Service
After=network.target
[Service]
Type=simple
ExecStart=/root/home/monitor_and_restart.sh
[Install]
WantedBy=default.target
Now we will write a shell script for the service that we have just created if the threshold of memory and CPU is greater than 80% it will restart the service.
— Create a monitor_and_restart.sh file at any location where you want. Make sure to change the location at the same place at /etc/systemd/system/php-fpm-monitor.service. “ExecStart”
#!/bin/bash
CPU_THRESHOLD=80
MEMORY_THRESHOLD=80
CPU_USAGE=$(top -b -n 1 | grep "Cpu(s)" | awk '{print $2}' | cut -d'%' -f1)
MEMORY_USAGE=$(free | awk '/Mem/ {print $3/$2 * 100.0}')
# Check if CPU or memory usage is above the threshold
if (( $(echo "$CPU_USAGE > $CPU_THRESHOLD" | bc -l) )) || (( $(echo "$MEMORY_USAGE > $MEMORY_THRESHOLD" | bc -l) )); then
echo "CPU or memory usage is above the threshold. Restarting PHP FPM pool..."
sudo systemctl restart php74rc-fpm.service #You can modifiy this with any services.
echo "PHP FPM pool restarted."
fi
Start the service that we have just created on /etc/systemd/system/php-fpm-monitor.service
systemctl start php-fpm-monitor.service
Note: The service will be in Idle start if the threshold is not high. Whenever the threshold hits the service will start and then go into an idle state automatically.
Check the service if its running or not
systemctl status php-fpm-monitor.service
Enable the service as the system service
systemctl enable php-fpm-monitor.service
######OPTIONAL######
If you want to log that on what time the service got Restarted and.
Add this snippet instead as a shell script on the same location
#!/bin/bash
CPU_THRESHOLD=80
MEMORY_THRESHOLD=80
LOG_FILE="/root/home/restart.monitor.log"
CPU_USAGE=$(top -b -n 1 | grep "Cpu(s)" | awk '{print $2}' | cut -d'%' -f1)
MEMORY_USAGE=$(free | awk '/Mem/ {print $3/$2 * 100.0}')
TIMESTAMP=$(date +"%Y-%m-%d %H:%M:%S") # Get the current time stamp
# Check if CPU or memory usage is above the threshold
if (( $(echo "$CPU_USAGE > $CPU_THRESHOLD" | bc -l) )) || (( $(echo "$MEMORY_USAGE > $MEMORY_THRESHOLD" | bc -l) )); then
echo "CPU or memory usage is above the threshold. Restarting PHP FPM pool..."
sudo systemctl restart php74rc-fpm.service
echo "PHP FPM pool restarted."
fi
# Log information to the file
echo "Timestamp: $TIMESTAMP" >> "$LOG_FILE"
echo "CPU Usage: $CPU_USAGE%" >> "$LOG_FILE"
echo "Memory Usage: $MEMORY_USAGE%" >> "$LOG_FILE"
echo "---------------------------------------" >> "$LOG_FILE"
Then Restart the Service
systemctl restart php-fpm-monitor.service