تعمل مساعدات العمليات الفرعية غير المتزامنة والمتزامنة كلتاهما في السكربتات العادية.
ويُبقي الابن الجاري السكربت حيًّا حتى حدث close الخاص به:
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()); // hiالصادرات المتاحة هي spawn وspawnSync وexec وexecSync وexecFile
وexecFileSync وfork وChildProcess. يعيد spawnSync الكائن
المألوف الذي يتضمن pid وstatus وsignal وstdout وstderr وoutput.
ويبدأ fork() سكربت Elide آخر مع قناة IPC: يتبادل child.send() و
process.send() الرسائل، التي تُسلَّم كأحداث message.
يتحكم في إنشاء العمليات --allow-run[=CMDS] و
--deny-run <CMDS> عندما تكون خيارات العزل نشطة.
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 |