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?
6 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
- type "screen". this will open up a new screen - kind of similar in look & feel to what "clear" would result in.
- run the process (e.g.: ./init-dev.sh to fire up a ChicagoBoss erlang server)
- type: Ctrl + A, and then Ctrl + D. This will detach your screen session but leave your processes running!
- 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.
- to kill all detached screens, run: screen -ls | grep pts | cut -d. -f1 | awk '{print $1}' | xargs kill

- 421
- 4
- 8
-
this is really cool! By "kill all detached screens" do you mean kill the process that you started, right? You start process, detach process from your SSH session, do something in your session (i.e. close SSH session). Upon coming back, you should see the process, right? Step 6 kills process for good. – Diana Vazquez Romo Aug 30 '22 at 19:26
-
Use `screen -list` to view all screens. Then use `screen -x [screen id]` to view/attach to a screen. – ThomasAFink Feb 28 '23 at 09:54
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.

- 1,092
- 7
- 18
-
1I think the question is on how and why the GCP Cloud compute VM shuts down on closing the ssh browser connection. – 55597 Feb 07 '19 at 19:48
I faced similar issue. I logged into Virtual Machine through google cloud command on my local machine, tried to exit by closing the terminal, It halted the script running in the instance.
Use command exit
to log out of cloud consoles in local machine putty
console (twice).

- 591
- 1
- 5
- 23

- 191
- 5
Make sure you have not enabled "PREEMPT INSTANCE" while creating a VM instance. It will force to close the instance within 24 hours to reduce the costing by a huge difference.

- 151
- 1
- 5