Skip to content

Instantly share code, notes, and snippets.

@akirattii
Last active May 2, 2023 01:29
Show Gist options
  • Save akirattii/0be958634b64e87e12eb2c32890ea0f9 to your computer and use it in GitHub Desktop.
Save akirattii/0be958634b64e87e12eb2c32890ea0f9 to your computer and use it in GitHub Desktop.
NodeJS: How to run a process (also a shell script etc) on background separated with the parent node process.
const spawn = require('child_process').spawn;
// a command you want to execute.
const command = "node cli/hoge.js --aaa --bbb=123";
const parts = command.split(" ");
const cmd = parts[0];
const args = parts.splice(1);
// a background process is running!
// it is not stopped even if parent node process is killed.
spawn(cmd, args, {
stdio: 'ignore', // piping all stdio to /dev/null
detached: true,
env: process.env,
}).unref();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment