2

I would like to have gcp turn on my instance in the morning and run a python script all day without me using ssh. I have added the startup-script metadata to the instance I have also added a cronjob to the Linux side but none of it works so please could someone help me. p.s I'm new to Linux and GCP

[my shell script]

#!/bin/bash
cd '/home/issaca/main/'
nohup python3 main.py &
2
  • WHy a VM? What about a server less product? Commented Jul 5, 2021 at 7:09
  • it receives forex prices and calculates indicators. I'm new to the cloud so I chose the most common option of a VM. please could you explain if serverless is better in this scenario Commented Jul 5, 2021 at 10:31

2 Answers 2

3

You need to:

  • have a script stored on the vm

sudo cat /root/main.py

#!/usr/bin/env python3
from time import sleep
while True:
    sleep(5)
  • metadata startup-script to run your script on startup:
#!/bin/bash
/root/main.py

Then if you can check whether your script is running with

sudo ps aux | grep [m]ain

or

pgrep -a -u root python3

if your script is not running, check the logs with sudo journalctl -u google-startup-scripts.service or in the /var/log/[messages,syslog,daemon].log files.

Sign up to request clarification or add additional context in comments.

5 Comments

with the startup-script will it run without ssh terminal or do I have to use something like nohup
does your script need ssh terminal for some reason? if not it should be fine. example above works without any nohups
no, I just need it to run all day. but suppose I open ssh to make some changes or to check up on it I still need it to work in the background without closing even when I quit ssh
Let me know if it doesn't work for you, we will think of something else
yes, it works I just had to also use nohup and & and also had to move it out of root into \main\main.py as there were some directory errors in python but thanks for your help.
1

This feature did not use to exist. It seems it just came out. Please have a look at this doc on how to schedule to start your instance. It is implemented within the compute engine UI.

Previously, one had to go through Cloud Scheduler.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.