Overview
Power Guard (power_guard.sh) is a lightweight system monitoring script that scans for CPU-intensive processes and automatically terminates those exceeding a configurable threshold. It includes a whitelist feature to protect critical system processes.
This script is designed to prevent runaway processes from consuming excessive CPU resources, particularly useful on resource-constrained systems or when running long-running tasks.
Key Features
Automatic Process Management
Continuously monitors and terminates high-CPU processes automatically
Configurable CPU Threshold
Set custom CPU limit percentage to match your system requirements
System Process Protection
Whitelist critical system processes to prevent accidental termination
Graceful Termination
Uses SIGTERM (kill -15) for clean process shutdown
Configuration
The script uses two main configuration variables at the top of the file:CPU_LIMIT
The maximum CPU usage percentage allowed before a process is terminated.Processes using more than this percentage will be killed (unless whitelisted).
power_guard.sh
- Conservative
- Moderate
- Aggressive
Set
CPU_LIMIT=50 for systems with occasional high-CPU tasks that should be allowed to complete.WHITELIST
Pipe-separated regex pattern of process names that should never be killed.Critical system processes should always be included to prevent system instability.
power_guard.sh
Default Whitelisted Processes
Default Whitelisted Processes
- gnome-shell - GNOME desktop environment
- Xorg - X Window System display server
- systemd - System and service manager
- NetworkManager - Network connection manager
Adding to the Whitelist
To protect additional processes, add them to the pipe-separated list:Custom Whitelist
How It Works
The script follows a simple but effective process:Scan Processes
Uses
ps -eo pid,comm,%cpu --sort=-%cpu to list all processes sorted by CPU usage (highest first)Code Walkthrough
power_guard.sh
Understanding the Code
Understanding the Code
ps -eo pid,comm,%cpu --sort=-%cpu- Lists processes with PID, command name, and CPU usage, sorted descendingtail -n +2- Skips the header line from ps outputcpu_int=${cpu%.*}- Removes decimal from CPU percentage for integer comparison[[ $cpu_int -gt $CPU_LIMIT ]]- Tests if CPU exceeds limit[[ ! $name =~ $WHITELIST ]]- Tests if process is NOT in whitelist (regex match)kill -15 $pid- Sends SIGTERM for graceful shutdown
Usage
One-Time Scan
Run the script once to scan and kill high-CPU processes:Continuous Monitoring
For continuous monitoring, run the script in a loop with a delay:Background Monitoring
Run as a background daemon:Systemd Service
For automatic monitoring on system boot, create a systemd service:Signal Types
The script useskill -15 (SIGTERM) for graceful termination:
Understanding Kill Signals
Understanding Kill Signals
- SIGTERM (15) - Graceful termination (default, used by script)
- Allows process to clean up resources
- Process can catch and handle the signal
- Recommended for normal process termination
- SIGKILL (9) - Forceful termination (not used)
- Immediately terminates process
- Cannot be caught or handled
- May leave resources in inconsistent state
- Use only when SIGTERM fails
Customization Examples
Protect Browser Processes
Aggressive Power Saving
Development Workstation
Server Environment
Logging Enhancement
Add logging to track killed processes:Enhanced Version
Use Cases
Development Machines
Prevent runaway test processes or build tools from freezing your system
Shared Servers
Enforce fair resource usage among multiple users
CI/CD Runners
Prevent stuck build jobs from consuming resources indefinitely
Resource-Limited Systems
Maximize availability on VMs or containers with limited CPU
Limitations and Considerations
Troubleshooting
Permission Denied
If you see “Operation not permitted”:Process Keeps Restarting
If a killed process immediately restarts:- Find the parent process or service manager
- Stop the service/parent instead:
Whitelist Not Working
Ensure the process name matches exactly:Related Scripts
Script Template
Use the standardized template for creating similar monitoring scripts
GDrive Ingest
Another example of a well-structured bash script with progress monitoring