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.

This Post Has 2 Comments

  1. PUTI

    How does using OCI CLI automation scripts improve the efficiency of managing Oracle Cloud Infrastructure instances compared to manually using the web console?

  2. admin

    Great point. Using OCI CLI automation scripts can significantly improve the efficiency of managing Oracle Cloud Infrastructure instances, especially when the same operations need to be performed repeatedly or across multiple resources.

    With automation, tasks such as starting, stopping, provisioning, tagging, or monitoring instances can be executed consistently through a single script, reducing the time spent navigating the web console and minimizing the risk of manual errors. Scripts can also be reused, version-controlled, scheduled, and integrated into CI/CD pipelines, making infrastructure management more scalable and repeatable.

    The OCI web console is still very useful for visual monitoring, troubleshooting, and occasional one-off changes. However, for routine operations, large environments, and standardized deployments, CLI automation provides greater speed, consistency, and control.

Leave a Reply