Open gendalf opened 9 years ago
I don't think there is a way to do this at the moment.
You can do this with linux command "nice" ex.
nice 19 node app.js
start app.js in lowest priority [high -20..19 low]
but have not figured out a way to do this with pm2
or while node is running do renice -n 19 -p [pid]
@ageorgios same way without pm2:
pm2 start app.js
renice -n 19 -p $(cat $(pm2 desc server | grep pid | awk '{print $5}')) //bit ugly would be cleaner with pm2 jlist + jq
I have done this in the nodejs it self, using the below script as I required a my nodejs script to be run as low priority process always. I did not want to write additional bash script to handle every restart of my low priority task.
var spawn = require('child_process').spawn;
var priority = 10;
var proc= spawn("renice", [priority, process.pid]);
proc.on('exit', function (code) {
if (code !== 0){
console.log("Process "+cmd+" exec failed with code - " +code);
}
});
proc.stdout.on('data', function(data){
console.log('stdout: ' + data);
});
proc.stderr.on('data', function(data){
console.log('stderr: '+ data);
});
Bump. Any plans for this?
I really think it's definitely not a good thing to let PM2 or a process decide CPU priority. This kind of decision should belong to OS or an external script.
@Unitech @soyuka @vmarchaud : do you have an opinion ?
I had built a package named "renice" because I though it'd be okay to add a "renice" command in pm2: https://github.com/soyuka/renice/blob/master/index.js
(May 25, 2016, few days after this issue opened haha) In fact this is just a convenient multi-os wrapper (haven't tested it for now cause I wasn't sure it'd be useful).
I don't see why we could not add this command to the list. To me this subject is the same as "adding startup methods for every supported OS embed in pm2" (ie pm2 startup).
One of the main things I do is to directly start node processes -- there's no external script to set the nice
.
see this too -- https://github.com/Unitech/pm2/pull/3912
I came up with a simple way to do it with nodejs.
const shell = require('shelljs');
const process = require('process');
if (process.pid && shell.which('renice')) {
shell.exec(`renice -n 10 -p ${process.pid}`);
console.log("Process re-niced");
}
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Seems we can use process.pid
and os.setPriority(pid,$newPriority)
.
Please make this natively supported.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
I would also need this as well. Use case: a pretender process (Chrome headless) is using a lot of CPU. Decreasing its priority sounds like a solution
Then again, if you have a modern nodejs version use os.setPriority()
(docs) or a cross-platform alternative http://npmjs.com/package/renice
Even though we could do it within the node file, it would be great to make it configurable from one - to configure the cluster, worker count, and priority all together...
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Bump to remove stale...
Came here looking looking for this exact same thing. Seems critical.
How I can register process low or high CPU priority?