Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass CPU priority for process #1253

Open
gendalf opened this issue May 11, 2015 · 19 comments
Open

Pass CPU priority for process #1253

gendalf opened this issue May 11, 2015 · 19 comments

Comments

@gendalf
Copy link

gendalf commented May 11, 2015

How I can register process low or high CPU priority?

@jshkurti
Copy link
Contributor

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

@ageorgios
Copy link

ageorgios commented May 25, 2016

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
Copy link
Collaborator

soyuka commented May 25, 2016

@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
Copy link

dbalaji commented Aug 17, 2016

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
Copy link

avimar commented Feb 1, 2018

Bump. Any plans for this?

@wallet77
Copy link
Contributor

wallet77 commented Feb 1, 2018

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
Copy link
Collaborator

soyuka commented Feb 1, 2018

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
Copy link

avimar commented Feb 1, 2018

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

@deanrather
Copy link
Contributor

see this too -- #3912

@Darren80
Copy link

Darren80 commented Mar 28, 2019

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
Copy link

stale bot commented May 24, 2020

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 stale bot added the stale label May 24, 2020
@avimar
Copy link

avimar commented May 24, 2020

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

Please make this natively supported.

@stale stale bot removed the stale label May 24, 2020
@stale
Copy link

stale bot commented Jun 23, 2020

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 stale bot added the stale label Jun 23, 2020
@gianpaj
Copy link

gianpaj commented Jul 1, 2020

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

@stale stale bot removed the stale label Jul 1, 2020
@soyuka
Copy link
Collaborator

soyuka commented Jul 17, 2020

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

@avimar
Copy link

avimar commented Jul 17, 2020

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
Copy link

stale bot commented Aug 16, 2020

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 stale bot added the stale label Aug 16, 2020
@avimar
Copy link

avimar commented Aug 16, 2020

Bump to remove stale...

@stale stale bot removed the stale label Aug 16, 2020
@zackees
Copy link

zackees commented May 7, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests