Terminal
TL;DR
The terminal service lets you execute shell commands on remote machines, stream real-time output, and manage interactive sessions. Supports configurable working directories, environment variables, timeouts, and terminal resizing. Use execute() for one-shot commands, stream() for real-time output, or createSession() for fully interactive shells with input/output and signal handling.
Execute commands, stream output, and manage interactive sessions on remote machines.
How do I execute a command?
import { CMDOPClient } from '@cmdop/node';
// Connect to the cloud relay and select the target machine
const client = await CMDOPClient.remote({ apiKey: 'cmdop_xxx' });
await client.terminal.setMachine('my-server');
// Execute a one-shot command and capture output + exit code
const { output, exitCode } = await client.terminal.execute('ls -la /var/log');
console.log(output.toString());
console.log('Exit code:', exitCode);With Options
// Execute with a custom working directory, environment, and timeout
const { output, exitCode } = await client.terminal.execute('npm install', {
cwd: '/app', // Run inside the /app directory
env: { NODE_ENV: 'production' }, // Set environment variables
timeout: 60000, // Fail after 60 seconds
});How do I stream command output in real time?
// Create a streaming command executor
const stream = client.terminal.stream();
// Attach a callback for each chunk of stdout/stderr data
stream.onOutput((data) => {
process.stdout.write(data.toString());
});
// Attach a callback for when the command exits
stream.onExit((code) => {
console.log('Exited with code:', code);
});
// Start streaming a long-running command
await stream.execute('tail -f /var/log/app.log');How do I create an interactive session?
// Open an interactive bash session with custom terminal dimensions
const session = await client.terminal.createSession({
shell: '/bin/bash',
cols: 120,
rows: 40,
});
// Handle output from the session
session.onOutput((data) => process.stdout.write(data));
// Send commands as if typing in a terminal (include newline to submit)
await session.sendInput('ls -la\n');
await session.sendInput('cd /app\n');How do I resize the terminal?
// Dynamically resize the terminal dimensions
await session.resize({ cols: 200, rows: 50 });How do I send signals to a session?
// Send SIGINT (equivalent to Ctrl+C) to interrupt a running command
await session.signal('SIGINT');
// Send SIGTERM to gracefully terminate the process
await session.signal('SIGTERM');How do I close a session?
// Close the interactive session and release resources
await session.close();How do I switch between machines?
// Set the target machine for subsequent terminal commands
await client.terminal.setMachine('prod-server');
// Switch to a different machine mid-session
await client.terminal.setMachine('staging-server');What parameters does execute() accept?
execute(command, options?)
| Parameter | Type | Description |
|---|---|---|
command | string | Shell command to execute |
options.cwd | string | Working directory |
options.env | Record<string, string> | Environment variables |
options.timeout | number | Timeout in milliseconds |
createSession(options?)
| Parameter | Type | Description |
|---|---|---|
options.shell | string | Shell to use (default: /bin/bash) |
options.cols | number | Terminal columns (default: 80) |
options.rows | number | Terminal rows (default: 24) |
Last updated on