What are Shutdown Scripts?

When you launch an instance in Google cloud, you have the option of passing user data to the instance that can be used to perform common automated configuration tasks and even run scripts after the instance starts.

How It Works

To enable the Shutdown script, an agent must be installed on the instance during startup.

This is done via the user-data entry and will have the agent up and running on the instance within a few seconds. Your

shutdown script will be deployed to every new instance on your Elastigroup.

Our agent will sample the API for updates every 10 seconds to figure out if the script should run.

Once we determine we are going to remove or replace the instance we will notify the agent so the script can execute. This means that the script will have around 10 minutes to execute prior to the instance termination.

Things to Note:
  • The script will install pip (Python 2.7 Package Manager) automatically, in order for Spotinst agent to be installed and for your shutdown scripts to function properly.
  • Shutdown scripts must start with the #! characters and the path to the interpreter you want to read the script (commonly /bin/bash).
  • Scripts entered as Shutdown scripts are executed as the root user, it is not necessary to use sudo in the script. Remember that any files you create will be owned by root; if you need non-root users to have file access, you should modify the permissions accordingly in the script. Also, because the script doesn’t run interactively, you cannot include commands that require user feedback (such as rm without the -f flag).
  • Adding the agent installation at boot time adds to the amount of time it takes to boot the instance. Please make sure the shutdown script installation code is the last step in your user-data. You should allow a few minutes of extra time for the tasks to complete before you test that the user script has finished successfully.
Adding a Shutdown Script

Navigate to the Compute Tab and scroll to Launch Specification settings.


Step 1:

Install the Spotinst agent. For ease of use, we recommend adding this at the end of your startup script.

Note: you need to provide your API token and Account id in the script.

For Linux:

#!/usr/bin/env bash
curl -fsSL https://s3.amazonaws.com/spotinst-public/services/agent/gcp-elastigroup-agent-init.sh | \

For Debian:

#!/usr/bin/env bash
wget -q -O- https://s3.amazonaws.com/spotinst-public/services/agent/gcp-elastigroup-agent-init.sh | \
Step 2:

Add your shell script to the shutdown script box.



#enter shutdown script here
#!/usr/bin/env bash
"Goodbye old instance"

Review and update your Elastigroup to save the change.