Automatically Start Stopped Oracle Instance Remotely

Feb 9, 2024, by Rovshan Mirza

If you are using one of the free-tier Oracle VMs without subscribing to Pay As You Go paid account, then you might have noticed that Oracle periodically shuts down your Always Free instances to reclaim them if they were in "idle" state for 7 days. By idle they mean CPU, Memory and Network utilization below certain percentage. This threshold used to be 10% in the beginning, but then they increased it to 15%, and now it's 20%. You can read about it here.

One of the methods of circumventing this limitation was to use synthetic load generator apps like lookbusy to keep CPU and Memory utilization above the idle threshold limit. But for some reason, this never worked for me. Oracle just kept stopping my instances promptly. So, it became very annoying when the services weren't running and I had to start the instances manually.

Anyway, I found a workround for this problem, which is running a small script to check if Oracle instance is up and running, and if not, it will start the instance automatically. For this to work, you'll need to do the following:

  1. Have OCI CLI installed on any Linux VM/container outside of Oracle.
  2. Schedule a cron job to run a script on the Linux VM/container.

1. Install OCI CLI on Linux VM/container outside of Oracle

If you are already using a cloud Linux VPS from another cloud provider, you can install OCI CLI there. I'm using my old PC as a Proxmox server, so I created LXC container there to run the script.

To install OCI CLI, you can read Install Oracle OCI CLI on Linux VM/container guide I wrote yesterday.

2. Schedule a cron job

  1. Create a bash script in your home directory using nano or any other text editor.
nano us-ashburn-1_start.sh
  1. Add below script. Update [server-ip-address] and [instance-ocid]. You can find this information in Computer > Instances in your Oracle Cloud account. This script will append logs to crontab_log.txt file.
#! /bin/bash

server="[server-ip-address]"

# check if server is running
if nc -w 2 -z $server 22 2>/dev/null; then
echo "$server ^|^s up"
echo `date` ": server is up" >> crontab_log.txt
else
echo "$server ^|^w down" >> crontab_log.txt
/root/bin/oci compute instance action --action START --instance-id [instance-ocid]
echo `date` ": server started" >>crontab_log.txt
fi
  1. Make the script file executable.
chmod +x us-ashburn-1_start.sh
  1. Open crontab file.
crontab -e
  1. Add below line in the bottom of the file. This will run the script every 5 minutes. You can change it to 1 minute if you like.
*/5 * * * * sh us-ashburn-1_start.sh
  1. You can now try stopping your instance and see if the script it working.