How to keep python script running when I close SSH connection to VM instance

2,892 views
Skip to first unread message

Aaron Markstaller

unread,
Apr 27, 2021, 10:09:12 AM4/27/21
to gce-discussion
Hello,

I have a VM instance with a python file I've loaded on to it that keeps running and updating me with emails.  I open an SSH connection and have to install python3.8 and then run the script through python3.8.  It runs while I have the SSH connection open, but it stops when I close the connection window.  Then I open it again and can start the function again.

How do I keep it running that script when I close a connection to it?

Thanks

Ted

unread,
Apr 27, 2021, 11:54:41 AM4/27/21
to gce-discussion
I assume your VM instance is running Linux.   If you want to start a command from an ssh connection, and you want it to continue after you logout, you can use the "nohup" (no hangup) command.  For more information: https://www.maketecheasier.com/nohup-and-uses

The more advanced way to do things is to simply provide a startup script when you launch the VM.   The startup script can install python3.8 before it runs the command, and this provides more automation.  For more information, see: https://cloud.google.com/compute/docs/startupscript

An even more advanced way of doing things is to create an image which has your Linux system configured as you like it, with whatever packages you want installed, your programs installed, etc., and then set it up so that when the VM starts, it automatically runs the command.   If this is something you are going to be doing a lot, it may be worth the effort to set up something like this.  For more information: https://cloud.google.com/compute/docs/images/create-delete-deprecate-private-images

If you want to see an example of how this can be automated, there are scripts which do all of this in the xfstests-bld github project.   This is probably more complicated than your use case, but if you look at the scripts, there are a lot of great examples of how the whole process of creating VM images, and launching VM images, etc, can be completely automated.    The whole point of this is so I can run a command like "gce-xfstests smoke", and this will automatically upload a freshly built Linux kernel from my source tree, and use that to test the file system code automatically, and then e-mail me back the results.  Documentation of the entire project, and a slide deck presentation about it can be found here:
A quickie tour of some of the more interesting scripts in this system:
  1. This script creates the image: https://github.com/tytso/xfstests-bld/blob/master/kvm-xfstests/test-appliance/gce-create-image
  2. It works by uploading the following script, which is run as the startup script on a freshly created "build VM" which customizes the system for the test appliance (installing packages and various custom scripts), and then shuts down the VM, so that the gce-create-image script can create a GCE image which is a "freeze dried" file systems test appliance: https://github.com/tytso/xfstests-bld/blob/master/kvm-xfstests/test-appliance/gce-xfstests-bld.sh
  3. One of the files installed by gce-xfstests-bld.sh is this startup script which is run via systemd when a VM is created using the test appliance image: https://github.com/tytso/xfstests-bld/blob/master/kvm-xfstests/test-appliance/files/root/kvm-xfstests.boot
  4. Test appliance VM's are launched using this script: https://github.com/tytso/xfstests-bld/blob/master/kvm-xfstests/gce-xfstests
Perhaps this can give you some ideas!

Cheers,

-- Ted

Wilfred L. (Cloud Platform Support)

unread,
Apr 27, 2021, 5:39:09 PM4/27/21
to gce-discussion
Found this link: https://unix.stackexchange.com/questions/362115/how-to-keep-a-python-script-running-when-i-close-putty

Basically use the nohup command like @theodore.tso said.

Aaron Markstaller

unread,
Apr 28, 2021, 2:30:54 AM4/28/21
to gce-discussion
Thanks guys that's perfect!
I'll have to look into making an image too.  That sounds useful.

Aaron Markstaller

unread,
Apr 28, 2021, 6:57:32 AM4/28/21
to gce-discussion
Actually, one more thing,
Is there a way to connect the stdin and stdout back to the shell or window command line with nohup?  I sometimes need to change some parameters and look and look at updates, so I have it monitor for inputs.
Or with tmux?  Does tmux occupy a core for each session, or slow things down checking for other sessions?

Thanks

Shamma Nikhat (Cloud Platform Support)

unread,
Apr 28, 2021, 1:46:44 PM4/28/21
to gce-discussion
Hello,

You can see this StackExchange thread for similar issue.
Note that, if you have more Linux related questions ServerFault is the right place to post your questions as this group is for Compute Engine inquiries.
Reply all
Reply to author
Forward
0 new messages