How to renice by process name?

I am trying to Launch a process using NICE but this process is starting other process by itself and they are not affected by the priority of the main process ( main process has the correct nice but sub process priority is set to default)

So I am trying to renice those process while they run.
I tried

 renice n -p $(pidof <process name>)

but it is not recognised by MacOS (it is probably linux specific ?) so how can I do that?

you can find a related discussion here: https://stackoverflow.com/questions/30062340/starting-process-with-nice-command-macos-leaves-the-process-priority-at-0#comment48287323_30062340

thanks.


You can use pgrep instead of pidof on OS X. Using your example from earlier, the following should work:

renice n -p $(pgrep <process name>)

That said, a child started after the parent has had their priority changed should inherit the parent's priority.


There are multiple correct answers, here an answer let suits your train of thought.

Run this oneliner:

renice 10 -p $(ps -ax | grep -i [p]rocessname | awk 'NR==1{print $1}')

What is does:

  • renice 10, this will make your process run with the nicety of ten, the nicer, the higher the number.
  • -p expects the process id
  • ps -ax lists all running processes with there name and PID
  • grep -i [p]rocessname only matches the processes with 'processname', case insensitive. the brackets will prevent to match the grep process itself.
  • awk 'NR==1{print $1}' will fetch the 1st instance (NR==1) of the first column ($1)

You already gave a good example of command substitution.