How to change niceness of process by name
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
Try this:
pgrep java | xargs -n 1 echo renice -n 5 -p
If output is okay, remove echo
.