psuspend

Suspend and resume processes on Windows, Mac or Linux

Usage no npm install needed!

<script type="module">
  import psuspend from 'https://cdn.skypack.dev/psuspend';
</script>

README

PSuspend

Pause and resume processes on all hosts, including Windows! The module contains logic to detect both Windows x32 and x64 and switch between the linux-compatible and included windows-compatible method as necessary.
Why not use ntsuspend? It brings process management functionality to the NT Shell through unsupported and discontinued methods, however the methods behind this module are being actively updated and supported as of 06/29/16, and last packaged on 03/23/21.
That method is PsSuspend, a part of PsTools by Mark Russinovich from Windows SysInternals. If you use this module on Windows, you accept the EULA for this. PSuspend will quietly load and cache a version of this executable according to your architecture as necessary.

Usage

Supply the process object or PID, and a boolean (pause/play), default is true

const suspend = require("psuspend");
const { spawn } = require("child_process");
const process = spawn("tree");

// Pause (it's reccomended to use a child process object when possible)
suspend(process);
/* OR, for non-native processes, supply the PID *\
suspend(process.pid);

// Play
suspend(process, false);
/*  OR */
suspend(process.pid, false);

Promise API

As of version 1.1.0, the main "suspend" function will return a promise that resolves when the suspension is complete. It will also have a boolean, which will be true if nothing unexpected happened. You can use this to see how the first call to suspend will take a little longer on windows because it has to do a little bit of loading. Below is a benchmark script using this functionality.

const suspend = require("psuspend");
const { spawn } = require("child_process");
const process = spawn("tree", ["C:"]);

const now = require('nano-time');
const bigInt = require("big-integer");

const bench = (() => {
    var start;
    start = bigInt(now());
    suspend(process).then(() => {
        let dur = bigInt(now()).minus(start).divide(1000000).toString();
        console.log(`Suspending took ${dur}ms`);
    });

    setTimeout(() => {
        start = bigInt(now());
        suspend(process, false).then(() => {
            let dur = bigInt(now()).minus(start).divide(1000000).toString();
            console.log(`Unsuspending took ${dur}ms`);
        });
    }, 1000);
});

bench();
setTimeout(bench, 2000);

And here are the benchmark results (on Windows)

Suspending took 268ms
Unsuspending took 78ms
Suspending took 192ms
Unsuspending took 81ms

IF YOU ARE MAKING A LINUX/MAC ONLY APP, SIMPLY DO THIS

const { spawn, exec } = require("child_process");
const process = spawn("tree");

// Pause
process.kill('SIGSTOP')
/* OR */
exec(`kill -SIGSTOP ${process.pid}`);

// Play
process.kill('SIGCONT')
/* OR */
exec(`kill -SIGCONT ${process.pid}`);