EsotericVoid
EsotericVoid

Reputation: 2576

Run python script on Google Cloud Compute Engine

I know this is an exact copy of this question, but I've been trying different solutions for a while and didn't come up with anything.

I have this simple script that uses PRAW to find posts on Reddit. It takes a while, so I need it to stay alive when I log out of the shell as well.

I tried to set it up as a start-up script, to use nohup in order to run it in the background, but none of this worked. I followed the quickstart and I can get the hello word app to run, but all these examples are for web applications and all I want is start a process on my VM and keep it running when I'm not connected, without using .yaml configuration files and such. Can somebody please point me in the right direction?

Upvotes: 2

Views: 4892

Answers (1)

EsotericVoid
EsotericVoid

Reputation: 2576

Well, at the end using nohup was the answer. I'm new to the GNU environment and I just assumed it didn't work when I first tried. My program was exiting with an error, but I didn't check the nohup.out file so I was unaware of it..

Anyway here is a detailed guide for future reference (Using Debian Stretch):

  1. Make your script an executable

    chmod +x myscript.py
    
  2. Run the nohup command to execute the script in the background. The & option ensures that the process stays alive after exiting. I've added the shebang line to my python script so there's no need to call python here

    nohup /path/to/script/myscript.py &
    
  3. Logout from the shell if you want

    logout
    

Done! Now your script is up and running. You can login back and make sure that your process is still alive by checking the output of this command:

ps -e | grep myscript.py

Upvotes: 3

Related Questions