How can I kill a child process without the child process 'exit' event triggering?

Kurt

I'm spawning a child process:

child = spawn("childprocess", [], {
    detached: true
}

And I'm watching the 'exit' event with:

child.on('exit', (code, signal) => {
    // Do stuff
}

When my app exits, I'm killing the child by using taskkill (because this is running on Windows):

exec(`taskkill /PID ${child.pid} /F /T`, (error, stdout, stderr) => {
    // Do stuff
}

The problem is, that when the process is killed the exit event fires (understandably), but I don't want it to. Is there some way to remove the event listener? Or kill the process without triggering events? I've tried child.removeListener('exit') but that didn't work (maybe because it's an anonymous function?).

Felix Kling

I've tried child.removeListener('exit') but that didn't work (maybe because it's an anonymous function?).

Whether the function has a name or not doesn't matter. You simply need to pass the same function object to removeListener (or off):

var handler = (code, signal) => {};
child.on('exit', handler);
child.off('exit', handler);

Or call child.removeAllListeners('exit');.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to kill a child process started by process in java?

分類Dev

Kill child process created with fork

分類Dev

How to monitor a child process without signals?

分類Dev

How to make sure that a child process of a supervisor job dies when I kill supervisor

分類Dev

when parent process is killed by 'kill -9', how to terminate child processes

分類Dev

Supervisor.restart_child / 2またはProcess.exit(pid、:kill)?

分類Dev

python subprocess.Popen kill process with child process

分類Dev

Python execute function at child process exit

分類Dev

In Windows, how can I trace in C which files a child process reads and writes?

分類Dev

Can a python Multiprocessing queue be passed to the child process?

分類Dev

Forward aliases to child process

分類Dev

How to read errors from a child process?

分類Dev

How to return value from a child process in a function?

分類Dev

How can I detect a tap event in a child class of UIView?

分類Dev

How to print output of an interactive child process from parent process?

分類Dev

How to get process id from child process in node js

分類Dev

Uncleanly exiting from C child process without valgrind complaining?

分類Dev

Visual Studio made a process I can't kill

分類Dev

In nodejs on windows, how do I use the `uid` option with child_process.spawn?

分類Dev

Error: Can't resolve 'child_process' Angular-cli

分類Dev

Share SSL socket with child process

分類Dev

Node js child process windows

分類Dev

Handling child process shutdown gracefully

分類Dev

How to kill a process on a port on ubuntu

分類Dev

How to kill process of another user?

分類Dev

How to kill a daemon process in linux?

分類Dev

How to initialize a child process with passed in functions in Node.js

分類Dev

How to create shared memory after fork or in child process?

分類Dev

How to send terminate signal to the child process on exception, when using subprocess

Related 関連記事

  1. 1

    How to kill a child process started by process in java?

  2. 2

    Kill child process created with fork

  3. 3

    How to monitor a child process without signals?

  4. 4

    How to make sure that a child process of a supervisor job dies when I kill supervisor

  5. 5

    when parent process is killed by 'kill -9', how to terminate child processes

  6. 6

    Supervisor.restart_child / 2またはProcess.exit(pid、:kill)?

  7. 7

    python subprocess.Popen kill process with child process

  8. 8

    Python execute function at child process exit

  9. 9

    In Windows, how can I trace in C which files a child process reads and writes?

  10. 10

    Can a python Multiprocessing queue be passed to the child process?

  11. 11

    Forward aliases to child process

  12. 12

    How to read errors from a child process?

  13. 13

    How to return value from a child process in a function?

  14. 14

    How can I detect a tap event in a child class of UIView?

  15. 15

    How to print output of an interactive child process from parent process?

  16. 16

    How to get process id from child process in node js

  17. 17

    Uncleanly exiting from C child process without valgrind complaining?

  18. 18

    Visual Studio made a process I can't kill

  19. 19

    In nodejs on windows, how do I use the `uid` option with child_process.spawn?

  20. 20

    Error: Can't resolve 'child_process' Angular-cli

  21. 21

    Share SSL socket with child process

  22. 22

    Node js child process windows

  23. 23

    Handling child process shutdown gracefully

  24. 24

    How to kill a process on a port on ubuntu

  25. 25

    How to kill process of another user?

  26. 26

    How to kill a daemon process in linux?

  27. 27

    How to initialize a child process with passed in functions in Node.js

  28. 28

    How to create shared memory after fork or in child process?

  29. 29

    How to send terminate signal to the child process on exception, when using subprocess

ホットタグ

アーカイブ