D
Deno

help

Run a detached child process in Deno

CChristopher8/2/2023
I'm looking at running a detached child process using Deno. Specifically I want run something like a script or a web server without needing the parent Deno command staying open. In node I can achieve it using spawn doing something like this.
// index.js
const { spawn } = require("child_process");

const childProcess = spawn("run-server", [], {
detached: true,
stdio: "ignore",
});

childProcess.unref();
// index.js
const { spawn } = require("child_process");

const childProcess = spawn("run-server", [], {
detached: true,
stdio: "ignore",
});

childProcess.unref();
When the parent node command terminates, the child process will continue running.
In Deno the equivalent would be something like this
// index.ts
const child_process = new Deno.Command("run-server", {
args: [],
stdio: "ignore"
}).spawn();

child_process.unref();
// index.ts
const child_process = new Deno.Command("run-server", {
args: [],
stdio: "ignore"
}).spawn();

child_process.unref();
In Deno though as soon as unref is called, the parent deno command terminates which then kills the child process. Is there a way using Deno to achieve the equivalent behaviour I can achieve in node?
Mmmastrac8/3/2023
I believe this is the following feature request: https://github.com/denoland/deno/issues/5501
GitHub
Detached processes in deno · Issue #5501 · denoland/deno
Currently node has the capability to run a child process in detached mode (setsid()), which is lacking in deno (?) It seems the Tokio Command doesn't expose any specific functionality for this....

Looking for more? Join the community!