Shutdown script not executing on Google Cloud VM

780 Views Asked by At

I have the following:

#! /usr/bin/python3.7
f=open("python_out.txt",'w',encoding='utf-8')
f.write("OK1")
import socket
import telegram
f.write("OK2")
BOT_TOKEN = "telegram BOT_TOKEN"
CHAT_ID = "chat_id"

bot = telegram.Bot(token=BOT_TOKEN)
host_name = socket.gethostname()
content = 'Machine name: %s is shutting down!' % host_name
bot.send_message(chat_id=CHAT_ID, text=content)
f.write("OK3")

I have checked my environment, I can make this script work through python3 script.py when it is in the instance,It can send notifications and output python_out.txt.

I set this script in shutdown-script Screenshot

But when I manually clicked the "stop" button, it did not work as expected. startup-script too. button Screenshot

I have read many posts:

Of course it also includes official documents:

I want to try setting powerbtn.sh,but i can't find /etc/acpi/ in GCP Ubuntu 16.04 LTS

etc ls Screenshot

I can't find any more schedule, any ideas?

1

There are 1 best solutions below

2
On

When you use startup script and shutdown script, the current user that execute is the root user, and the default directory /root/. This directory isn't writable, that's why nothing happens with your code.

Simply write files in writable directory and that's all.

Don't forget that the files that you create are written by the root user and all user can't read and/or write on file wrote by root. Use chmod or chown to change this.