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.

1 Answer 1

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');
Sign up to request clarification or add additional context in comments.

2 Comments

Could you explain to me what the 'child.on' function is doing?
It declares a callback. On a close event, it will call the associated function, which prints a message to the console.

Your Answer

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.