![Google Cloud Platform for Architects](https://wfqqreader-1252317822.image.myqcloud.com/cover/763/36699763/b_36699763.jpg)
上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.
![](https://epubservercos.yuewen.com/C15174/19470392301559306/epubprivate/OEBPS/Images/Chapter_48.jpg?sign=1739669063-GeE7v7Kr8PpXW7g9nUDwfg687Wlyx896-0-9eb6ec7c7ea9276d35885d46e9a68164)
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