Command Execution
Morph Cloud provides robust capabilities for executing commands on your instances, whether you need to run a single command or a sequence of commands.
Execute Commands on Instance
- Python
- TypeScript
- CLI
from morphcloud.api import MorphCloudClient
client = MorphCloudClient()
instance_id = "morphvm_abc123" # Replace with a valid instance ID
instance = client.instances.get(instance_id=instance_id)
result = instance.exec(command="uname -a")
print(f"Exit Code: {result.exit_code}")
print(f"Stdout:\n{result.stdout}")
print(f"Stderr:\n{result.stderr}")
import { MorphCloudClient } from 'morphcloud';
const client = new MorphCloudClient();
async function executeCommand() {
const instanceId = "morphvm_abc123"; // Replace with a valid instance ID
const instance = await client.instances.get({ instanceId: instanceId });
const result = await instance.exec("uname -a");
console.log(`Exit Code: ${result.exitCode}`);
console.log(`Stdout:\n${result.stdout}`);
console.log(`Stderr:\n${result.stderr}`);
}
executeCommand();
# Execute a command on an instance
morphcloud instance exec morphvm_abc123 uname -a
# For commands with spaces or multiple arguments
morphcloud instance exec morphvm_abc123 bash -c "ls -la /tmp && echo 'Hello World'"
Execute Multiple Commands
You can also run a sequence of commands in a single execution.
- Python
- TypeScript
- CLI
from morphcloud.api import MorphCloudClient
client = MorphCloudClient()
instance_id = "morphvm_abc123" # Replace with a valid instance ID
instance = client.instances.get(instance_id=instance_id)
commands = """
apt-get update
apt-get install -y nginx
systemctl enable nginx
systemctl start nginx
"""
result = instance.exec(command=commands)
print(f"Exit Code: {result.exit_code}")
print(f"Stdout:\n{result.stdout}")
print(f"Stderr:\n{result.stderr}")
import { MorphCloudClient } from 'morphcloud';
const client = new MorphCloudClient();
async function executeMultipleCommands() {
const instanceId = "morphvm_abc123"; // Replace with a valid instance ID
const instance = await client.instances.get({ instanceId: instanceId });
const commands = `
apt-get update
apt-get install -y nginx
systemctl enable nginx
systemctl start nginx
`;
const result = await instance.exec(commands);
console.log(`Exit Code: ${result.exitCode}`);
console.log(`Stdout:\n${result.stdout}`);
console.log(`Stderr:\n${result.stderr}`);
}
executeMultipleCommands();
# Execute multiple commands
morphcloud instance exec morphvm_abc123 bash -c "apt-get update && apt-get install -y nginx && systemctl enable nginx && systemctl start nginx"
Handling Execution Results
Command execution results contain stdout, stderr, and an exit code that you can use to determine success or failure:
- Python
- TypeScript
from morphcloud.api import MorphCloudClient
client = MorphCloudClient()
instance_id = "morphvm_abc123" # Replace with a valid instance ID
instance = client.instances.get(instance_id=instance_id)
result = instance.exec(command="ls /nonexistent")
if result.exit_code == 0:
print("Command succeeded")
print(f"Output: {result.stdout}")
else:
print("Command failed")
print(f"Error: {result.stderr}")
print(f"Exit code: {result.exit_code}")
import { MorphCloudClient } from 'morphcloud';
const client = new MorphCloudClient();
async function handleExecutionResults() {
const instanceId = "morphvm_abc123"; // Replace with a valid instance ID
const instance = await client.instances.get({ instanceId: instanceId });
const result = await instance.exec("ls /nonexistent");
if (result.exitCode === 0) {
console.log("Command succeeded");
console.log(`Output: ${result.stdout}`);
} else {
console.log("Command failed");
console.log(`Error: ${result.stderr}`);
console.log(`Exit code: ${result.exitCode}`);
}
}
handleExecutionResults();