Create and run shutdown scripts that execute commands right before a virtual machine (VM) instance is stopped or restarted. This is useful if you rely on automated scripts to start up and shut down instances, allowing instances time to clean up or perform tasks, such as exporting logs, or syncing with other systems.
Shutdown scripts are especially useful for VMs in a managed instance group with an autoscaler. If the autoscaler shuts down a VM in the group, the shutdown script runs before the VM stops and the shutdown script performs any actions that you define. The script runs during the limited shutdown period before the VM stops. For example, your shutdown script might copy processed data to Cloud Storage or back up any logs.
There are some limitations to note when using shutdown scripts:
- Shutdown scripts have a limited amount of time to finish running before the instance stops:
- On-demand instances: 90 seconds after you stop or delete an instance
- Preemptible instances: 30 seconds after instance preemption begins
- Compute Engine executes shutdown scripts only on a best-effort basis. In rare cases, Compute Engine cannot guarantee that the shutdown script will complete.
- On Windows, the Local Group Policy is used to launch the shutdown script.
- The installation package configures the Local Group Policy
Computer Configuration/Windows Setting/Scripts (Startup/Shutdown)
setting to launch the script upon system shutdown.
- The installation package configures the Local Group Policy