Getty Images

How to use startup scripts in Google Cloud

Google Cloud lets you use startup scripts when booting VMs to improve security and reliability. Follow these steps to create your own startup scripts.

Some teams opt to use startup scripts to improve the productivity, reliability and security of their Google Cloud environment. These scripts are files containing commands that automatically execute when you start a new Linux or Windows VM instance.

Some of the top reasons for cloud teams to use startup scripts are to do the following:

  • Save time. Automate the installation and configuration of software and other dependencies of your new VM instance.
  • Maintain consistency and reliability. Automate the performance of everyday operations tasks that teams must perform, such as installing software updates or running maintenance scripts, to ensure the Google Cloud environment is running smoothly. Also enforce configuration standards across VMs in your Google Cloud instance or corporate wide.
  • Watch cloud spending. Enable dynamic scaling in conjunction with Google Cloud's autoscaling feature. Create and configure new VM instances automatically to scale your application up or down to meet business or budgetary requirements.
Key elements of Google Cloud startup script
A properly running startup script should include these key elements.

Create a startup script in Google Cloud

When creating a new VM instance, programmers can specify startup scripts in the Google Cloud Console. Optionally they can also modify the metadata of an existing Google Cloud instance to add a startup script. Just as with other platforms, the exact elements of a Google Cloud startup will vary depending on the application it is intended to run.

Step 1. Use the text editor of your choice to create a new script file and save it on your local computer.

Step 2. Upload the script file to a Cloud Storage bucket to enable access to the script from your instance.

Step 3. Open the Google Cloud Console and navigate to the VM instances page.

Screenshot of the VM Instances tab in the Google Cloud console
The VM instance page shows yours available VMs.

Step 4. Select the VM instance in which you want to add the startup script.

Step 5. Click Edit at the top of the page and scroll to the Custom Metadata section.

Step 6. Click Add item and enter the following information:

  • Key: <startup-script>.
  • Value: the URL of the script file you uploaded to Cloud Storage, which will start with "gs://".
Enter the name and URL of your startup script in the key 1 and value 1 field
Enter the name of your startup script in the Key 1 field and its URL in the Value 1 field.

Step 7. Click Save to apply the changes to the VM instance.

Step 8. Start or restart the VM instance. The startup script executes automatically when the instance starts up.

There are three ways to determine if your startup script ran successfully:

  • Check the instance to ensure that it's running properly.
  • Check the script output for any error messages or other notifications that the script didn't run successfully.
  • Check your instance logs to verify the successful execution of the startup script.

Startup script best practices

When you create startup scripts, follow these tips to avoid common mistakes:

  • Specify the interpreter. For example, if you write a Python script, you must specify the interpreter as /bin/python.
  • Use absolute paths. Use absolute paths for any command line arguments to ensure the command can be found and run correctly.
  • Include the #! at the top of the script. The shebang -- #! -- acts as a special marker that specifies the interpreter that runs the script. Without it, the script may not run correctly.
  • Remember the correct script permissions. The startup script must be executable to run. Use the chmod command to specify the +x flag to set the correct permissions on the startup script.
  • Place the startup script in the correct location. The startup script must be in the right place to run automatically. Typically, the startup script location is var/run/google.startup.scripts.

Next Steps

How to create a Google Cloud Spot VM

Dig Deeper on Cloud provider platforms and tools

Data Center
ITOperations
SearchAWS
SearchVMware
Close