0

Is there a way to kill the child_process.exec() from previous request by a new request ex

I have part of code like so :

 var proc = require('child_process').exec('ffmpeg -i a.mp4 -o b.avi');

scenario like this

request come -> check the exec() is running or not -> if running kill it->run new exec() -> return !

is that possible to kill this running process by a new HTTP request?

is there a way to set an app status for Node.js and set a flag, then check the flag to stop the process?

koner
  • 45
  • 6
  • It's all in the documentation https://nodejs.org/api/child_process.html just use `proc.kill();` – Molda Aug 13 '19 at 08:58

1 Answers1

0

In my opinion, you can kill a child process by two ways.

Solution 1: Use child process object with kill() function like

proc.kill();

Solution 2: Get process PID from child process and kill it by nodejs process anywhere in your application (execute kill action in HTTP request) like

// Get process's pid and save it somewhere or global variable
let pid = proc.pid;

At next HTTP request comes, you can kill it by:

// Use node process to kill it anywhere by pid
process.kill(pid);

For your information, if you want to check a child process is running or not, try to check my question here Nodejs how to check independently a process is running by PID?

ThanhPhanLe
  • 1,315
  • 3
  • 14
  • 25
  • but the request is already return ,and the exec() sill running , i want to kill it went the next request come – koner Aug 13 '19 at 09:12
  • You can save its PID of child process and kill it at next request comes as solution 2, I also updated my answer to help you understand it – ThanhPhanLe Aug 13 '19 at 09:14