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.
Asked
Active
Viewed 2,461 times
1 Answers
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
-
Could you explain to me what the 'child.on' function is doing? – liam923 Aug 03 '17 at 22:14
-
It declares a callback. **On** a `close` event, it will call the associated function, which prints a message to the console. – Valentin Lorentz Aug 04 '17 at 10:38