Christopher
Christopher
DDeno
Created by Christopher on 8/2/2023 in #help
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?
2 replies