Set process priority
Run a CPU-hungry job at lower priority so it does not steal cycles from interactive work — or, conversely, give a latency-sensitive job a priority boost.
How to set process priority in each shell
nice -n 19 ./long-task`-n N` where N is in the range -20 (HIGH priority) to +19 (LOWEST). Negative values require root. Default if no `-n` is +10. Use `renice -n 19 -p PID` to change priority of an already-running process.
nice -n 19 ./long-tasknice -n 19 ./long-task(Get-Process -Id $pid).PriorityClass = 'BelowNormal'Enum: Idle / BelowNormal / Normal / AboveNormal / High / RealTime. RealTime requires admin and can FREEZE the system if the process hogs CPU. BelowNormal is the safe equivalent of `nice -n 10`.
start /low cmd /c long-task.exe`/low`, `/belownormal`, `/normal`, `/abovenormal`, `/high`, `/realtime` — map directly to the .NET PriorityClass enum. `start /high` requires admin; `/realtime` requires admin AND is dangerous.
Equivalents listed for Bash, Zsh, Fish, PowerShell, cmd.exe.
Gotchas & notes
- **Niceness range -20 to +19, with COUNTER-INTUITIVE direction**: -20 is "very nice to OTHERS by giving ME priority" no wait — -20 is "I am LEAST nice, I take CPU first"; +19 is "I am NICEST, I cede CPU to everyone else". The mnemonic: "nice" = considerate of others = lower priority for yourself = HIGHER number. Negative values (HIGHER PRIORITY) require root (`sudo nice -n -10 cmd`). The Linux kernel CFS scheduler maps niceness to weight: each step is roughly 10% — niceness 19 gets ~1/56 the CPU weight of niceness 0. Niceness only affects CPU; for I/O priority use `ionice` (next note).
- **`ionice` for disk I/O priority** — separate from CPU. `ionice -c 3 ./batch-job` runs at "idle" I/O priority (only gets disk when nothing else wants it). Classes: 1=realtime (root only), 2=best-effort (default, sub-priorities 0-7), 3=idle. Useful for rsync backups, `find` scans, or `tar` runs on a busy server — `nice -n 19 ionice -c 3 rsync …` runs LOW on both axes. Only works on CFQ/BFQ I/O schedulers (most distros default to one of these); on `noop` or `deadline`, `ionice` is silently ignored.
- **Real-time priority is DANGEROUS** — `chrt -f 50 cmd` on Linux (SCHED_FIFO real-time policy at priority 50) or pwsh `PriorityClass = "RealTime"` runs the process ahead of EVERYTHING including kernel threads. A bug (infinite loop, deadlock) FREEZES the system — even sshd can't schedule. Only use for hard-real-time workloads (audio, video synchronisation, embedded control) and ALWAYS limit to specific cores (`chrt -f --cpu 0 50 cmd`) with watchdog. For "I really need this to be fast" — `nice -n -10` (high but not RT) is almost always the right answer.
- **Changing priority on a RUNNING process**: `renice` (Linux/macOS) — `sudo renice -n 19 -p 12345` (root needed to LOWER priority of another user's process, or to RAISE priority of your own). On modern Linux 4.10+, you can also renice EVERY thread of a process tree atomically: `renice -n 5 -g 12345` (group). pwsh: `(Get-Process -Id 12345).PriorityClass = "BelowNormal"` — instant, no admin if it's your own process. `taskset -p -c 0,1 12345` is a SEPARATE concept — CPU affinity (limit to specific cores), not priority. Affinity + nice often combined: `nice -n 10 taskset -c 0,1 ./batch` runs on cores 0-1 at low priority.
- **Container / cgroup interaction**: inside a Docker/K8s container, `nice` works WITHIN the cgroup but cannot exceed the cgroup's CPU share. `docker run --cpu-shares 512 --cpu-quota 50000 …` are the container-level knobs; nice inside the container only affects relative priority AMONG processes in the same container. K8s `resources.limits.cpu` translates to CFS quota, not nice. If the goal is "this container shouldn't starve other containers" — use container-level CPU shares, not in-container nice. If the goal is "within my container, batch job shouldn't starve the API" — use nice.
Related commands
Related tasks
- Monitor CPU and memory live— Watch CPU and memory utilisation continuously from a shell — for triage, capacity planning, and detecting transient spikes that a one-shot ps would miss.
- Run a command as a daemon— Detach a process from the controlling terminal and any parent shell — so it keeps running after logout, SSH disconnect, or terminal close.
- Find processes using the most CPU— List the top CPU-consuming processes — useful for performance triage, runaway-script detection, and capacity planning.
- Run a command in the background— Launch a long-running command without blocking the current shell session.