Processes and jobs
nice
Run a command with a lower (or, as root, higher) CPU scheduling priority, or print the current niceness.
nice [-n ADJUSTMENT] [COMMAND [ARG...]]
Every process has a niceness from -20 to 19. The higher the number, the "nicer" the process
is to others: when the CPU is contended, the scheduler gives it a smaller share. Niceness only
matters when something else wants the CPU; on an idle machine a niced job runs at full speed.
nice -n 10 make -j8 is the classic use: a big build or compression job that should not make
the rest of the machine sluggish.
nice adds its adjustment to the current niceness (default +10) and starts the command with
the result, clamped to 19. Children inherit the value, so niceness set on a script applies to
everything it runs. Ordinary users can only make things nicer; lowering the number (raising
priority) needs root, or more precisely the CAP_SYS_NICE capability. To change a process that
is already running, use renice.
Niceness governs CPU only. For a job that hammers the disk, ionice -c3 (idle I/O class) is the
companion tool; the two are often combined: nice -n 19 ionice -c3 tar czf .... In ps and
top, niceness is the NI column.
Examples
Print the current niceness
nice
0
With no command, nice prints the niceness it inherited. Shells and services normally start
at 0.
Run a command at lower priority
nice nice
10
The outer nice runs the inner one with the default adjustment of +10, and the inner one
prints what it got. Any command works in place of the inner nice.
Choose the adjustment
nice -n 5 nice
nice -n 19 nice
nice -n 50 nice
5 19 19
-n N adds N. 19 is the lowest priority there is; larger adjustments are silently clamped
to it, as the third line shows.
Adjustments add up
nice -n 5 nice -n 3 nice
8
The first nice starts at 0 and gives 5; the second adds 3 to that. Niceness is relative
to whatever the parent had.
See niceness in the process list
nice -n 10 sleep 300 &
sleep 300 &
ps -o pid,ni,pri,args -C sleep
PID NI PRI COMMAND 1615 10 9 sleep 300 1616 0 19 sleep 300
The NI column shows niceness. PRI is the kernel's resulting priority as procps reports
it, where a higher niceness gives a lower number; compare NI, which is what you set.
Children inherit the niceness
nice -n 7 bash -c 'sleep 300 & ps -o ni=,args= -p $!'
7 sleep 300
The shell started under nice has niceness 7, and so does the sleep it starts. Niceness a
script starts with applies to everything it runs.
Raising priority needs privileges
nice -n -5 nice
echo "exit status: $?"
nice: cannot set niceness: Permission denied 0 exit status: 0
An unprivileged user cannot lower niceness. GNU nice warns cannot set niceness:
Permission denied but still runs the command at the unchanged value (0) and returns its
status. As root, sudo nice -n -5 cmd works.
Run a heavy job in the background without slowing the machine
nice -n 19 gzip -k -9 access.log &
wait
ls -l access.log*
-rw-rw-r-- 1 dev dev 2309 Oct 24 21:55 access.log -rw-rw-r-- 1 dev dev 591 Oct 24 21:55 access.log.gz
The usual pattern for compression, backups and builds: nice it fully and background it. On a busy machine the job yields to interactive work; on an idle one it finishes just as fast.
Common pitfalls
Writing
nice -10 cmdand expecting -10. In that old syntax-10means an adjustment of +10; a negative value would be--10. Use-nand the meaning is unambiguous.Expecting
niceto help with disk or memory contention. It only affects CPU scheduling; useionicefor disk I/O.Niceness is not a hard limit. A niced job on an idle machine still uses 100% of a core. To cap CPU use, use cgroups (systemd's
CPUQuota=) orcpulimit.Assuming a failed negative adjustment stops the command. GNU
niceonly warns and runs it anyway at the old niceness.
GNU vs BSD and macOS
The examples above ran the GNU/Linux version. These are the differences that matter on macOS and the BSDs (written from their documentation, not run here).
nice -n Nis POSIX and works the same on macOS and the BSDs. The shell may have its ownnicebuiltin (csh and tcsh do) with slightly different syntax;command niceor/usr/bin/nicegets the standalone program.ioniceis Linux-only (util-linux).
Options worth knowing
A selection, described in our own words. For the full list, see nice(1) or nice --help.
| Option | What it does |
|---|---|
-n, --adjustment N | Add N to the current niceness (default 10); negative values need privileges. |
Related
reniceChange the niceness (CPU scheduling priority) of processes that are already running, by PID, process group or user.psPrint a snapshot of running processes, with the columns, filters and sort order you choose.topShow a live, sorted view of processes and system load; batch mode prints snapshots for logs and scripts.timeMeasure how long a command takes (wall-clock, user and system CPU time); GNU /usr/bin/time also reports memory and I/O.