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.


Update – September 2026

The script now includes a details <instance_name> command, which displays the instance’s main compute, operating system, network, and storage information.

#!/usr/bin/env bash

set -euo pipefail

# Compartment nel quale cercare le istanze.

COMPARTMENT_ID="ocid1.compartment.oc1..aaaaaaaadjghns62toal56nat2gt33diyle5xci7ofvzdsyxn57yqjhdc5ja"

# Se usi un profilo OCI specifico, scommenta e modifica questa riga:

# OCI_PROFILE="DEFAULT"

usage() {

  echo "Uso:"

  echo "  $0 list"

  echo "  $0 details <nome_istanza[,nome_istanza,...]>"

  echo "  $0 start <nome_istanza[,nome_istanza,...]>"

  echo "  $0 stop <nome_istanza[,nome_istanza,...]>"

  echo "  $0 reset <nome_istanza[,nome_istanza,...]>"

}

oci_cmd() {

  if [[ -n "${OCI_PROFILE:-}" ]]; then

    oci --profile "$OCI_PROFILE" "$@"

  else

    oci "$@"

  fi

}

value_or_dash() {

  local value="${1:-}"

  if [[ -z "$value" || "$value" == "null" || "$value" == "[]" ]]; then

    printf '%s' '-'

  else

    printf '%s' "$value"

  fi

}

find_instance_id() {

  local instance_name="$1"

  local match_count

  match_count=$(oci_cmd compute instance list \
    --compartment-id "$COMPARTMENT_ID" \
    --display-name "$instance_name" \
    --all \
    --query 'length(data[?"lifecycle-state" != `TERMINATED`])' \
    --raw-output)

  if [[ "$match_count" -eq 0 ]]; then

    echo "Istanza non trovata: $instance_name" >&2

    return 1

  fi

  if [[ "$match_count" -gt 1 ]]; then

    echo "Trovate più istanze non terminate con nome: $instance_name" >&2

    echo "Usa la tabella seguente per individuare e rinominare quella corretta:" >&2

    oci_cmd compute instance list \
      --compartment-id "$COMPARTMENT_ID" \
      --display-name "$instance_name" \
      --all \
      --query 'data[?"lifecycle-state" != `TERMINATED`].{Name:"display-name",State:"lifecycle-state",Id:id}' \
      --output table >&2

    return 1

  fi

  oci_cmd compute instance list \
    --compartment-id "$COMPARTMENT_ID" \
    --display-name "$instance_name" \
    --all \
    --query 'data[?"lifecycle-state" != `TERMINATED`] | [0].id' \
    --raw-output

}

show_details() {

  local instance_id="$1"

  local instance_name="$2"

  local state shape ocpus memory availability_domain fault_domain

  local image_id image_name operating_system operating_system_version time_created

  local vnic_id hostname mac_address private_ip public_ip subnet_id

  local subnet_name subnet_cidr vcn_id vcn_name vcn_cidrs

  local boot_volume_id boot_volume_name boot_volume_size boot_volume_vpus

  state=$(oci_cmd compute instance get --instance-id "$instance_id" \
    --query 'data."lifecycle-state"' --raw-output)

  shape=$(oci_cmd compute instance get --instance-id "$instance_id" \
    --query 'data.shape' --raw-output)

  ocpus=$(oci_cmd compute instance get --instance-id "$instance_id" \
    --query 'data."shape-config".ocpus' --raw-output)

  memory=$(oci_cmd compute instance get --instance-id "$instance_id" \
    --query 'data."shape-config"."memory-in-gbs"' --raw-output)

  availability_domain=$(oci_cmd compute instance get --instance-id "$instance_id" \
    --query 'data."availability-domain"' --raw-output)

  fault_domain=$(oci_cmd compute instance get --instance-id "$instance_id" \
    --query 'data."fault-domain"' --raw-output)

  image_id=$(oci_cmd compute instance get --instance-id "$instance_id" \
    --query 'data."image-id"' --raw-output)

  time_created=$(oci_cmd compute instance get --instance-id "$instance_id" \
    --query 'data."time-created"' --raw-output)

  image_name="-"

  operating_system="-"

  operating_system_version="-"

  if [[ -n "$image_id" && "$image_id" != "null" ]]; then

    image_name=$(oci_cmd compute image get --image-id "$image_id" \
      --query 'data."display-name" || `"-"`' --raw-output)

    operating_system=$(oci_cmd compute image get --image-id "$image_id" \
      --query 'data."operating-system" || `"-"`' --raw-output)

    operating_system_version=$(oci_cmd compute image get --image-id "$image_id" \
      --query 'data."operating-system-version" || `"-"`' --raw-output)

  fi

  # list-vnics restituisce direttamente le VNIC e indica quale e' la primaria.

  vnic_id=$(oci_cmd compute instance list-vnics \
    --instance-id "$instance_id" \
    --all \
    --query 'data[?"is-primary" == `true`] | [0].id' \
    --raw-output)

  hostname="-"

  mac_address="-"

  private_ip="-"

  public_ip="-"

  subnet_id="-"

  subnet_name="-"

  subnet_cidr="-"

  vcn_id="-"

  vcn_name="-"

  vcn_cidrs="-"

  if [[ -n "$vnic_id" && "$vnic_id" != "null" ]]; then

    hostname=$(oci_cmd network vnic get --vnic-id "$vnic_id" \
      --query 'data."hostname-label"' --raw-output)

    mac_address=$(oci_cmd network vnic get --vnic-id "$vnic_id" \
      --query 'data."mac-address"' --raw-output)

    private_ip=$(oci_cmd network vnic get --vnic-id "$vnic_id" \
      --query 'data."private-ip"' --raw-output)

    public_ip=$(oci_cmd network vnic get --vnic-id "$vnic_id" \
      --query 'data."public-ip" || `"-"`' --raw-output)

    subnet_id=$(oci_cmd network vnic get --vnic-id "$vnic_id" \
      --query 'data."subnet-id"' --raw-output)

    if [[ -n "$subnet_id" && "$subnet_id" != "null" ]]; then

      subnet_name=$(oci_cmd network subnet get --subnet-id "$subnet_id" \
        --query 'data."display-name"' --raw-output)

      subnet_cidr=$(oci_cmd network subnet get --subnet-id "$subnet_id" \
        --query 'data."cidr-block"' --raw-output)

      vcn_id=$(oci_cmd network subnet get --subnet-id "$subnet_id" \
        --query 'data."vcn-id"' --raw-output)

      if [[ -n "$vcn_id" && "$vcn_id" != "null" ]]; then

        vcn_name=$(oci_cmd network vcn get --vcn-id "$vcn_id" \
          --query 'data."display-name"' --raw-output)

        vcn_cidrs=$(oci_cmd network vcn get --vcn-id "$vcn_id" \
          --query 'join(`, `, data."cidr-blocks")' --raw-output)

      fi

    fi

  fi

  # availability-domain e' obbligatorio per boot-volume-attachment list.

  boot_volume_id=$(oci_cmd compute boot-volume-attachment list \
    --availability-domain "$availability_domain" \
    --compartment-id "$COMPARTMENT_ID" \
    --instance-id "$instance_id" \
    --all \
    --query 'data[?"lifecycle-state" == `ATTACHED`] | [0]."boot-volume-id"' \
    --raw-output)

  boot_volume_name="-"

  boot_volume_size="-"

  boot_volume_vpus="-"

  if [[ -n "$boot_volume_id" && "$boot_volume_id" != "null" ]]; then

    boot_volume_name=$(oci_cmd bv boot-volume get \
      --boot-volume-id "$boot_volume_id" \
      --query 'data."display-name"' --raw-output)

    boot_volume_size=$(oci_cmd bv boot-volume get \
      --boot-volume-id "$boot_volume_id" \
      --query 'data."size-in-gbs"' --raw-output)

    boot_volume_vpus=$(oci_cmd bv boot-volume get \
      --boot-volume-id "$boot_volume_id" \
      --query 'data."vpus-per-gb"' --raw-output)

  fi

  echo

  echo "Dettagli istanza"

  echo "-----------------"

  printf '%-24s %s\n' "Nome:" "$instance_name"

  printf '%-24s %s\n' "Stato:" "$(value_or_dash "$state")"

  printf '%-24s %s\n' "Shape:" "$(value_or_dash "$shape")"

  printf '%-24s %s\n' "OCPU:" "$(value_or_dash "$ocpus")"

  printf '%-24s %s GB\n' "Memoria:" "$(value_or_dash "$memory")"

  printf '%-24s %s\n' "Availability Domain:" "$(value_or_dash "$availability_domain")"

  printf '%-24s %s\n' "Fault Domain:" "$(value_or_dash "$fault_domain")"

  printf '%-24s %s\n' "Creata il:" "$(value_or_dash "$time_created")"

  printf '%-24s %s\n' "Instance OCID:" "$instance_id"

  printf '%-24s %s\n' "Sistema operativo:" "$(value_or_dash "$operating_system")"

  printf '%-24s %s\n' "Versione OS:" "$(value_or_dash "$operating_system_version")"

  printf '%-24s %s\n' "Immagine:" "$(value_or_dash "$image_name")"

  printf '%-24s %s\n' "Image OCID:" "$(value_or_dash "$image_id")"

  echo

  echo "Rete (VNIC primaria)"

  echo "--------------------"

  printf '%-24s %s\n' "Hostname:" "$(value_or_dash "$hostname")"

  printf '%-24s %s\n' "MAC address:" "$(value_or_dash "$mac_address")"

  printf '%-24s %s\n' "VNIC OCID:" "$(value_or_dash "$vnic_id")"

  printf '%-24s %s\n' "IP privato:" "$(value_or_dash "$private_ip")"

  printf '%-24s %s\n' "IP pubblico:" "$(value_or_dash "$public_ip")"

  printf '%-24s %s\n' "Subnet:" "$(value_or_dash "$subnet_name")"

  printf '%-24s %s\n' "Subnet CIDR:" "$(value_or_dash "$subnet_cidr")"

  printf '%-24s %s\n' "Subnet OCID:" "$(value_or_dash "$subnet_id")"

  printf '%-24s %s\n' "VCN:" "$(value_or_dash "$vcn_name")"

  printf '%-24s %s\n' "VCN CIDR:" "$(value_or_dash "$vcn_cidrs")"

  printf '%-24s %s\n' "VCN OCID:" "$(value_or_dash "$vcn_id")"

  echo

  echo "Boot volume"

  echo "-----------"

  printf '%-24s %s\n' "Nome:" "$(value_or_dash "$boot_volume_name")"

  printf '%-24s %s GB\n' "Dimensione:" "$(value_or_dash "$boot_volume_size")"

  printf '%-24s %s\n' "VPUs per GB:" "$(value_or_dash "$boot_volume_vpus")"

  printf '%-24s %s\n' "Boot volume OCID:" "$(value_or_dash "$boot_volume_id")"

}

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[?"lifecycle-state" != `TERMINATED`].{Name:"display-name",State:"lifecycle-state",Id:id}' \
      --output table

    ;;

  DETAILS|START|STOP|RESET)

    if [[ $# -lt 2 ]]; then

      usage

      exit 1

    fi

    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

    IFS=',' read -r -a INSTANCE_NAMES <<< "$2"
    EXIT_STATUS=0

    for INSTANCE_NAME in "${INSTANCE_NAMES[@]}"; do
      # Rimuove gli spazi intorno ai nomi, consentendo anche "k81, k82".
      INSTANCE_NAME="${INSTANCE_NAME#"${INSTANCE_NAME%%[![:space:]]*}"}"
      INSTANCE_NAME="${INSTANCE_NAME%"${INSTANCE_NAME##*[![:space:]]}"}"

      if [[ -z "$INSTANCE_NAME" ]]; then
        echo "Nome istanza non valido nell'elenco: $2" >&2
        EXIT_STATUS=1
        continue
      fi

      if ! INSTANCE_ID=$(find_instance_id "$INSTANCE_NAME"); then
        EXIT_STATUS=1
        continue
      fi

      if [[ "$COMMAND" == "DETAILS" ]]; then
        show_details "$INSTANCE_ID" "$INSTANCE_NAME"
        continue
      fi

      CURRENT_STATE=$(oci_cmd compute instance get \
        --instance-id "$INSTANCE_ID" \
        --query 'data."lifecycle-state"' \
        --raw-output)

      if [[ "$COMMAND" == "START" && "$CURRENT_STATE" == "RUNNING" ]]; then
        echo "L'istanza $INSTANCE_NAME e' gia' RUNNING."
        continue
      fi

      if [[ "$COMMAND" == "STOP" && "$CURRENT_STATE" == "STOPPED" ]]; then
        echo "L'istanza $INSTANCE_NAME e' gia' STOPPED."
        continue
      fi

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

      if ! oci_cmd compute instance action \
        --instance-id "$INSTANCE_ID" \
        --action "$OCI_ACTION" \
        --wait-for-state "$WAIT_STATE"; then
        echo "Operazione non riuscita per: $INSTANCE_NAME" >&2
        EXIT_STATUS=1
        continue
      fi

      echo "Operazione completata per: $INSTANCE_NAME"
    done

    exit "$EXIT_STATUS"

    ;;

  *)

    usage

    exit 1

    ;;

esac

Now make it executable and run it:

chmod +x oci-inst-ctl.sh
./oci-inst-ctl.sh list
./oci-inst-ctl.sh details web-server-01,web-server-02

Example output:

Dettagli istanza
-----------------
Nome:                   web-server-01
Stato:                  RUNNING
Shape:                  VM.Standard.A1.Flex
OCPU:                   3.0
Memoria:                16.0 GB
Sistema operativo:      Oracle Linux
Versione OS:            9
Availability Domain:    EU-FRANKFURT-1-AD-1
Fault Domain:           FAULT-DOMAIN-2

Rete (VNIC primaria)
--------------------
IP privato:             10.10.1.106
IP pubblico:            203.0.113.25
Subnet:                 private-subnet
Subnet CIDR:            10.10.1.0/24
VCN:                    production-vcn
VCN CIDR:               10.10.0.0/16

Boot volume
-----------
Nome:                   web-server-01 (Boot Volume)
Dimensione:             50 GB
VPUs per GB:            10



Dettagli istanza
-----------------
Nome:                   web-server-02
Stato:                  RUNNING
Shape:                  VM.Standard.A1.Flex
OCPU:                   3.0
Memoria:                16.0 GB
Sistema operativo:      Oracle Linux
Versione OS:            9
Availability Domain:    EU-FRANKFURT-1-AD-1
Fault Domain:           FAULT-DOMAIN-3

Rete (VNIC primaria)
--------------------
IP privato:             10.10.1.107
IP pubblico:            203.0.113.63
Subnet:                 private-subnet
Subnet CIDR:            10.10.1.0/24
VCN:                    production-vcn
VCN CIDR:               10.10.0.0/16

Boot volume
-----------
Nome:                   web-server-02 (Boot Volume)
Dimensione:             50 GB
VPUs per GB:            10

The complete output also includes the instance, image, VNIC, subnet, VCN, and boot volume OCIDs.

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