最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to pause process in node js using spawn - Stack Overflow

programmeradmin1浏览0评论

Is there any way, how to pause process in node 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().

Or some system call using cmd?

I am using windows.

Thank you for any help.

Is there any way, how to pause process in node 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().

Or some system call using cmd?

I am using windows.

Thank you for any help.

Share Improve this question asked Jul 26, 2017 at 6:32 user8367984user8367984
Add a ment  | 

4 Answers 4

Reset to default 3

You can find out the process PID and use linux mand to pause/resume process by PID.
You can change your code to process = spawn(cmd & echo $! > txt).
It would save your cmd process PID to txt, and then you can use fs to read the PID.

Now you have PID, run exec() to pause process kill -STOP <PID> or resume stopped cmd kill -CONT <PID>.
These two are unix mand, should work fine.

I specifically made a library to pause/resume processes from Node.js on Windows, 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

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

Send it a signal: read this

But what signal? read this

Just use the readline package, you can basically use the functions easily

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

Pause - node.js docs

发布评论

评论列表(0)

  1. 暂无评论