I have a background process running at 100% CPU on Mac OS X. All other applications are very slow because of it.
I'd like to set this process to take no more than 50% so that my applications can run better. How can I do this?
From the command line (Terminal.app or whatever) use nice and renice, just like on other unixes.
Use nice when launching a process:
nice -n <priority> <command> <arguments to command>
The default priority is zero, positive values are "nicer" (that is lower priority) and negative values are "less nice" (higher priority). Looks like Mac OS runs from +10 to -10.
Use renice to change the priority of a process already running (from the renice man page on 10.5):
renice priority [[-p] pid ...] [[-g] pgrp ...] [[-u] user ...]
renice -n increment [[-p] pid ...] [[-g] pgrp ...] [[-u] user ...]
The part you're interested in here is the pid bit. That is the process id for the job and you can find it using ps -u <your username> and looking for the process name, but I prefer top -o in this case, because the process you're interested in will be near the top.
Note: Without superuser privileges you can never increase a process's priority. For normal users, nice and renice are one way streets. And small changes in priority can have large effects on running time. So go easy on this until you understand it.
You can use the command:
renice -n # PID
Where:
top on the terminal app (utilities/terminal.app)If it is a system process or another user process you should type:
sudo renice -n 10 PID
It will ask you for your password (if you are sudoer). As for the number I would recommend 10 or 19 (even lower priority).
Note that this will change the priority not the CPU usage. If you aren't running other processes which require CPU or you have more than one CPU on your Mac (Core 2 Quad Core) the process might still use 100% of CPU.
renice 20 $(pgrep ImageOptim)
Or use the name of your program instead of ImageOptim
If your process is an app or app helper utility rather than a system process and you don't want to do nice or renice on every system boot you can give it a shot to AppPolice.
It's open source and free. You can download it here, or install it with the homebrew command brew cask install AppPolice
If you want to change a running process or service or app's macOS alloted CPU-thread resources, and also change scheduling priority by applying the renice on running process via GUI tool, then you may try CPUSetter. Its not open-source though, but works in newer macOS (tested on macOS Monterey v12.6.1), and cost-free. It is a voluntary donation-ware.
Another alternative would be a very nice command line task manager GUI called htop. You can see all the processes and tune their nice values by F7 and and F8 shortcuts.
(Please note that negative nice values are more prioritized and opposite for positive values)
htop to see and manage processes in Terminal or sudo htop to cover all the system.
Careful when you set scheduling-priority value with nice or renice tool/function:
20 or +20 is lowest priority.-20 is highest priority.There are also a number of GUI utilities, like the free BeNicer and Process Wizard (my previous favorite), and the $1.99 version of Freezer, which is my new favorite. These all work on running applications.
You can set the nice value (priority) for the daemon permanently using the variable in the PLIST file for the app. To find out how type MAN plist in a terminal window.