Keeping My Oracle Always Free Instance Alive

When you sign up for an Always Free Instance server on the Oracle Cloud, they tell you that your server will be shut down if it sits idle for too long. This doesn’t mean they delete it, it just means they turn it off as if you’d issued a shutdown command. You have to log into the control panel and start it back up. They do this to save cycles in their cloud. From their perspective, it’s expensive to have a bunch of servers just sitting around doing nothing. From my perspective, I don’t want my server to shut down. Having it sitting there waiting to perform an internet search for me whenever I ask it to do so is exactly what I set it up for. So, is there a way I can make it so that Oracle doesn’t see my server as being idle?

Oracle’s definition of an Always Free Instance being idle is:

  • CPU utilization for the 95th percentile is less than 10%
  • Network utilization is less than 10%
  • Memory utilization is less than 10% (applies to A1 shapes only)

The way I read the linked page, all three of those things must be true over a seven-day period for the instance to be identified as being idle. If I can make just one of them not true, then my instance won’t get shut down.

So I’ve written a Python script that will make sure that the CPU utilization on my server is always above 10%. It dynamically adjusts depending on what is happening on the system. When other processes are using lots of CPU, the script reduces the load it puts on the system, and vice versa. I’ve set things up so that this script is run as a systemd service so that it automatically starts at boot and is restarted if it ever fails.

After this script is installed and running the instance’s actual system load for the 95th percentile can be seen in the metrics graphs on the instance details page of the Oracle Cloud console.

#!/usr/bin/env python3

#  Place enough load on system to keep load average over 10%
#
#  Oracle stops any Always Free instances that are running at less
#  than 10% CPU utilization in the 95th percentile over seven days.

import time
import psutil
import logging
logging.basicConfig(format='%(asctime)s: %(levelname)s: %(message)s', datefmt='%d-%b-%y %H:%M:%S', level=logging.INFO)

#  How often should we write a log message?
log_interval = 300   # seconds

#  Arbitrary starting value based on trial and error, in nanoseconds
loop_interval = 230000000 
try:
    log_timer = time.time()
    while True:
        #  Go into a hard loop for an adjustable period of time
        timeout = time.perf_counter_ns() + loop_interval
        while time.perf_counter_ns() < timeout:
            pass
        #  Adjust the period of time up or down to acheive the desired CPU utilization
        cpu_percent = psutil.cpu_percent()
        if time.time() > log_timer + log_interval:
            log_timer = time.time()
            logging.info('CPU at {}% with an interval of {} nanoseconds'.format(cpu_percent, loop_interval))
        if cpu_percent > 13:
            loop_interval = loop_interval - 10000000
        if cpu_percent < 12:
            loop_interval = loop_interval + 10000000
        #  Sleep long enough to allow for interupts
        time.sleep(0.25)
except KeyboardInterrupt:
    logging.info('keyboard interrupt, terminating')
    exit()

dlk

One thought on “Keeping My Oracle Always Free Instance Alive”

  1. This is interesting. I think I used your browser a few months ago and i didn’t have any problems.
    You can probably see when I used it last. You are so smart Dad.❤️

Leave a Reply to DeVries Anna Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.