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
But when I manually clicked the "stop" button, it did not work as expected. startup-script
too.
I have read many posts:
- Shutdown script not executing on a Google Cloud VM
- Reliably executing shutdown scripts in Google Compute Engine
- Pro Tip: Use Shutdown Script Detect Preemption on GCP
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
I can't find any more schedule, any ideas?
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 byroot
. Usechmod
orchown
to change this.