简体   繁体   中英

Is that possible to kill the child process in Node.js by new HTTP request

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?

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM