OperatingSystemMXBean.getSystemCpuLoad doesn't work when run as service

792 Views Asked by At

We have a section of code that needs to monitor the CPU load. We get the CPU load with the following line of code:

((com.sun.management.OperatingSystemMXBean) ManagementFactory
  .getOperatingSystemMXBean()).getSystemCpuLoad();

During Testing, this works fine.

When deployed to a 2012 Windows Server, running as a Service, getSystemCpuLoad always returns -1.

If we stop the service and run it as an application (still on the 2012 Window Server machine), it works again.

I have looked at classpath differences, and environmental variable differences and cannot find any discrepancies.

Does anyone have an idea of why getSystemCpuLoad works when running in an application but not as a service?

1

There are 1 best solutions below

1
On BEST ANSWER

I finally found what was causing the issue. In case anyone else is running into this:

I had to add the user that the service is running as to the "Performance Log Users" group. Once it was added here, i was able to get the system CPU load.