How to change niceness of process by name
5,499
Solution 1
If you have only one java
instance running, simply:
renice -n 5 -p $(pgrep ^java$)
$(pgrep ^java$)
: command substitution;bash
replaces this with the output ofpgrep ^java$
;pgrep ^java$
returns the list of PIDs of the processes whose name matches the regular expression^java$
, which matches all processes whose name is exactlyjava
If you have multiple java
instances running:
for p in $(pgrep ^java$); do renice -n 5 -p $p; done
for p in $(pgrep ^java$); do renice -n 5 -p $p; done
: almost the same as above;$(pgrep ^java$)
is a command substitution;bash
replaces this with the output ofpgrep ^java$
;pgrep ^java$
returns the list of PIDs of the processes whose name matches the regular expression^java$
, which matches all processes whose name is exactlyjava
; this is expanded into thefor
loop, which assigns a new line of the output ofpgrep ^java$
to the variable$p
and runsrenice -n 5 -p $p
at each iteration until the output ofpgrep ^java$
is consumed
Solution 2
Try this:
pgrep java | xargs -n 1 echo renice -n 5 -p
If output is okay, remove echo
.
Related videos on Youtube
Author by
Sebastian Piskorski
Updated on September 18, 2022Comments
-
Sebastian Piskorski over 1 year
Java process is killing my computer (Intel i3, 8GB RAM). It takes over whole CPU and system starts to hang. I was trying to change
niceness
of java processes but i have to control it for all time and this is not always possible. So for beginning I tried to construct a command to change processniceness
by name. Ended up with something like this:ps ax -o pid,comm | grep java | awk '{print $1}' | tr "\n" " " | renice -n 5 -p
But it looks like it doesn't work. And I don't know where to go next. Bash script maybe? Run it by
cron
or bywatch
everyx
time? Or is there a better way? -
Sebastian Piskorski almost 9 yearsThat works pretty well. But to take next step, what is better to run this periodically. Some cron or watch command?
-
Corbie almost 4 yearsThe first command is also working, if you have one single program running multiple processes (like a parallel processing thread pool in MATLAB).