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?
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.
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.
You need to press the letter q to "quit" from that. You can press "h" for help.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With