Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

killing multiple child processes #4050

Open
CoyiCodes opened this issue Dec 14, 2022 · 9 comments
Open

killing multiple child processes #4050

CoyiCodes opened this issue Dec 14, 2022 · 9 comments

Comments

@CoyiCodes
Copy link

CoyiCodes commented Dec 14, 2022

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.

@krrishdholakia
Copy link

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

@CoyiCodes
Copy link
Author

CoyiCodes commented Dec 26, 2022

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.

@xinzhao111
Copy link

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

@preveen-stack
Copy link
Contributor

cc @nodejs/child_process PTAL

@hoboly
Copy link

hoboly commented Sep 20, 2023

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

@morningf
Copy link

@lizhongyue248
Copy link

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.

@fedehann
Copy link

fedehann commented May 7, 2024

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()
    }
})

@morningf
Copy link

morningf commented May 8, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants