To ensure your Oracle Cloud Infrastructure (OCI) virtual machine (VM) is not deleted due to low utilization, you'll need to implement a solution that periodically generates minimal but sufficient CPU, network, and memory activity to stay above the 20% thresholds. Here's a recommended approach:
Solution Overview:
- Create a lightweight background script that:
- Generates minimal CPU load (>20% 95th percentile).
- Performs small network transfers (>20% utilization).
- Allocates and uses a small portion of memory (>20% utilization).
- Deploy the script as a systemd service to ensure it runs automatically at startup.
Step-by-Step Implementation:
1. Create the Script (prevent_shutdown.sh)
Save this script to /usr/local/bin/prevent_shutdown.sh:
#!/bin/bash
# Configuration
MEMORY_TARGET_PERCENT=25 # Target memory usage (above 20%)
CPU_LOAD_PERCENT=25 # Target CPU load (above 20% 95th percentile)
NETWORK_ACTIVITY_INTERVAL=60 # Network check interval (seconds)
TEST_FILE_URL="http://speedtest.tele2.net/10MB.zip" # Small file for network activity
# Function to generate CPU load
generate_cpu_load() {
while true; do
timeout 1 yes > /dev/null # Generates CPU load for 1 second
sleep 4 # Sleep to average ~20% CPU
done
}
# Function to generate memory load
generate_memory_load() {
MEMORY_TOTAL=$(free -b | awk '/Mem:/ {print $2}')
MEMORY_TARGET=$((MEMORY_TOTAL * MEMORY_TARGET_PERCENT / 100))
while true; do
# Allocate and use memory in a loop
stress-ng --vm 1 --vm-bytes ${MEMORY_TARGET} --vm-keep --timeout 10s
sleep 10
done
}
# Function to generate network activity
generate_network_activity() {
while true; do
# Download a small file and delete it
curl -s -o /dev/null "$TEST_FILE_URL"
sleep $NETWORK_ACTIVITY_INTERVAL
done
}
# Start all load generators in the background
generate_cpu_load &
generate_memory_load &
generate_network_activity &
# Keep the script running
wait
2. Install Dependencies
The script uses stress-ng and curl. Install them if missing:
sudo apt-get update
sudo apt-get install -y stress-ng curl
3. Make the Script Executable
sudo chmod +x /usr/local/bin/prevent_shutdown.sh
4. Create a Systemd Service (prevent-shutdown.service)
Save this to /etc/systemd/system/prevent-shutdown.service:
[Unit]
Description=Prevent OCI VM Shutdown Due to Low Utilization
After=network.target
[Service]
ExecStart=/usr/local/bin/prevent_shutdown.sh
Restart=always
User=root
[Install]
WantedBy=multi-user.target
5. Enable and Start the Service
sudo systemctl daemon-reload
sudo systemctl enable prevent-shutdown
sudo systemctl start prevent-shutdown
Key Features of This Solution:
- CPU Load: Uses
yes command in short bursts to keep 95th percentile >20%.
- Memory Load: Uses
stress-ng to allocate and hold ~25% of RAM.
- Network Activity: Downloads a small file periodically (adjust
NETWORK_ACTIVITY_INTERVAL as needed).
- Automatic Startup: The systemd service ensures the script runs at boot and restarts if killed.
Verification:
- Check CPU usage:
top or mpstat 1
- Check memory usage:
free -m
- Check network activity:
iftop or nload
- Check service status:
sudo systemctl status prevent-shutdown
Notes:
- Adjust intervals/percentages based on your VM's specs.
- Test in a non-production environment first.
- The script is lightweight but monitor resource usage initially.
This solution ensures your VM consistently meets OCI's utilization requirements without significant overhead.