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 user8367984user83679844 Answers
Reset to default 3You 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