Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node-webkit execute external command?

How to execute system command in node-webkit (or node.js) external process parallel of current script.

I'm trying to use child_process. After interruption of my script subprocess is exit. However i need a simple way execute bash command without output or with output but without program stop when my script will be interrupted.

I need a correct simple way.

Thanks all.

like image 374
xercool Avatar asked Oct 29 '13 19:10

xercool


1 Answers

Use detached option in spawn/execute arguments:

If the detached option is set, the child process will be made the leader of a new process group. This makes it possible for the child to continue running after the parent exits.

By default, the parent will wait for the detached child to exit. To prevent the parent from waiting for a given child, use the child.unref() method, and the parent's event loop will not include the child in its reference count.

Example of detaching a long-running process and redirecting its output to a file:

 var fs = require('fs'),
     spawn = require('child_process').spawn,
     out = fs.openSync('./out.log', 'a'),
     err = fs.openSync('./out.log', 'a');

 var child = spawn('prg', [], {
   detached: true,
   stdio: [ 'ignore', out, err ]
 });

 child.unref();
like image 159
Andrey Sidorov Avatar answered Sep 24 '22 10:09

Andrey Sidorov