How to run bash script on Google Cloud VM? - google-cloud-platform

I found this auto shutdown script for VM instances on GCP and tried to add that into the VM's metadata.
Here's a link to that shutdown script.
The config sets it so that after 20 mins the idle VM will shut down, but it's been a few hours and it never shut down. Are there any more steps I have to do after adding the script to the VM metadata?
The metadata script I added:

Startup scripts are executed while the VM starts. If you execute your "shutdown script" at the boot there will be nothing for it to do. Additionally in order for this to work a proper service has to be created and it will be using this script to detect & shutdown the VM in case it's idling.
So - even if the main script ashutdown was executed at boot and there was no idling it did nothing. And since the service wasn't there to run it again your instance will run indefinatelly.
For this to work you need to install everything on the VM in question;
Download all three files to some directory in your vm, for example with curl:
curl -LJO https://raw.githubusercontent.com/GoogleCloudPlatform/ai-platform-samples/master/notebooks/tools/auto-shutdown/ashutdown
curl -LJO https://raw.githubusercontent.com/GoogleCloudPlatform/ai-platform-samples/master/notebooks/tools/auto-shutdown/ashutdown.service
curl -LJO https://raw.githubusercontent.com/GoogleCloudPlatform/ai-platform-samples/master/notebooks/tools/auto-shutdown/install.sh
Make install.sh exacutable: sudo chmod +x install.sh
Run it: sudo ./install.sh.
This should install & run the ashutdown service in your system.
You can check if it's running with service ashutdown status.
These instructions are for Debian system so if you're running CentOS or other flavour of Linux they may differ.

Related

Set up EC2 instance to allow debugging on my local machine using PhpStorm

I have a EC2 instance using nginx and a PHP test web application on it.
Currently every time I want to debug using PhpStorm I have to enter this command in my Ubuntu terminal:
sudo ssh -N -R 9000:localhost:9000 -i "devInstanceNginx.pem" ec2-user#test.co.uk
I am wondering what I can do so that I don't have to do that command.
(Note I will only generally debug from one of 2 devices if that makes a difference)
You can create a "Shell script" run configuration with this script. Configuration can be added via Run > Edit Configurations:

Google Cloud Shell Keep Disconnecting

looking for solution to prevent google cloud shell disconnecting when it found you idle, even it also disconnect when you run some processing and leave system idle.
message shown Connection to Cloud Shell has been lost. Any additional changes will not be saved.
This behavior is by design, as Cloud Shell is intended for interactive use only.
One way to fix it is running a ping command within a TMUX Terminal.
apt install tmux
apt update
tmux
ping google.com
Ctrl+b "
The downside of this is you'll be working halfscreen.

How to keep a server processing running on a Google Cloud VM?

This question seems very basic, but I wasn't able to quickly find an answer at https://cloud.google.com/compute/docs/instances/create-start-instance. I'm running a MicroMDM server on a Google Cloud VM by connecting to is using SSH (from the VM instances page in the Google Cloud Console) and then running the command
> sudo micromdm serve
However, I notice that when I shut down my laptop, the server also stops, which is actually why I wanted to run the server in a VM in the first place.
What would be the recommended way to keep the server running? Should I use systemd or perhaps run the process as a Docker container?
When you run the service from the command line, you "attach" it to your shell process, when you terminate your ssh session, your job gets terminated also.
To make a process run in background, simply append the & at the end of the command, in your case:
sudo micromdm serve &
This way your server is alive even after you quit your session.
I also suggest you to add that line in the instance startup script, if you want that server to always be up, so that you don't have to run the command by hand each time :)
More on Compute Engine startup scripts here.
As the Using MicroMDM with systemd documentation, it suggested to use systemd command to run MicroMDM service on linux.First, on our linux host, we create the micromdm.service file, then we move it to the location ‘/etc/systemd/system/micromdm.service’ . We can start the service. In this way, it will keep the service running, or restart service after the service fails or server restart.

Where do GCE store metadata's startup-script in VM?

After I create a vm with startup script, where can I find startup script in vm ?
Will this startup script store in vm or outsite the vm ?
If I want to edit my startup script, how could it edit it ?
The startup script is taken from the metadata server.
If you restart your instance, after it boots up it will connect to the metadata server and take the script from there and then execute it.
Therefore, you need to change the instance metadata in order to change your startup sript (uses the compute.instances.setMetadata permission).
You can do that straight from the UI, API or CLI tool. More info on all of the above here - Compute Engine Docs - Running Startup Scripts
After you change the startup script for an instance it will execute on the next (re)boot. The article above also provides a command you can use if you wanted to force its execution straight away:
$ sudo google_metadata_script_runner --script-type startup --debug

Running a process with nohup after SSH to a server using Python

I am trying to run a program as a background process in a server (AWS EC2 instance).
I have used boto.manage.cmdshell to obtain an ssh connection to the server.
However, I am having trouble running this command:
"nohup daemon-program param 2>&1 > ./logs/out.log &"
It runs fine if I manually ssh into the machine and run this command.
My console hangs after ssh-ing into the machine and running this command via python script.
If I remove nohup, the program starts and quits when the ssh session ends.
I would like it to run as a bg process even after I quit.
I tried reading about pty and nohup manual, but I seem to have missed something here.
Kindly point me to a (better?) instruction manual or explain why this fails while manual execution succeeds.
TIA!
If anyone is stuck, ran the command inside byobu and it worked.