Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run command in background in Windows from Node.JS

Tags:

python

node.js

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?

like image 835
m0nhawk Avatar asked Jul 03 '26 10:07

m0nhawk


1 Answers

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
like image 199
Cody Gustafson Avatar answered Jul 04 '26 23:07

Cody Gustafson