上QQ阅读APP看书,第一时间看更新
Startup scripts
We can run our own startup scripts from VM instances to perform automatic actions such as updating the system, and sending notifications and so on.
A startup script is specified using metadata keys through the metadata server and can be applied using the console or gcloud command line:
- Once you have set the default region and zone for your instance, you can provide a startup script using the gcloud command line while creating an instance. The script is provided with a—metadata flag:
gcloud compute instances create example-instance --tags http-server --metadata startup-script='#! /bin/bash # Installs apache and a custom homepage # Go to root directory sudo su - # For automatic Updates apt-get update # Install apache apt-get install -y apache2 # Edit index.html file cat <<EOF > /var/www/html/index.html <html><body><h1>Hello World</h1> <p>This page was created from a simple start up script!</p> </body></html> EOF'
- For an already running instance, startup scripts can be added using the add-metadata flag as follows. The path can be a URL as well, which means that the script can also be located from a Cloud Storage bucket object:
gcloud compute instances add-metadata example-instance --metadata-from-file startup-script=path/to/file