Run the process (using Runtime.exec / ProcessBuilder.start) with low priority

I need to start a system process with heavy use of a low priority processor so that it does not slow down my server. How to do it on Linux?

This is similar to this question: Run a low priority Java process using Runtime.exec / ProcessBuilder.start? except Linux instead of Windows. This is normal if the process priority changes after the process starts (if there is a slight delay).

+4
source share
1 answer

Run the command using /usr/bin/nice . For instance:

 $ /usr/bin/nice -n 10 somecommand arg1 arg2 

somecommand arg1 arg2 will be executed with a value of +10. (On Unix / Linux, a higher value results in a lower priority for the scheduler. The nice range is usually from -19 to +19.)

Please note that this solution is platform specific. It will only work on Linux and Unix systems ...


FOLLOW UP

ProcessBuilder should be created like any normal command; i.e.

  new ProcessBuilder("nice", "-n", "10", "somecommand", "arg1", "arg2"); 

There is no black magic about when / how to split commands / arguments. The command syntax (for example, nice ) determines what its arguments should be, and determines how they should be indicated on the shell command line and how they should be provided when using ProcessBuilder (or the built-in exec* system calls, this is important).

I don't think there should be problems with waitFor() , etc., because (AFAIK) the /usr/bin/nice command uses exec (not fork / exec ) to run the provided command. Try it ...

+8
source

Source: https://habr.com/ru/post/1393613/


All Articles