To prevent your Oracle Cloud VM from being deleted, you need to create a lightweight background process that maintains sufficient resource utilization. Here's the most effective solution:
Create a Resource Utilization Script
Create a script that will run continuously and maintain the required thresholds:
#!/bin/bash
# Save as /usr/local/bin/keep-vm-alive.sh
# Function to generate CPU load
generate_cpu_load() {
# Create CPU load for short bursts to hit 95th percentile requirement
# Run intensive task for 30 seconds every 5 minutes
while true; do
timeout 30s yes > /dev/null 2>&1 &
sleep 300 # Wait 5 minutes
done
}
# Function to generate network activity
generate_network_activity() {
while true; do
# Ping a reliable server and download small files periodically
ping -c 10 8.8.8.8 > /dev/null 2>&1
curl -s http://httpbin.org/bytes/1024 > /dev/null 2>&1
sleep 60 # Every minute
done
}
# Function to maintain memory usage
maintain_memory_usage() {
# Allocate and hold memory (adjust size based on your VM's RAM)
# This example uses about 200MB
python3 -c "
import time
data = []
# Allocate memory in chunks
for i in range(50):
data.append(' ' * (1024 * 1024 * 4)) # 4MB chunks
time.sleep(1)
# Keep memory allocated
while True:
time.sleep(3600) # Hold for 1 hour, then refresh
" &
}
# Start all functions in background
generate_cpu_load &
generate_network_activity &
maintain_memory_usage &
# Keep script running
wait
Set Up Automatic Startup
- Make the script executable:
sudo chmod +x /usr/local/bin/keep-vm-alive.sh
- Create a systemd service for automatic startup:
sudo nano /etc/systemd/system/keep-vm-alive.service
Add this content:
[Unit]
Description=Keep VM Alive Service
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/bin/keep-vm-alive.sh
Restart=always
RestartSec=10
User=root
[Install]
WantedBy=multi-user.target
- Enable and start the service:
sudo systemctl daemon-reload
sudo systemctl enable keep-vm-alive.service
sudo systemctl start keep-vm-alive.service
Alternative Simpler Approach
If you prefer a simpler solution, add these lines to your crontab:
crontab -e
Add:
# CPU spike every 5 minutes for 30 seconds
*/5 * * * * timeout 30s yes > /dev/null 2>&1
# Network activity every minute
* * * * * ping -c 5 8.8.8.8 > /dev/null 2>&1
# Memory allocation script (run once at startup)
@reboot python3 -c "data=[' '*(1024*1024*4) for i in range(50)]; import time; time.sleep(86400)" &
Key Points
- The CPU load is designed to create periodic spikes that will push the 95th percentile above 20%
- Network activity is lightweight but consistent
- Memory allocation happens at startup and maintains a baseline usage
- The systemd service ensures everything starts automatically on boot
- Resource usage is kept minimal to avoid impacting performance while meeting Oracle's thresholds
Monitor your VM's resource usage through the OCI console to verify the solution is working effectively. You may need to adjust the memory allocation size based on your specific VM configuration.