If I run a script through ssh to GCP, will it be interrupted if my local machine goes to sleep?

Viewed 677

I have a script that should take about 8 hours or so to run on a GCP VM instance with 8CPUs. I am somewhat new to cloud computing and am a bit nervous to set up ssh, run my python file and walk away.

If I ssh into a GCP VM instance, start a python script and walk away, will it continue to run even if my computer goes to sleep? My intuition is no, as this seems to be the whole point of cloud computing, but I want to be sure. Thanks.

2 Answers

You are right, if your computer goes to sleep and your ssh session closes, your script will stop.

To prevent this, you can use & and then disown to push your script to the background and remove the process from the shell's job control, but it still leaves it connected to the terminal.

For example:

  1. I ran my script

./script.sh &

  1. We will obtain the JobID, because it will start running in background:

[1] 7812

  1. With this JobID I can run the disown command to remove the process from the shell's job control:

disown 7812

At this point, you can close your ssh session and the script will continue working.

Alternatively, you also can use Screen. When screen is called, it creates a single window with a shell in it (or the specified command) and then gets out of your way so that you can use the program as you normally would.

Screen will keep running even if your session gets disconnected. When you reconnect to the server you can reconnect to the screen session and everything will be as if nothing happened, this is a good option if you want to see the results of your script in the standard output.

You can consult the official documentation on the following link. Also, you can use the following guide to know how to use screen.

Additionally, you can see the following post, where the community explains the difference between nohup, disown and & .

I hope you find this information useful.

Yes, if you close your SSH session your script stops. I usually use nohup command for this

nohup <command + param> &

I assume that you are under linux

Related