Nice Command in Linux: How to Use It

Nice Command in Linux: How to Use It

A backup or batch conversion can share CPU time with work you need to use interactively. The nice command starts a program with a scheduling niceness value, while renice changes that value on a running process.

I’ll show how nice adjusts a command at launch and how renice changes and verifies a running process’s niceness.

TL;DR

The nice command starts a program with an adjusted scheduling niceness. Use renice to change a running process’s value.

  • nice -n 7 command adds 7 to the current niceness.
  • Lower values mean more favorable scheduling, with the usual Linux range from -20 to 19.
  • Check the NI column with ps or top.
  • Negative values may require extra privilege.

What Does nice Do in Linux?

Niceness is a scheduling hint that favors lower values when the scheduler chooses work. On Linux, the familiar range is -20 to 19 and a process commonly starts at 0. A lower number means greater scheduling favor.

For a new command, nice applies an adjustment at launch. For an existing one, renice changes its scheduling value. Children inherit their parent’s nice value, so launching a shell at a changed value also affects commands started from that shell.

How to Use nice and renice in Linux

At launch, a positive adjustment lowers the process’s scheduling urgency, which suits a non-urgent background job. Check the resulting NI value before relying on the change.

Step 1: Start a command with an adjustment

The -n value is added to the current niceness. GNU nice uses 10 when no adjustment is supplied. The script launches a child shell and prints its inherited value.

#!/bin/sh
nice -n 7 sh -c 'printf "nice=%s\\n" "$(nice)"'

Run the script with ./nice_value_demo.sh. Its output is:

nice=7
Terminal output showing a process niceness of 7

In GNU nice, the adjustment is relative to the command’s inherited value. If the calling shell has niceness 7, a further adjustment of 3 starts the child at 10. A concrete no-output check is:

nice -n 3 true

This exits without printing text. Check the NI column when the parent shell’s value may not be zero.

Step 2: Check the process value and PID

For an interactive overview, top displays a process table with a priority-related NI column. Its screen updates continuously, while ps prints a snapshot suited to scripts and logs. A PID identifies a task for a targeted change, so use the command name and owner columns to avoid choosing a similarly named process by mistake.

ps -o pid,ni,pri,comm -p <PID>

Replace with a process number. The NI field shows niceness and PRI shows a separate scheduler priority display.

Step 3: Change niceness on a running process

renice changes the scheduling value after launch. In this script, a temporary sleep process starts at 0, receives an absolute nice value of 12, and is checked again before the script ends it.

#!/bin/sh
sleep 30 &
pid=$!
printf 'PID=%s\\n' "$pid"
ps -o pid,ni,pri,comm -p "$pid"
renice 12 -p "$pid"
ps -o pid,ni,pri,comm -p "$pid"
kill "$pid"
wait "$pid" 2>/dev/null || true

Run ./renice_demo.sh. The exact captured output was:

PID=120467
    PID  NI PRI COMMAND
 120467   0  19 sleep
120467 (process ID) old priority 0, new priority 12
    PID  NI PRI COMMAND
 120467  12   7 sleep
Terminal showing a sleep process changed from nice 0 to nice 12

Util-linux renice sets an absolute value here. Its relative mode specifies a change. Check the displayed NI value afterward, since permissions can constrain the requested value.

Nice Command Limits and Common Errors

The scheduler’s response depends on runnable work and other system settings.

SituationWhat to expectWhat to do
Positive adjustment with niceUsually allowed for your commandCheck NI with ps
Negative adjustmentMay fail with permission deniedUse only with the needed privilege or configured resource limit
Changing another user’s processOwnership or capability rules applyCheck process owner and authorization
NI changed, CPU use looks similarNiceness is not a CPU quotaCompare behavior under CPU contention

A higher nice value is a reasonable choice for a long-running backup or batch conversion that can yield CPU to interactive work. Keep the default value for ordinary commands unless there is a scheduling reason to change it. If there is no CPU contention, the adjusted process can still run at full speed, so compare responsiveness while other tasks are active instead of judging by CPU percentage alone.

Unprivileged users can usually increase niceness only on processes they own. Lowering the value to raise scheduling favor may require privilege or a suitable RLIMIT_NICE limit.

A permission error means the caller is not authorized for that priority change. Check ownership and the configured limit before retrying.

The range has practical limits. GNU nice treats a value outside the supported range as its nearest endpoint, not as an unlimited priority boost or reduction. A process at 19 cannot be made less urgent by requesting a larger value.

A nice adjustment changes CPU scheduling preference only. It does not set a memory limit, choose disk request order or control a time-sensitive scheduling policy. Use it for relative CPU urgency, not as a general resource quota.

For a quick local reference, both commands provide help output. Check the installed help text if an option is unfamiliar, since GNU nice and util-linux renice are separate utilities with distinct option conventions.

nice --help
renice --help

A process-group or user selector can change several related tasks in one operation. This is useful when a batch job starts child processes or when a user owns a group of background jobs. It can also affect unrelated work under that user, so inspect the target set first and prefer an explicit PID for a single-task adjustment.

Conclusion: Adjust Niceness, Not CPU Guarantees

Use nice at launch and renice on a running task. See the htop guide for a live process view.

FAQ

These answers cover the common questions about Linux niceness.

What does the nice command do in Linux?

It starts a command with adjusted scheduling niceness. Lower values are more favorable to the process, but do not guarantee CPU time.

How do I change the nice value of a running process?

Use renice with the process ID, such as renice 12 -p 1234, then check the NI field with ps -o pid,ni,comm -p 1234.

Why does a negative nice value require permission?

A lower nice value raises scheduling favor. Linux restricts that change unless the caller has suitable privilege or a configured nice resource limit.