0

Node does not seem to have a system call to kill a process (given I know its PID), or does it?

If there is no built-in function for this, what is a valid cross-platform approach?

2
  • 1
    Nodejs has a process.kill() method that you can use to kill a process by pid. Commented Dec 17, 2021 at 15:55
  • @TGrif That's good stuff! The terminate library also uses process.kill under the hood, but it also deals with some processes needing more than one kill signal, and makes sure that all children are killed as well. Commented Dec 17, 2021 at 16:02

1 Answer 1

0

The terminate library has seemingly been built for this purpose. It is cross-platform. (I tried it on Windows and MAC.)

Here is a working sample. It also illustrates how to find PID by PORT.

Example:

const { promisify } = require('util'); /** * NOTE: as of 2021, terminate is still callback-based. */ const terminate = promisify(require('terminate')); await terminate(myPid); console.log('Process terminated.');

Note that if the process does not exist (or in case of any other problems), it will throw, so you might want to try/catch correspondingly.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.