How can I run a command in the background in Node.JS? In Python I have the following code (for Windows) to make an exe run in background. child_process.execFile didn't have a support for this...
si = subprocess.STARTUPINFO()
si.dwFlags |= subprocess.STARTF_USESHOWWINDOW
si.wShowWindow = 4 #constant for SHOWNOACTIVATE
subprocess.Popen(startCommands, startupinfo = si)
Is there a support for STARTUPINFO in Node.JS?
The method you want is spawn. The thing you want to look out for is how you are running the file. You probably want to execute it using the python command rather than running the file as an executable. spawn will run the file in the background. You may be confused what exactly you mean by "in the background". To clarify everything in node can be considered "in the background" if it is not specifically considered a synchronous function. The following will not prevent further execution in node while runnable.py is running. However, the spawnSync command runs synchronously in node which means it will prevent any other code from being executed while that runs.
var spawn = require('child_process').spawn
var runnable = spawn('python', ['./runnable.py']);
//optionally log the output
runnable.stdout.on('data', function (data) {
console.log(data.toString());
})
//code here will continue to run while running.py executes
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With