简体   繁体   English

是否有可能通过新的HTTP请求杀死Node.js中的子进程

[英]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 有没有办法通过新请求ex杀死先前请求中的child_process.exec()

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 ! 请求来->检查exec()是否正在运行->如果正在运行,则将其杀死->运行新的exec()-> return!

is that possible to kill this running process by a new HTTP request? 是否有可能通过新的HTTP请求杀死正在运行的进程?

is there a way to set an app status for Node.js and set a flag , then check the flag to stop the process? 有没有一种方法可以为Node.js设置应用程序状态并设置标志 ,然后检查标志以停止该过程?

In my opinion, you can kill a child process by two ways. 我认为,可以通过两种方式杀死子进程。

Solution 1: Use child process object with kill() function like 解决方案1:将子进程对象与kill()函数一起使用

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 解决方案2:从子进程中获取进程PID,然后在应用程序process任何位置通过nodejs process其终止(在HTTP请求中执行终止动作),例如

// 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: 在下一个HTTP请求到来时,您可以通过以下方法将其杀死:

// 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? 供您参考,如果您想检查子进程是否在运行,请尝试在此处检查我的问题Node.js如何独立检查PID所运行的进程?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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