Some teams choose to use startup scripts to improve the productivity, reliability, and security of their Google Cloud environment. These scripts are files that contain commands that are automatically executed when you start a new Linux or Windows virtual machine instance.
Some of the main reasons cloud teams use startup scripts are:
- 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 daily operations tasks that teams need to perform, such as installing software updates or running maintenance scripts, to ensure that your Google Cloud environment runs smoothly. Also apply configuration standards across all virtual machines in your Google Cloud instance or across the enterprise.
- Look at 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 budget requirements.

Create a startup script in Google Cloud
When creating a new VM instance, developers can specify startup scripts in the Google Cloud Console. They can also optionally modify the metadata of an existing Google Cloud instance to add a startup script. As with other platforms, the exact elements of a Google Cloud startup will vary depending on the application you intend to run.
Step 1. Use the text editor of your choice to create a new script file and save it to 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 go to the virtual machine instances page.

Stage 4. Select the VM instance where 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:
. - Value – The URL of the script file you uploaded to Cloud Storage, starting with “gs://”.

Step 7. Click Save to apply the changes to the VM instance.
Step 8. Start or restart the VM instance. The startup script runs automatically when the instance starts.
There are three ways to determine if your startup script ran successfully:
- Check the instance to make sure it is running correctly.
- Check the script output for any error messages or other notifications that the script did not run successfully.
- Check your instance logs to verify the successful execution of the startup script.
Startup Script Best Practices
When creating startup scripts, follow these tips to avoid common mistakes:
- Specify the interpreter. For example, if you write a python script, you would specify the interpreter as /bin/python.
- Use absolute paths. Use absolute paths for command line arguments to ensure that the command can be found and executed successfully.
- 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 in the startup script.
- Put the startup script in the correct location. The startup script must be in the correct place to run automatically. Typically, the location of the startup script is var/run/google.startup.scripts.