How to keep a server processing running on a Google Cloud VM? - google-cloud-platform

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.

Related

How to make SSH in Google cloud keep running after closing the browser whilst running a .exe-file?

I’m running a gameserver, more specifically a Terraria server on GCP. When I run ./TerrariaServer.bin.x86_64 (exe-file)
I have to input some settings for the server to run. Hence, nohup and & have not worked for me to keep the server running after I close the SSH-terminal. Any suggestions on how to achieve this?
Do not use hohup for SSH sessions. The program will be terminated when the SSH session ends.
Instead use programs like screen or tmux.
Techniques to Keep SSH Session Running After Disconnection

How to run bash script on Google Cloud VM?

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.

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

chef client run in aws bootup script not getting registered in chef server

We have used below command to run on booting aws machine. It ran chef client and software is installed. But i am not able to find server in chef console when i searched with vm ip address.
chef-client -r 'role[test]'
Can some one explain chef-client -r option works
The -r flag runs chef-client as normal but replaces the run-list coming from the Chef Server with the one specified on the command line. The two likely reasons for your confusion are either that chef-client is hitting an error which prevents the node data from being saved back to the server, or the way you are searching isn't correct. If you are looking for ipaddress:1.2.3.4, make sure you are using the private IP address (I think).

Understanding fabric

I've just stumbled upon Fabric and the documentation doesn't really make it obvious how it works.
My educated guess is that you need to install it on both client-side and server-side. The Python code is stored on the client side and transferred through Fabric's wire-protocol when the command is run. The server accepts connections using the OpenSSH SSH daemon through the ~/.ssh/authorized_keys file for the current user (or a special user, or specified in the host name to the fab command).
Is any of this correct? If not, how does it work?
From the docs:
Fabric is a Python (2.5 or higher) library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks.
It provides a basic suite of operations for executing local or remote shell commands (normally or via sudo) and uploading/downloading files, as well as auxiliary functionality such as prompting the running user for input, or aborting execution.
So it's just like ssh'ing into a box and running the commands you've put into run()/sudo().
There is no transfer of code, so you only need to have ssh running on the remote machine and have some sort of shell (bash is assumed by default).
If you want remote access to a python interpreter you're more looking at something like execnet.
If you want more information on how execution on the remote machine(s) work look to this section of the docs.
Most what you are saying is correct, except that the "fabfile.py" file only has to be stored on your client. An SSH server like OpenSSH needs to be installed on your server and an SSH client needs to be installed on your client.
Fabric then logs into one or more servers in turn and executes the shell commands defined in "fabfile.py". If you are located in the same dir as "fabfile.py" you can go "fab --list" to see a list of available commands and then "fab [COMMAND_NAME]" to execute a command.
The user on the server does not need to be added to "~/.ssh/authorized_keys" but if it is you don't have to type the password every time you want to execute a command.