Robert Elder Software Inc.
  • Home
  • Store
  • Blog
  • Contact
  • Home
  • Store
  • Blog
  • Contact
  • #linux
  • |
  • #commandline
  • |
  • #softwareengineering
  • |
  • #embeddedsystems
  • |
  • #compilers
  • ...
  • View All >>

Intro To 'nice' Command In Linux

2023-07-19 - By Robert Elder

     I use the 'nice' command to change how much CPU time a process gets:

nice date
Thu 20 Jul 2023 10:26:48 AM EDT

Running A Process Without 'nice'

     If I run this 'factor' command, it uses a lot of CPU and takes several seconds to finish:

factor 48324798327489321749327483254325354313
48324798327489321749327483254325354313: 3 3 11 23 3618065157073333 5865845715918448993

Running A Process Slower With Lower CPU Priority

     I can make sure that the 'factor' command is nice to other process by running it with a 'nice value' of 19:

nice -n 19 factor 48324798327489321749327483254325354313
48324798327489321749327483254325354313: 3 3 11 23 3618065157073333 5865845715918448993

Running A Process Faster With Higher CPU Priority

     I can also make the 'factor' command less nice by using a negative niceness value of -20:

sudo nice -n -20 factor 48324798327489321749327483254325354313
48324798327489321749327483254325354313: 3 3 11 23 3618065157073333 5865845715918448993

Demonstrating The Usefulness Of The 'nice' Command

     Here, I have a C program that uses lots of CPU and then prints out how much time it's been running:

#include <sys/time.h>
#include <sys/resource.h>
#include <stdio.h>
#include <unistd.h>

int main(int argc, char *argv[]){
        struct timeval stop, start;
        gettimeofday(&start, NULL);
        long long int i = 0;
        while(i < 1000000000L){
                i++;
        }
        gettimeofday(&stop, NULL);
        int diff_seconds = stop.tv_sec - start.tv_sec;
        int diff_useconds = stop.tv_usec - start.tv_usec;
        long long int us = diff_seconds * 1000000L + diff_useconds;
        double num_seconds = (double)us/(double)1000000;
        int pid = getpid();
        int priority = getpriority(PRIO_PROCESS, 0);
        printf("Finished in %.02f seconds, pid=%d, niceness=%d, i=%s.\n", num_seconds, pid, priority, argv[1]);
}

     If I compile this program like this:

gcc my-program.c -o my-program

     and use the following wrapper script 'do-random-niceness.sh':

#!/bin/bash

for i in {1..200}; do
        NICENESS_TO_USE=$(($RANDOM % 40 - 20))
        nice -n $NICENESS_TO_USE ./my-program $i &
done

     to launch 200 different instances of it with random niceness values using the following script:

sudo ./do-random-niceness.sh

     you can see that the copies of this process that have negative niceness values finish first, while the ones with most positive niceness values tend to finish last:

Finished in 3.47 seconds, pid=38049, niceness=-17, i=5.
Finished in 3.59 seconds, pid=38072, niceness=-20, i=28.
Finished in 4.26 seconds, pid=38112, niceness=-20, i=68.
Finished in 4.68 seconds, pid=38097, niceness=-20, i=53.
Finished in 3.46 seconds, pid=38244, niceness=-19, i=200.
Finished in 6.43 seconds, pid=38077, niceness=-17, i=33.
Finished in 6.56 seconds, pid=38054, niceness=-17, i=10.
Finished in 5.98 seconds, pid=38168, niceness=-17, i=124.
Finished in 6.71 seconds, pid=38152, niceness=-16, i=108.
Finished in 7.51 seconds, pid=38115, niceness=-17, i=71.
...Many Omitted entries...
Finished in 48.21 seconds, pid=38159, niceness=19, i=115.
Finished in 47.74 seconds, pid=38181, niceness=16, i=137.
Finished in 48.07 seconds, pid=38074, niceness=17, i=30.
Finished in 48.28 seconds, pid=38116, niceness=19, i=72.
Finished in 48.47 seconds, pid=38094, niceness=19, i=50.
Finished in 48.83 seconds, pid=38107, niceness=16, i=63.
Finished in 48.45 seconds, pid=38153, niceness=18, i=109.
Finished in 47.22 seconds, pid=38213, niceness=19, i=169.
Finished in 48.18 seconds, pid=38165, niceness=19, i=121.

     This is true even though every process is identical and the order in which they were launched doesn't matter.

Default Value

     If you run the 'nice' command without arguments, it will print the current niceness value:

nice
0

     You can use this feature to find the default 'niceness' value of the 'nice' command itself:

nice nice
10

     And that's why the 'nice' command is my favourite Linux command.

Intro To 'stty' Command In Linux
Intro To 'stty' Command In Linux
Published 2023-10-04
Terminal Block Mining Simulation Game
$1.00 CAD
Terminal Block Mining Simulation Game
Intro To 'nproc' Command In Linux
Intro To 'nproc' Command In Linux
Published 2023-07-15
Intro To 'comm' Command In Linux
Intro To 'comm' Command In Linux
Published 2023-09-06
How To Force The 'true' Command To Return 'false'
How To Force The 'true' Command To Return 'false'
Published 2023-07-09
A Surprisingly Common Mistake Involving Wildcards & The Find Command
A Surprisingly Common Mistake Involving Wildcards & The Find Command
Published 2020-01-21
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
Published 2019-08-01
Intro To 'chroot' Command In Linux
Intro To 'chroot' Command In Linux
Published 2023-06-23
Join My Mailing List
Privacy Policy
Why Bother Subscribing?
  • Free Software/Engineering Content. I publish all of my educational content publicly for free so everybody can make use of it.  Why bother signing up for a paid 'course', when you can just sign up for this email list?
  • Read about cool new products that I'm building. How do I make money? Glad you asked!  You'll get some emails with examples of things that I sell.  You might even get some business ideas of your own :)
  • People actually like this email list. I know that sounds crazy, because who actually subscribes to email lists these days, right?  Well, some do, and if you end up not liking it, I give you permission to unsubscribe and mark it as spam.
© 2025 Robert Elder Software Inc.
SocialSocialSocialSocialSocialSocialSocial
Privacy Policy      Store Policies      Terms of Use