Executing terminal command on compute engine via API

754 views
Skip to first unread message

Carl Dietz

unread,
Jun 4, 2021, 8:29:18 AM6/4/21
to Google Cloud Developers

I would like to be able to execute terminal commands on a GCP compute engine instance using the GCP API. Is this possible?

Ideally I would like to do the following: (1) start a compute engine instance, (2) run an executable on that instance, (3) wait for the executable to complete, and then (4) shut the instance down. I know how to do 1, 3, and 4, but 2 is missing. I can do that by SSH'ing into the instance and running ./path_to_executable but would like to do so programmatically.

I have tried setting the machine's startup script as the path to that executable but that does not seem to work. Is there any other way to accomplish this?

Alexis (Google Cloud Support)

unread,
Jun 4, 2021, 12:18:18 PM6/4/21
to Google Cloud Developers
Hi,

If you were using APIs, you wouldn't need scripts because you'd be doing HTTP calls. I assume you require CLI and programmatic access instead. Your scripts should have two parts. First part acts on the infrastructure to start/stop instances and the second part to SSH in the instances and do its work. Maybe you can even use the gcloud SDK for the second part as well without having to SSH depending on what you are doing. Regardless, it sounds like this script will need to be a on a separate machine, or it could use Google's Cloud Shell.



To be clear, the gcloud SDK is pre-installed on Cloud Shell and project's configs get loaded automatically into Cloud Shell. However, Cloud Shell may not be as powerful as another machine to run scripts and the Cloud Shell gets recycled after a period of inactivity: https://cloud.google.com/shell/docs/limitations#usage_limits


Reply all
Reply to author
Forward
0 new messages