1

How can I start and stop a python script from a NodeJS server? I have seen the module "python-shell", but it doesn't provide a way to kill the script after running it.

liam923
  • 991
  • 1
  • 10
  • 19

1 Answers1

2

Use child_process.

Example from the doc:

const { spawn } = require('child_process');
const child = spawn('python3', ['script.py']);

child.on('close', (code, signal) => {
  console.log(
    `child process terminated due to receipt of signal ${signal}`);
});

// Send SIGTERM to process
child.kill('SIGTERM');
Valentin Lorentz
  • 9,556
  • 6
  • 47
  • 69