3

I've been trying to find the best way to start a python script on startup of my cloud compute instance. So far, I haven't gotten it to run. The script does run when manually executed on the instance. I did make the file executable.

This is what I have tried so far:

1) Add script directly to metadata with key "startup-script". Script starts with:

#! /usr/bin/python3

Followed by the script contents.

Result: Won't run, doesn't show up in log.

2) Try to execute local script from metadata with key "startup-script":

#! /usr/bin/bash"
/home/dir/scripts/script.py.

Result: Won't run, doesn't show up in log.

3) Point to file located in storage bucket with "startup-script-url".

gs://project.appspot.com/folder/script.py

Result: Won't run, but shows "Found startup script" in log.

I hope anyone has some insights.

1 Answer 1

3

This worked for me:

#! /bin/bash
cat <<EOF > /var/myScriptStackOverflow.py
with open("/var/python_was_here_stack_overflow.txt", "w") as file:
    file.write("Looks that the script is executed")
EOF
python3 /var/myScriptStackOverflow.py

The script above is explicit in respect to the paths, but this also works:

#! /usr/bin/python3
with open("/var/python_was_here_stack_overflow.txt", "w") as file:
    file.write("Looks that the script is executed this way as well...")

  • Edit the instance, paste the script above in the Custom metadata with the key startup-script:

enter image description here

  • Reset the instance
  • ssh inside the instance to check the results:
ls -la /var | grep -i py
-rw-r--r--  1 root root   119 Aug  3 17:33 myScriptStackOverflow.py
-rw-r--r--  1 root root    33 Aug  3 17:33 python_was_here_stack_overflow.txt
cat /var/myScriptStackOverflow.py 
with open("/var/python_was_here_stack_overflow.txt", "w") as file:
    file.write("Looks that the script is executed")
cat /var/python_was_here_stack_overflow.txt 
Looks that the script is executed
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for taking a look! This was a good approach to troubleshoot the problem. Turns out that the script was running, even though it didn't show in the console. The two files were created, however, for some reason the python file still did not execute. Any ideas why that would be?
I don't understand your comment. If both files were created, it means that the python script was executed: the execution of the python script creates the /var/python_was_here_stack_overflow.txt file
This is the file that is created by the python script. I used the second option that I suggested: cat python_was_here_stack_overflow.txt Looks that the script is executed this way as well...
Ah I see. I have no experience with bash so I just now fully understood whats happening. I'll give it another try tomorrow.
You answer helped to make sure that everything was running as it should. As for my issue, it just ended up being a path problem.

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.