user8367984
user8367984

Reputation:

How to pause process in node js using spawn

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.

Upvotes: 5

Views: 4848

Answers (4)

Fr3ddyDev
Fr3ddyDev

Reputation: 474

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');

Upvotes: 2

鄭元傑
鄭元傑

Reputation: 1657

You can find out the process PID and use linux command 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 command, should work fine.

Upvotes: 3

K1nzy_The_Idiot
K1nzy_The_Idiot

Reputation: 47

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

Upvotes: -2

notme
notme

Reputation: 444

Send it a signal: read this

But what signal? read this

Upvotes: -1

Related Questions