This tutorial will guide you through setting up a script that automatically reboots your Raspberry Pi if the CPU load exceeds 90% or RAM usage exceeds 80%.
Prerequisites
- A Raspberry Pi.
- Raspbian OS installed.
- Basic knowledge of using the terminal.
Step 1: Install Required Tools
Open the terminal and run the following commands to update your package list and install the necessary tools:
sudo apt-get update sudo apt-get install stress
`stress` is a tool we’ll use later to test high CPU and RAM usage.
Step 2: Create the Monitoring Script
- Open a terminal on your Raspberry Pi.
- Create the script file in the
/usr/local/bin
directory:sudo nano /usr/local/bin/cpu_ram_monitor.sh
- Paste the following script into the editor:
#!/bin/bash CPU_THRESHOLD=90 # CPU load threshold (%) RAM_THRESHOLD=80 # RAM usage threshold (%) while true; do CPU_LOAD=$(uptime | awk -F '[, ]+' '{print $10}' | sed 's/,//') RAM_USAGE=$(free | grep Mem | awk '{print $3/$2 * 100.0}' | awk '{printf "%.0f\n", $1}') echo "Current CPU Load: $CPU_LOAD%" echo "Current RAM Usage: $RAM_USAGE%" if (( $(echo "$CPU_LOAD >= $CPU_THRESHOLD" | bc -l) )); then echo "High CPU load detected: $CPU_LOAD%. Restarting..." sudo reboot fi if (( $(echo "$RAM_USAGE >= $RAM_THRESHOLD" | bc -l) )); then echo "High RAM usage detected: $RAM_USAGE%. Restarting..." sudo reboot fi sleep 1 # Check every second done
- Save the file:
- Press
CTRL + X
, thenY
, and hitEnter
to save the changes.
- Press
Step 3: Make the Script Executable
To allow the system to run the script, make it executable with this command:
sudo chmod +x /usr/local/bin/cpu_ram_monitor.sh
Step 4: Set the Script to Run at Startup
To ensure the script runs whenever your Raspberry Pi boots up, you can create a systemd service file.
- Create the service file:
sudo nano /etc/systemd/system/cpu_ram_monitor.service
- Add the following content to the file:
[Unit] Description=CPU and RAM Monitor Script After=network.target [Service] ExecStart=/usr/local/bin/cpu_ram_monitor.sh Restart=always [Install] WantedBy=multi-user.target
- Save and close the file:
- Press
CTRL + X
, thenY
, and hitEnter
.
- Press
- Enable the service so it starts automatically at boot:
sudo systemctl enable cpu_ram_monitor.service
- Start the service immediately (without rebooting):
sudo systemctl start cpu_ram_monitor.service
- Verify the service is running:
sudo systemctl status cpu_ram_monitor.service
Step 5: Test the Script
You can test if the script works by creating high CPU and RAM usage. For example: To stress the RAM:
stress --vm 1 --vm-bytes 1.7G --timeout 60
Or to simulate high web traffic load:
ab -n 1000 -c 25 https://yourdomain.com/
Verify the Reboot
After running these tests, check if the system has rebooted by using the following command:
uptime
Additionally, you can inspect the system logs to see reboot-related messages:
sudo dmesg | tail -n 20
Or:
cat /var/log/syslog | grep reboot
This guide provides an automated monitoring and restart mechanism for your Raspberry Pi, ensuring stability under high resource loads.