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?

1

1 Answer 1

32

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);
  }
});
Sign up to request clarification or add additional context in comments.

6 Comments

Hi vinayr. In your first example, does the ulimit really affect the following spawn/child then?
Ideally it should. Did you test?
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
Maybe you want to try 'ulimit -v' instead?
@VasanthSriram sure cp.spawn(lighttpdPath,['-f','lighttpd.conf','-m','lib']);
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.