help icon indicating copy to clipboard operation
help copied to clipboard

killing multiple child processes

Open CoyiCodes opened this issue 3 years ago • 13 comments

Details

Hi I am trying to kill a process but I can not do it. I created a flask server with python and packed it with pyinstaller. When I start my electron app I start the process like this ( and it starts ok):

This is used under app.on('ready'...)

const { execFile, spawn } = require('child_process');
var kill = require('tree-kill');
let child;

function startExecutable() {
    let browseDirectory = process.cwd() + "/api/Tracker.exe"
    child = execFile(browseDirectory)
}

This creates 2 processes:

Task manager view

I am trying to kill the process on app.on('window-all-closed'...)

function stopExecutable() {
    // console.log(child.pid)
    // child.kill('SIGKILL')
    // kill(child.pid, 'SIGTERM');
    kill(child.pid, 'SIGKILL');
}

But this only kills PID 8868 and other one keeps running.

  • I tried using tree-kill, Kill-Process-By-Name packages.
  • I tried using child.kill(), child.kill('SIGKILL').
  • I tried using spawn(taskkill ['/F', '/IM', 'Tracker.exe', '/T'])

But When I run code the code below on command prompt. It kills all of them

taskkill /F /IM Tracker.exe /T

They all kill only one of the processes. How Do I kill all processes with the same name?

Node.js version

v16.13.1

Example code

No response

Operating system

Windows 10

Scope

code

Module and version

Not applicable.

CoyiCodes avatar Dec 14 '22 12:12 CoyiCodes

@CoyiCodes Are you running the code with elevated privileges? - Clerkie

krrishdholakia avatar Dec 26 '22 12:12 krrishdholakia

elevated privileges

I am packing it with electron-builder then I tried both starting it by duble clicking and runing it as admin. Nothing changed unfortunately.

CoyiCodes avatar Dec 26 '22 12:12 CoyiCodes

I also encountered the same problem!I'm so sad

xinzhao111 avatar Jun 09 '23 08:06 xinzhao111

cc @nodejs/child_process PTAL

preveen-stack avatar Aug 25 '23 06:08 preveen-stack

taskkill /F /PID xxxx /T can solve the problem

hoboly avatar Sep 20 '23 07:09 hoboly

This is the behavior of PyInstaller, see https://pyinstaller.readthedocs.io/en/stable/advanced-topics.html#the-bootstrap-process-in-detail.

morningf avatar Mar 12 '24 08:03 morningf

This is the behavior of PyInstaller, see https://pyinstaller.readthedocs.io/en/stable/advanced-topics.html#the-bootstrap-process-in-detail.

You are correct. Do you have any good solutions? At present, I can only kill all associated processes through tree-kill, but I don't think this is the best solution.

lizhongyue248 avatar Apr 28 '24 04:04 lizhongyue248

You are correct. Do you have any good solutions? At present, I can only kill all associated processes through tree-kill, but I don't think this is the best solution.

I'm unable to solve the issue even by killing all the associated processes. I tried the following thing:

var kill = require('tree-kill');
let mainProcessPid
...

app.on("ready", () => {
    createWindow();
    mainProcessPid = mainWindow.webContents.getOSProcessId()
})

...

app.on("window-all-closed", () => {
    if(process.platform !== "darwin") {
        // Retrieve main process Pid
        // Kill main process and all the subprocesses
        kill(mainProcessPid)
        // Quit
        app.quit()
    }
})

fedehann avatar May 07 '24 22:05 fedehann

This is the behavior of PyInstaller, see https://pyinstaller.readthedocs.io/en/stable/advanced-topics.html#the-bootstrap-process-in-detail.

You are correct. Do you have any good solutions? At present, I can only kill all associated processes through tree-kill, but I don't think this is the best solution.

Currently, I'm using the following approach:

  1. When starting a Python process, you can obtain its PID.
  2. Find another process with the same name but different PIDs.
  3. If step 2 finds one, kill this process and automatically terminate it when getting directly obtained by another progress at that time.
  4. If step 2 doesn't find any, then directly kill the process corresponding to the given PID.

morningf avatar May 08 '24 08:05 morningf

Hi! Node.js (AFAIK) doesn't support killing processes by name. If you manage to obtain the PID of the processes to kill, you can do it like that.

Additionally, via the command you provided, taskkill /F /IM Tracker.exe /T, you can kill processes via child_process.

Do you need more help, or can this be closed?

avivkeller avatar Jul 15 '24 22:07 avivkeller

That's what I found best, also suggested by others, fairly simple in use and less error prone

function killPythonServer() {
    const killCmd = `tskill processName`;
    spawn('cmd.exe', ['/c', killCmd]);
}

app.on('quit', (event) => {
    killPythonServer();
});

Cosmicoppai avatar Jul 29 '24 01:07 Cosmicoppai

That's what I found best, also suggested by others, fairly simple in use and less error prone

function killPythonServer() {
    const killCmd = `tskill processName`;
    spawn('cmd.exe', ['/c', killCmd]);
}

app.on('quit', (event) => {
    killPythonServer();
});

But this method only works on Windows...

lizhongyue248 avatar Jul 29 '24 01:07 lizhongyue248

function killPythonServer() {
    const killCmd = `tskill processName`;
    spawn('cmd.exe', ['/c', killCmd]);
}

app.on('quit', (event) => {
    killPythonServer();
});

@lizhongyue248 yeah, I meant we can use the general idea of killing using the process name. Similar action in unix can be achieve via pkill -f processName

cosmicoppai@CosmicOppai:~$ ps -aux | grep 'LiSA'
cosmico+    1353  5.4  0.7 325836 60480 pts/0    Sl+  07:20   0:00 python3 LiSA.py
cosmico+    1359  0.1  0.5 225672 41256 pts/0    Sl+  07:20   0:00 python3 LiSA.py
cosmico+    1364  0.0  0.5 225672 40988 pts/0    Sl+  07:20   0:00 python3 LiSA.py
cosmico+    1370  0.0  0.5 225672 40960 pts/0    Sl+  07:20   0:00 python3 LiSA.py
cosmico+    1377  0.0  0.5 225672 40904 pts/0    Sl+  07:20   0:00 python3 LiSA.py
cosmico+    1748  0.0  0.0   4032  2036 pts/2    S+   07:20   0:00 grep --color=auto LiSA
cosmicoppai@CosmicOppai:~$ pkill -f LiSA
cosmicoppai@CosmicOppai:~$ ps -aux | grep 'LiSA'
cosmico+    1802  0.0  0.0   4032  2020 pts/2    S+   07:20   0:00 grep --color=auto LiSA
cosmicoppai@CosmicOppai:~$

Cosmicoppai avatar Jul 29 '24 01:07 Cosmicoppai

It seems there has been no activity on this issue for a while, and it is being closed in 30 days. If you believe this issue should remain open, please leave a comment. If you need further assistance or have questions, you can also search for similar issues on Stack Overflow. Make sure to look at the README file for the most updated links.

github-actions[bot] avatar Jan 26 '25 01:01 github-actions[bot]

It seems there has been no activity on this issue for a while, and it is being closed. If you believe this issue should remain open, please leave a comment. If you need further assistance or have questions, you can also search for similar issues on Stack Overflow. Make sure to look at the README file for the most updated links.

github-actions[bot] avatar Feb 25 '25 01:02 github-actions[bot]