client.command runs a command on a running instance. Use runCommand() to stream output as it is produced, or runCommandSync() to wait for the command to finish and read its combined output.
The examples on this page use a Compute client:
targetContainerName. Set it to run the command in that container, which must already be running on the instance. See Create and manage compute instances to start containers at creation time. Omit it to run the command directly in the guest, as macOS instances do.
command.runCommand()
Run a command and stream its output. Stdout and stderr arrive as individual responses. The final response carries an exitStatus with the command’s exit code.
Example
API reference
Arguments and options
The name of the container to run the command in. When omitted, the command runs directly in the guest.
Cancellation, timeout, and header options. See Shared call options.
Return value
Returns an async iterable ofRunCommandResponse.
Set only on the last message in the stream. Carries the command’s
exitCode.NotFound. The same applies when targetContainerName is set and no such container exists.
command.runCommandSync()
Run a command and wait for it to complete. Stdout and stderr are buffered and returned in a single response.
Example
API reference
Arguments and options
runCommandSync() takes the same RunCommandRequest as runCommand().
Cancellation, timeout, and header options. See Shared call options.
Return value
Returns aRunCommandSyncResponse.
If the instance does not exist, the call fails with NotFound. The same applies when targetContainerName is set and no such container exists.
The whole command runs within a single RPC. Raise
timeoutMs in the call options for long-running commands, or use runCommand() and consume output as it arrives.Related documentation
Create and manage instances
Create instances and start the containers commands run in.
Instance logs
Stream and fetch logs from an instance.