Unitech / pm2

Node.js Production Process Manager with a built-in Load Balancer.
https://pm2.keymetrics.io/docs/usage/quick-start/
Other
41.31k stars 2.61k forks source link

Pass CPU priority for process #1253

Open gendalf opened 9 years ago

gendalf commented 9 years ago

How I can register process low or high CPU priority?

jshkurti commented 9 years ago

I don't think there is a way to do this at the moment.

ageorgios commented 8 years ago

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]

soyuka commented 8 years ago

@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
dbalaji commented 8 years ago

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);
        });
avimar commented 6 years ago

Bump. Any plans for this?

wallet77 commented 6 years ago

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 ?

soyuka commented 6 years ago

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).

avimar commented 6 years ago

One of the main things I do is to directly start node processes -- there's no external script to set the nice.

deanrather commented 5 years ago

see this too -- https://github.com/Unitech/pm2/pull/3912

Darren80 commented 5 years ago

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");
}
stale[bot] commented 4 years ago

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.

avimar commented 4 years ago

Seems we can use process.pid and os.setPriority(pid,$newPriority).

Please make this natively supported.

stale[bot] commented 4 years ago

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.

gianpaj commented 4 years ago

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

soyuka commented 4 years ago

Then again, if you have a modern nodejs version use os.setPriority() (docs) or a cross-platform alternative http://npmjs.com/package/renice

avimar commented 4 years ago

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...

stale[bot] commented 4 years ago

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.

avimar commented 4 years ago

Bump to remove stale...

zackees commented 4 months ago

Came here looking looking for this exact same thing. Seems critical.