简体   繁体   English

如何使用 spawn 在节点 js 中暂停进程

[英]How to pause process in node js using spawn

Is there any way, how to pause process in node js?有什么办法,如何在节点js中暂停进程?

          const { spawn } = require('child_process');

          const process = spawn("run.cmd");

          process.stdout.on('data', (data) => {
            console.log(`stdout: ${data}`);
          });

          process.stderr.on('data', (data) => {
            console.log(`stderr: ${data}`);
          });

          process.on('close', (code) => {

          });

I want call somethink like this: process.pause() , process.continue() .我想打电话这样的想法: process.pause()process.continue()

Or some system call using cmd?或者一些使用cmd的系统调用?

I am using windows.我正在使用窗户。

Thank you for any help.谢谢你的帮助。

You can find out the process PID and use linux command to pause/resume process by PID.您可以找出进程 PID 并使用 linux 命令通过 PID 暂停/恢复进程。
You can change your code to process = spawn(cmd & echo $! > txt) .您可以将代码更改为process = spawn(cmd & echo $! > txt)
It would save your cmd process PID to txt, and then you can use fs to read the PID.它将您的cmd进程PID保存到txt,然后您可以使用fs读取PID。

Now you have PID, run exec() to pause process kill -STOP <PID> or resume stopped cmd kill -CONT <PID> .现在你有了 PID,运行exec()来暂停进程kill -STOP <PID>或恢复停止的 cmd kill -CONT <PID>
These two are unix command, should work fine.这两个是unix命令,应该可以正常工作。

I specifically made a library to pause/resume processes from Node.js on Windows, ntsuspend .我专门制作了一个库来暂停/恢复 Windows 上的 Node.js 进程, ntsuspend

const { spawn } = require('child_process');
const ntsuspend = require('ntsuspend');
const process = spawn("run.cmd");
ntsuspend.suspend(process.pid);
ntsuspend.resume(process.pid);

If you're using Linux or MacOS如果您使用的是 Linux 或 MacOS

const { spawn } = require('child_process');
const ntsuspend = require('ntsuspend');
const process = spawn("run.cmd");
process.kill('SIGSTOP');
process.kill('SIGCONT');

Just use the readline package, you can basically use the functions easily使用readline包,基本可以轻松使用功能

To pause the process暂停进程

rl.pause()

To resume the process恢复进程

rl.resume()

If you want more information you can check out these two places如果您想了解更多信息,可以查看这两个地方

Resume - node.js docs简历 - node.js 文档

Pause - node.js docs暂停 - node.js 文档

Send it a signal: read this给它一个信号: 阅读这个

But what signal?但什么信号? read this读这个

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

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