Mike Frank
Mike Frank

Reputation: 389

How can I determine from within an instance shutdown script, that it is being deleted?

I have a shutdown script that runs on a GCE Windows instance. I want to apply different logic if the instance is being deleted, rather than just shut down or restarted. I was hoping to find a property on the instance meta-data to do this, but I haven't seen anything so far. I have also tried checking the instance resource API, but don't see anything to help me there either.

How can I determine, from within an instance shutdown script, that it is being deleted?

Upvotes: 0

Views: 77

Answers (2)

As others have said, I don’t know if there is a way of doing this from the shutdown script in general, but you can get this information for specific cases.

More specifically, your VM’s maintenance-event field in its metadata will be populated up to sixty seconds in advance of a live migration event: https://cloud.google.com/compute/docs/metadata/getting-live-migration-notice#maintenanceevents

Of course, this assumes your VM has live migration enabled, and obviously does not apply for shutdowns other than those directly triggered by a live migration.

This does also mean that you have to query the VM’s metadata for this key at least once a minute, so this is definitely not a perfect solution, unfortunately.

Upvotes: 0

minet
minet

Reputation: 162

You cannot really directly determine from within a shutdown script that the instance is being deleted. However, using gcloud compute instance-groups managed list-instances will give you an output of the current actions on instances with the column "ACTION" and there are statuses like RESTARTING, STOPPING, or DELETING.

Upvotes: 0

Related Questions