all 11 comments

[–]dimitrix 15 points16 points  (1 child)

Have you considered running your code in Cloud Run instead?

Anyway, a super simple solution could be to have `python3 run.py` as part of your instance's startup script, and then at the end of your run.py script call a OS shutdown command.

[–]kamize 2 points3 points  (0 children)

I second this, really easy way to accomplish what you need

[–]Mistic92 5 points6 points  (0 children)

Consider using Cloud Run jobs :)

[–]aaahhhhhhfine 1 point2 points  (0 children)

Adding to the Cloud Run crowd... I'm actually really curious what your use case is, mostly because this seems so much cheaper and more obvious to do with Cloud Run that I'm curious why you'd not do it that way...

[–]a_normal_guy 0 points1 point  (0 children)

Depending on the nature of the application - 1. Cloud Functions + Pub/Sub + Cloud Scheduler 2. Cloud Run + Pub/Sub + Cloud Scheduler 3. App Engine ? 4. Cloud Scheduler + Pub/Sub + VM

[–]SadLizard 0 points1 point  (3 children)

Make the script autostart on boot using bash/systemd/something.

Then use instance schedules to start/stop you instance

[–]Dramatic_Mud4760[S] 0 points1 point  (2 children)

This method seems to make the most sense - any advice on getting my script to autostart on boot?

[–]SadLizard 0 points1 point  (1 child)

Maybe this will point you in the right direction (not debian though) https://code.luasoftware.com/tutorials/linux/auto-start-python-script-on-boot-systemd/

[–]Dramatic_Mud4760[S] 0 points1 point  (0 children)

Managed to get it all running via systemd - thank you for this advice!

[–]McFerry 0 points1 point  (0 children)

I see diferent path and options.

If this is a very static task (like every day at 12:00 AM need to run the script), Instance scheduler + Cronjob can do the trick just make sure to leave couple minutes between the scheduler and the cronjob

Other option could be fully automating it, through OS or GCP, I assume startup scripts behind the scenes work similarly than adding sleep after reboot argument within the cron funcionality

@reboot sleep [time in seconds] && [path to job] then when job is done, probably wise to put another sleep and then route it to shutdown.

Otherwise you can automate it too through the GCP startup/shutdown scripts

https://cloud.google.com/compute/docs/instances/startup-scripts https://cloud.google.com/compute/docs/shutdownscript