Intro

We can configure a bash startup script that automatically runs on a instance immediately after the instance becomes active.

Example

We can configure a bash startup script using Shadeform’s Create Instance API. In this example, we will run the script below once the instance becomes ready.

Script
#!/bin/bash
counter=1

# Infinite loop
while true; do
  echo $counter
  # Increment the counter
  ((counter++))
  # Wait for 3 seconds
  sleep 3
done

First, we must Base64 encode the script to preserve all special characters, whitespacing, and formatting of the script. We recommend using this tool to base64 encode our script. After base64 encoding the script, you should get the following:

Base64 Encoded Script
IyEvYmluL2Jhc2gKY291bnRlcj0xCgojIEluZmluaXRlIGxvb3AKd2hpbGUgdHJ1ZTsgZG8KICBlY2hvICRjb3VudGVyCiAgIyBJbmNyZW1lbnQgdGhlIGNvdW50ZXIKICAoKGNvdW50ZXIrKykpCiAgIyBXYWl0IGZvciAzIHNlY29uZHMKICBzbGVlcCAzCmRvbmUK

After we have base64 encoded our script, we can create a new GPU instance with the startup script included in the base64_script property.

Once the instance is created, we can get the status and IP address of the instance by calling the Instances API. Once the instance is active, we can verify that the script ran correctly by SSHing into the instance and running:

CLI
journalctl -u init-script -f

Security

The script provided is encrypted at rest.