27

I need to spawn a child process from node.js, whilst using ulimit to keep it from using to much memory.

Following the docs, it wasn't hard to get the basic spawn working: child = spawn("coffee", ["app.coffee"]).

However, doing what I do below just makes the spawn die silently.

child = spawn("ulimit", ["-m 65536;", "coffee app.coffee"])

If I would run ulimit -m 65536; coffee app.coffee - it works as intented.

What am I doing wrong here?

Industrial
  • 41,400
  • 69
  • 194
  • 289
  • Possible duplicate of [Execute a command line binary with Node.js](https://stackoverflow.com/questions/20643470/execute-a-command-line-binary-with-node-js) – iSkore Jul 27 '17 at 12:00

1 Answers1

31

Those are two different commands. Don't club them if you are using spawn. Use separate child processes.

 child1 = spawn('ulimit', ['-m', '65536']);
 child2 = spawn('coffee', ['app.coffee']);

If you are not interested in output stream(if you want just buffered output) you can use exec.

var exec = require('child_process').exec,
child;

child = exec('ulimit -m 65536; coffee app.coffee',
  function (error, stdout, stderr) {
    console.log('stdout: ' + stdout);
  }
});
vinayr
  • 11,026
  • 3
  • 46
  • 42
  • 1
    Hi vinayr. In your first example, does the `ulimit` really affect the following spawn/child then? – Industrial Oct 08 '12 at 12:56
  • Ideally it should. Did you test? – vinayr Oct 08 '12 at 13:02
  • Well, I did create a child script that creates a huge array with random data - eating up 220mb of ram according to `process.memoryUsage().rss` regardless of ulimit being used as in your example – Industrial Oct 08 '12 at 13:08
  • Maybe you want to try 'ulimit -v' instead? – Soichi Hayashi Jan 19 '14 at 22:03
  • @vinayr: Is there anyway, where I could pass multiple parameters and create only one child process? This is what I am trying to do. var lighttpdObj = cp.spawn(lighttpdPath,['-f lighttpd.conf','-m lib']); – Vasanth Sriram Apr 01 '14 at 14:11
  • 2
    @VasanthSriram sure `cp.spawn(lighttpdPath,['-f','lighttpd.conf','-m','lib']);` – vinayr Apr 01 '14 at 23:05