execute python script in google cloud VM

Asked

Viewed 424 times

0

I created a vm in google cloud to run a script (bot) but every time I close my terminal the script is stopped for no apparent reason.

the code itself has only one while True with a conditional in the middle to create a delay;

while True:
    time.sleep(30)
    # do stuff

To run the script I use the hohup through the cloud console provided by google;

nohup python3 script.py &

This command generates a file nohup.out which stores the generated logs, even after executing the command no data is inserted in the nohup.outbut the script is started successfully.

How do I run a script on a google cloud VM forever?

  • I am unable to check now to write an answer, but what is happening is expected. Your terminal window is "owner" of the Python process that runs your script. If you use Bash, it does not respect the nohup, when you close the window the Processor belonging to the terminal are terminated. You need to let the script 'own yourself'. Search 'disown'.

  • found some things @Rocchi and it seems nohup already owns a disownembedded and there is no need to write nohup python3 script.py & disown :/

  • I reread the question and realized that I missed an important detail. The problem is only that the log is empty? It’s probably all working normally... The log is only filled in after the buffer has been filled in first. There is a way to decrease the buffer size for the log to be filled more often, if applicable. You can test if everything is working normally by writing a file inside the while. Puts a file = open('seila.txt','w'); file.write(contador); file.close() inside the loop, and see if the contents of the file changes as the counter increments.

  • there are actually 2 doubts the main is how to keep a script running even after leaving the ssh session, the second is to understand why the nohup.out is not receiving any information. nohup as root and 10 minutes after leaving the sessionVolteo to check with ps aux | grep "script.py" and no task was being performed

  • I just tested the scenario using the online google cloud terminal. I put the script to run, I checked that it was running, I closed the terminal with exit, I waited a while and when I came back the script was still running normally. Even nohup.out was written. I have no more ideas. If I need to, I test other things here to compare.

  • Voce used print how to log? to exit I simply closed the window I did not type exit. as Voce did to check that it was working?

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.