Sowohl asynchrone als auch synchrone Hilfsfunktionen für Unterprozesse funktionieren
in gewöhnlichen Skripten. Ein laufender Kindprozess hält das Skript bis zu seinem
Ereignis close am Leben:
import { spawn, execFileSync } from "node:child_process";
const child = spawn("echo", ["hello"]);
child.stdout.on("data", (data) => console.log(String(data).trim())); // hello
child.on("close", (code) => console.log("exit", code)); // exit 0
const output = execFileSync(process.execPath, [
"--snippet",
'process.stdout.write("hi")',
]);
console.log(output.toString()); // hiVerfügbare Exporte sind spawn, spawnSync, exec, execSync, execFile,
execFileSync, fork und ChildProcess. spawnSync gibt das vertraute Objekt mit
pid, status, signal, stdout, stderr und output zurück. fork() startet
ein weiteres Elide-Skript mit einem IPC-Kanal: child.send() und process.send()
tauschen Nachrichten aus, die als Ereignisse message zugestellt werden.
Das Erstellen von Prozessen wird bei aktiven Sandbox-Flags über
--allow-run[=CMDS] und --deny-run <CMDS> gesteuert.
Reference
Status: Full · Not measured · since 1.1.0
Exports
| Export | Kind | Status | Notes |
|---|---|---|---|
exec(), execSync() | function | Full | Executes a command in a shell |
execFile(), execFileSync() | function | Full | Executes a file without a shell |
spawn(), spawnSync() | function | Full | Spawns a new process using the given command |
fork() | function | Full | Spawns a new Node.js process using the given module path |