Kill deno process tree

How would you kill a Deno process tree as in, force kill of Deno process and any of it's subprocesses? Deno.killappears to possibly leave subprocesses alive. Ideally using Deno.ChildProcess API, but I highly suspect it's not possible using the runtime API; I would also be happy with just a linux command I could run along with the PID! (I'm not that well versed with linux commands and process stuffs, so far I've been using killall deno which definitely works but would be nice to target only a specific process tree)
DAH
Deno AI Helper41d ago
I wasn't able to find the answer to your question in the official Deno documentation. However, based on your message, I can provide some general advice that might help you with your issue. To kill a Deno process tree, including all of its subprocesses, you would typically need to send a signal to the parent process that also affects all its children. On Linux, you can use the pkill command with the -P flag to target a specific parent PID and its child processes. For example, if you have the PID of the Deno process, you could use:
pkill -P <parent-pid>
pkill -P <parent-pid>
Replace <parent-pid> with the PID of the Deno process you want to kill. This should terminate the process and all of its subprocesses. Please note that this advice is not based on the Deno documentation, and I would advise you to double-check the accuracy of this command and its suitability for your use case. It's also important to use such commands with caution, as they can terminate processes abruptly. For a more Deno-specific solution, you might need to manage the subprocesses manually, keeping track of their PIDs and sending kill signals as necessary, but this would be more complex and is not directly supported by the Deno.ChildProcess API as of my knowledge cutoff date.
R
raunioroo41d ago
good bot pkill -P <parent-pid> does not seem to recursively kill the whole process tree, only killing descendants but not descendants-of-descendants 🤔 Curiously it does seem like there is no "kill whole tree by pid" command built into linux - all the stack overflow answers seem to suggest a recursive bash script to accomplish that. So here's what I came up with to avoid introducing a bash script to my codebase, keeping it in Deno. It's an unfortunately named function, and a hodgepodge of JS, pgrepand Deno.killto recursively kill a whole subprocess tree. Quick testing seem to indicate it works. (I'm using my own wrapper lib for Deno.Command, hence the weird API, but you'll get the idea)
const killChilds = async (pid) => {
const getChildsCmd = new Process();
getChildsCmd.setCommand("pgrep");
getChildsCmd.addArgument("-P");
getChildsCmd.addArgument(pid);
const result = await getChildsCmd.runCapture();
const childs = result.split("\n");
for (let i = 0; i < childs.length; i++) {
const child = childs[i];
if (child) {
await killChilds(child);
Deno.kill(child * 1, "SIGKILL");
}
}
}
const killChilds = async (pid) => {
const getChildsCmd = new Process();
getChildsCmd.setCommand("pgrep");
getChildsCmd.addArgument("-P");
getChildsCmd.addArgument(pid);
const result = await getChildsCmd.runCapture();
const childs = result.split("\n");
for (let i = 0; i < childs.length; i++) {
const child = childs[i];
if (child) {
await killChilds(child);
Deno.kill(child * 1, "SIGKILL");
}
}
}