Skip to content

Instantly share code, notes, and snippets.

@alisade
Created July 18, 2025 18:18
Show Gist options
  • Save alisade/0c25743fd67cf43fe25289e61dcaad0c to your computer and use it in GitHub Desktop.
Save alisade/0c25743fd67cf43fe25289e61dcaad0c to your computer and use it in GitHub Desktop.
#!/bin/bash
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
set -euo pipefail
# This script is invoked in /etc/cron.d/c9-automatic-shutdown
# which uses the full path to the file: /home/<username>/.c9/stop-if-inactive.sh
# Parse the username from the file path, which can vary based on the Cloud9's OS.
USER=$(echo $0 | cut -d'/' -f3)
CONFIG=$(cat /home/$USER/.c9/autoshutdown-configuration)
SHUTDOWN_TIMEOUT=${CONFIG#*=}
if ! [[ $SHUTDOWN_TIMEOUT =~ ^[0-9]*$ ]]; then
echo "shutdown timeout is invalid"
exit 1
fi
is_shutting_down() {
is_shutting_down_ubuntu &> /dev/null || is_shutting_down_al1 &> /dev/null || is_shutting_down_al2 &> /dev/null || is_shutting_down_al2023 &> /dev/null
}
is_shutting_down_ubuntu() {
local TIMEOUT
TIMEOUT=$(busctl get-property org.freedesktop.login1 /org/freedesktop/login1 org.freedesktop.login1.Manager ScheduledShutdown)
if [ "$?" -ne "0" ]; then
return 1
fi
local SHUTDOWN_TIMESTAMP
SHUTDOWN_TIMESTAMP="$(echo $TIMEOUT | awk "{print \$3}")"
if [ $SHUTDOWN_TIMESTAMP == "0" ] || [ $SHUTDOWN_TIMESTAMP == "18446744073709551615" ]; then
return 1
else
return 0
fi
}
is_shutting_down_al1() {
pgrep shutdown
}
is_shutting_down_al2() {
local FILE
FILE=/run/systemd/shutdown/scheduled
if [[ -f "$FILE" ]]; then
return 0
else
return 1
fi
}
is_shutting_down_al2023() {
local TIMEOUT
TIMEOUT=$(busctl get-property org.freedesktop.login1 /org/freedesktop/login1 org.freedesktop.login1.Manager ScheduledShutdown)
if [ "$?" -ne "0" ]; then
return 1
fi
local SHUTDOWN_TIMESTAMP
SHUTDOWN_TIMESTAMP="$(echo $TIMEOUT | awk "{print \$3}")"
if [ $SHUTDOWN_TIMESTAMP == "0" ] || [ $SHUTDOWN_TIMESTAMP == "18446744073709551615" ]; then
return 1
else
return 0
fi
}
is_vfs_connected() {
pgrep -f vfs-worker >/dev/null
}
is_vscode_connected() {
pgrep -u $USER -f .vscode-server/bin/ -a | grep -v -F 'shellIntegration-bash.sh' >/dev/null
}
if is_shutting_down; then
if [[ ! $SHUTDOWN_TIMEOUT =~ ^[0-9]+$ ]] || is_vfs_connected || is_vscode_connected; then
sudo shutdown -c
echo > "/home/$USER/.c9/autoshutdown-timestamp"
else
TIMESTAMP=$(date +%s)
echo "$TIMESTAMP" > "/home/$USER/.c9/autoshutdown-timestamp"
fi
else
if [[ $SHUTDOWN_TIMEOUT =~ ^[0-9]+$ ]] && ! is_vfs_connected && ! is_vscode_connected; then
# Get the current instance ID from EC2 metadata
INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
# Get the Cloud9 environment ID from the instance tags
ENV_ID=$(aws ec2 describe-instances --instance-ids $INSTANCE_ID --query 'Reservations[0].Instances[0].Tags[?Key==`aws:cloud9:environment`].Value' --output text)
# Delete the Cloud9 environment instead of just terminating the instance
aws cloud9 delete-environment --environment-id $ENV_ID
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment