Last active
May 2, 2023 01:29
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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