Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set nodejs windows service as high priority

I have a node server and script that runs as a windows service using https://github.com/jon-hall/pm2-windows-service and pm2.

The server is time sensitive and I am seeing situations where the hardware is hitting 100% CPU usage and the script timing goes off, sometimes several minutes off.

I want to mitigate this by setting the service to high priority. How can I do that?

I have found some solutions involving VBScript https://serverfault.com/questions/179161/change-windows-service-priority, but is there a pm2 or node way of setting the service or elevating itself after it runs?

Also, please comment if I posted this on wrong site. It's a bit grey since it's both a server problem and a programming problem.

like image 730
Trevor Avatar asked Mar 08 '26 02:03

Trevor


2 Answers

I was able to do it using this code and the end of my load up process.

const {exec} = require('child_process');
exec('wmic process where "ProcessId=' + process.pid + '" CALL setpriority 256');
like image 120
Trevor Avatar answered Mar 09 '26 20:03

Trevor


You can use os.setPriority:

import os from 'node:os';
os.setPriority(process.pid, os.constants.priority.PRIORITY_HIGHEST);
like image 40
Richie Bendall Avatar answered Mar 09 '26 21:03

Richie Bendall



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!