Skip to content
shellhacks.dev
Linux

Run a Command at Lower CPU Priority with nice on Linux

Start a command at lower priority with nice -n 10 cmd. The value is added to the current niceness and capped at 19, and only root can use a negative value.

1 min read
Summarize in AI

A batch job should not slow down interactive work on the same machine.

Shell
nice
nice -n 10 nice
nice -n 10 nice -n 15 nice
setpriv --reuid=1000 --regid=1000 --clear-groups nice -n -5 nice
Text
0
10
19
nice: cannot set niceness: Permission denied
0

nice with no command prints its current niceness, which is 0. nice -n 10 nice prints 10, so the increment is added to the niceness of the caller. Asking for 25 in two steps, 10 and then 15, stops at 19, the highest value. Lowering the value below zero needs privilege, and the unprivileged run shows cannot set niceness: Permission denied followed by the command running anyway at 0. See nice invocation.

Niceness only matters when CPUs are contended, and it does nothing for disk or network, where ionice applies. The warning does not stop the command, so a script that depends on the priority has to check the result. Use renice for a process that is already running.

Share

More snippets

Get new posts by email

One email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.