Created
April 28, 2026 16:09
-
-
Save vkalra/7ade3e02b9a48996b7a35f28d07f8020 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| set -euo pipefail | |
| COMPARTMENT_ID="ocid1.compartment.oc1.." | |
| TIMEOUT_SECONDS=60 | |
| # Pull only running instances in the compartment. | |
| oci compute instance list \ | |
| --compartment-id "$COMPARTMENT_ID" \ | |
| --query 'data[?"lifecycle-state"==`RUNNING`].id' \ | |
| --raw-output | tr -d '[]",' | while read -r INSTANCE_ID; do | |
| [ -z "$INSTANCE_ID" ] && continue | |
| echo "Checking Compute Instance Run Command plugin status for $INSTANCE_ID" | |
| RUN_COMMAND_STATUS=$(oci instance-agent plugin list \ | |
| --compartment-id "$COMPARTMENT_ID" \ | |
| --instanceagent-id "$INSTANCE_ID" \ | |
| --query 'data[?name==`Compute Instance Run Command`].status | [0]' \ | |
| --raw-output) | |
| if [ "$RUN_COMMAND_STATUS" != "RUNNING" ]; then | |
| echo "Skipping $INSTANCE_ID: Compute Instance Run Command status is '$RUN_COMMAND_STATUS'" | |
| continue | |
| fi | |
| printf -v TARGET '{\n "instanceId": "%s"\n}' "$INSTANCE_ID" | |
| #echo $TARGET | |
| COMMAND_ID=$(oci instance-agent command create \ | |
| --compartment-id "$COMPARTMENT_ID" \ | |
| --display-name "Uptime check for $INSTANCE_ID" \ | |
| --content file://content.json \ | |
| --target "$TARGET" \ | |
| --timeout-in-seconds "$TIMEOUT_SECONDS" \ | |
| --query "data.id" \ | |
| --raw-output) | |
| echo "Created command: $COMMAND_ID for instance: $INSTANCE_ID" | |
| oci instance-agent command-execution get \ | |
| --command-id "$COMMAND_ID" \ | |
| --instance-id "$INSTANCE_ID" \ | |
| --raw-output | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment