You are currently viewing Fast OCI Instance Status Management from Your Terminal

Fast OCI Instance Status Management from Your Terminal

Managing Oracle Cloud Infrastructure instances does not always require opening the web console. With a simple script and the OCI CLI, you can start, stop, reset, and list your instances directly from your terminal in seconds.

Prerequisites

Before using the script, make sure you have:

The Script

Below is a simple script that lets you list instances and perform actions using their names.

#!/usr/bin/env bash
set -euo pipefail

# Change with your OCID compartment
COMPARTMENT_ID="ocid1.compartment.oc1..xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

# Uncomment and change it if you use a different OCI_PROFILE
# OCI_PROFILE="DEFAULT"

usage() {
  echo "Uso:"
  echo "  $0 list"
  echo "  $0 START <nome_istanza>"
  echo "  $0 STOP <nome_istanza>"
  echo "  $0 RESET <nome_istanza>"
}

oci_cmd() {
  if [[ -n "${OCI_PROFILE:-}" ]]; then
    oci --profile "$OCI_PROFILE" "$@"
  else
    oci "$@"
  fi
}

if [[ $# -lt 1 ]]; then
  usage
  exit 1
fi

COMMAND="${1^^}"

case "$COMMAND" in
  LIST)
    echo "Istanze nel compartment:"
    oci_cmd compute instance list \
      --compartment-id "$COMPARTMENT_ID" \
      --all \
      --query 'data[].{Name:"display-name",State:"lifecycle-state",Id:id}' \
      --output table
    ;;

  START|STOP|RESET)
    if [[ $# -lt 2 ]]; then
      usage
      exit 1
    fi

    INSTANCE_NAME="$2"

    case "$COMMAND" in
      START)
        OCI_ACTION="START"
        WAIT_STATE="RUNNING"
        ;;
      STOP)
        OCI_ACTION="STOP"
        WAIT_STATE="STOPPED"
        ;;
      RESET)
        OCI_ACTION="RESET"
        WAIT_STATE="RUNNING"
        ;;
    esac

    INSTANCE_ID=$(oci_cmd compute instance list \
      --compartment-id "$COMPARTMENT_ID" \
      --all \
      --query "data[?\"display-name\"=='$INSTANCE_NAME'].id | [0]" \
      --raw-output)

    if [[ -z "$INSTANCE_ID" || "$INSTANCE_ID" == "null" ]]; then
      echo "Istanza non trovata: $INSTANCE_NAME"
      exit 1
    fi

    echo "Eseguo $COMMAND su: $INSTANCE_NAME"
    echo "Instance OCID: $INSTANCE_ID"

    oci_cmd compute instance action \
      --instance-id "$INSTANCE_ID" \
      --action "$OCI_ACTION" \
      --wait-for-state "$WAIT_STATE"

    echo "Operazione completata."
    ;;

  *)
    usage
    exit 1
    ;;
esac

Make the script executable:

chmod +x oci-inst-ctl.sh

How to Use It

List your instances:

./oci-inst-ctl.sh list

Example output:

+----------------+-----------+------------------------------------------+
| Name           | State     | Id                                       |
+----------------+-----------+------------------------------------------+
| web-server-01  | RUNNING   | ocid1.instance.oc1.eu-madrid-1.xxxxxxxx  |
| db-server-01   | STOPPED   | ocid1.instance.oc1.eu-madrid-1.yyyyyyyy  |
+----------------+-----------+------------------------------------------+

Start an instance:

./oci-inst-ctl.sh START web-server-01

Stop an instance:

./oci-inst-ctl.sh STOP web-server-01

Reset an instance:

./oci-inst-ctl.sh RESET web-server-01

Final Thoughts

This approach removes the need to log into the OCI console for simple operations. It is fast, scriptable, and ideal for daily workflows or automation tasks.

Once you start using it, going back to the web interface for basic instance control will feel slow.

Leave a Reply