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.
1 Answer
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');
2 Comments
liam923
Could you explain to me what the 'child.on' function is doing?
Valentin Lorentz
It declares a callback. On a
close event, it will call the associated function, which prints a message to the console.