Christopher
Christopher12mo ago

Run a detached child process in Deno

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?
1 Reply
mmastrac
mmastrac12mo ago
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....