Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

google cloud instance terminate after close browser

I have a bash script. I would like to run it continuously on google cloud server. I connected to my VM via SSH in browser but after I've closed my browser, script was stopped. I tried to use Cloud Shell but if I restart my laptop, script launches from start. It doesn't work continuously! Is it possible to launch my script in google cloud, shut down laptop and be sure what my script works?

like image 768
Sergey Balakin Avatar asked Jan 12 '18 07:01

Sergey Balakin


People also ask

How do I close a Google Cloud instance?

To stop a VM, use the Google Cloud console, the gcloud CLI, or the Compute Engine API. In the console, go to the VM instances page. Select one or more VMs that you want to stop. Click Stop.

What is difference between stop and suspend in GCP?

Suspending an instance differs from stopping an instance in the following ways: Suspended instances preserve the guest OS memory, device state, and application state. Google charges for the storage necessary to save instance memory. You can only suspend an instance for up to 60 days.

How do I get out of GCP console?

You need to press the letter q to "quit" from that. You can press "h" for help.

Do you pay for stopped instances GCP?

A stopped instance does not incur charges, but all of the resources that are attached to the instance will still be charged. For example, you are charged for persistent disks and external IP addresses according to the price sheet, even if an instance is stopped.


2 Answers

The solution: GNU screen. This awesome little tool let's you run a process after you've ssh'ed into your remote server, and then detach from it - leaving it running like it would run in the foreground (not stopped in the background).

So after we've ssh'ed into our GCE VM, we will need to: 1. install GNU screen: apt-get update apt-get upgrade apt-get install screen

  1. type "screen". this will open up a new screen - kind of similar in look & feel to what "clear" would result in.
  2. run the process (e.g.: ./init-dev.sh to fire up a ChicagoBoss erlang server)
  3. type: Ctrl + A, and then Ctrl + D. This will detach your screen session but leave your processes running!
  4. feel free to close the SSH terminal. whenever you feel like it, ssh back into your GCE VM, and type screen -r to resume your previously detached session.
  5. to kill all detached screens, run: screen -ls | grep pts | cut -d. -f1 | awk '{print $1}' | xargs kill
like image 100
chandan singh Avatar answered Oct 17 '22 04:10

chandan singh


You have the following options:
1. Task schedules - which involves cron jobs. Check this sample. Via this answer;
2. Using startup scripts.

I performed the following test and it worked for me:
I created an instance in GCE, SSH-d into it and created the following script, myscript.bash:

#!/bin/bash          
          sleep 15s
          echo Hello World > result.txt

and then, ran
$ bash myscript.bash
and immediately closed the browser window holding the SSH session.
I then waited for at least 15 seconds, re-engaged in an SSH connection with the VM in question and ran $ ls and voila:
myscript.bash result.txt

So the script ran even after closing the browser holding the SSH session.
Still, technically, I believe your solution lies with 1. or 2.

like image 36
Tudormi Avatar answered Oct 17 '22 03:10

Tudormi