pm2 icon indicating copy to clipboard operation
pm2 copied to clipboard

Pass CPU priority for process

Open gendalf opened this issue 9 years ago • 19 comments

How I can register process low or high CPU priority?

gendalf avatar May 11 '15 21:05 gendalf

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

jshkurti avatar May 17 '15 19:05 jshkurti

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 avatar May 25 '16 09:05 ageorgios

@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

soyuka avatar May 25 '16 10:05 soyuka

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);
        });

dbalaji avatar Aug 17 '16 11:08 dbalaji

Bump. Any plans for this?

avimar avatar Feb 01 '18 16:02 avimar

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 ?

wallet77 avatar Feb 01 '18 17:02 wallet77

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

soyuka avatar Feb 01 '18 17:02 soyuka

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

avimar avatar Feb 01 '18 17:02 avimar

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

deanrather avatar Oct 12 '18 06:10 deanrather

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");
}

Darren80 avatar Mar 28 '19 15:03 Darren80

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.

stale[bot] avatar May 24 '20 04:05 stale[bot]

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

Please make this natively supported.

avimar avatar May 24 '20 07:05 avimar

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.

stale[bot] avatar Jun 23 '20 08:06 stale[bot]

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

gianpaj avatar Jul 01 '20 19:07 gianpaj

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

soyuka avatar Jul 17 '20 07:07 soyuka

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

avimar avatar Jul 17 '20 08:07 avimar

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.

stale[bot] avatar Aug 16 '20 09:08 stale[bot]

Bump to remove stale...

avimar avatar Aug 16 '20 13:08 avimar

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

zackees avatar May 07 '24 01:05 zackees