Can I set ulimit from node.js?

心已入冬 提交于 2020-07-19 03:30:42

问题


I'd like to restrict child processes from writing too much data or taking up too much cpu time (infinite loop). In C, I'd call setrlimit(2) to do that. Is there something like this in node.js?


回答1:


As far as I know, there is no node.js extension that provides setrlimit() functionality, but you can work around the limitation with a small shell workaround hack:

/bin/sh -c "ulimit -t 100; exec /usr/bin/node /my/node/program.js"

Looks like core node.js will never have setrlimit() now that it supports Windows and is no longer a POSIX-only framework: https://github.com/joyent/node/pull/2143

Update: I converted the rejected Node patch into a separate posix extension module which is now available in NPM and Github.




回答2:


I think this newly-released module is just what you're looking for: https://github.com/melor/node-posix




回答3:


I don't think there is any built-in method. However, you can occasionally check the memory usage of a process using process.memoryUsage() and kill the process manually.

Alternatively, if you know how long it is expected to run, you can specify a timeout as described here.



来源:https://stackoverflow.com/questions/8141057/can-i-set-ulimit-from-node-js

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!