Shutdown script call
Script termination is performed when the instance is scheduled to restart or terminate. There are many ways to restart or terminate an instance, but only a few actions trigger the shutdown of the script. The shutdown script will execute when:
The instance is deleted using instances (). delete request. This includes any tools or scripts that use the API, such as the Google Cloud Platform console and gcloud. The instance is closed through the console or the instance.stop () method. The instance restarts or exits through a request to the guest operating system, such as stopping sudo or rebooting sudo.
Note. If your disconnect script requires a network connection, we recommend that you disconnect your instance using this method, due to a known problem with losing a network connection. The problem primarily affects instances that were closed outside the guest operating system. The shutdown script will not be executed if the reset instance is used by the instances (). Reset
Shutdown script runtime
When a shutdown script is called, it has a limited period of time to run when the request is executed to complete or restart the instance, before the instance is actually completed. During this period, the Compute Engine will try to start shutting down the script, but if the script takes longer than this period of time, the instance will automatically end and all running tasks will be killed. If you complete or restart the instance by performing a request to the guest OS (for example, by running sudo shutdown), the restriction will not apply.
In general, we recommend shutting down the script within this period of time so that the operating system can complete its shutdown, including flushing buffers to disk.
For more information about this time limit, see Shutdown Time.