Run a Shell Command
Direct exec — safest
Section titled “Direct exec — safest”Install the required plugin:
tynd add processimport { process } from "@tynd/plugin-process/client";
const { stdout, stderr, code } = await process.exec("git", { args: ["status", "--short"], cwd: "/path/to/repo", timeoutMs: 5000,});
if (code !== 0) throw new Error(stderr);console.log(stdout);Arguments are passed as an array — no shell interpolation. Safe against injection.
Shell exec — when you need pipes
Section titled “Shell exec — when you need pipes”const { stdout } = await process.execShell("ls -la | grep tynd | wc -l");Interpolates through cmd.exe /c (Windows) or sh -c (elsewhere). Pipes, globs, shell builtins work.
Never pass user input directly — that’s a shell injection. Quote aggressively or switch back to process.exec with array arguments.
Cancellation / timeout
Section titled “Cancellation / timeout”timeoutMs (default 60 s) is the cleanest option:
try { const res = await process.exec("slow-tool", { timeoutMs: 10_000 });} catch (err) { // "process.exec: timed out after 10000ms" — the process was killed}For explicit cancel mid-run, use process.spawn instead — it returns a handle with streamed stdout / stderr, exit() and kill():
const child = await process.spawn("slow-tool", { args: ["--watch"] });cancelButton.onclick = () => child.kill();const { code } = await child.exit();Environment
Section titled “Environment”await process.exec("node", { args: ["build.js"], env: { NODE_ENV: "production", PATH: "/custom/path:/usr/bin", },});env is merged with the current environment. Pass a single key to add to PATH / override a var; other vars remain.
Related: process API · terminal API · sidecar API.