≡ Menu

How to Change Process Priority using Linux Nice and Renice Examples

Every running process in Unix has a priority assigned to it.

You can change the process priority using nice and renice utility. Nice command will launch a process with an user defined scheduling priority. Renice command will modify the scheduling priority of a running process.

Linux Kernel schedules the process and allocates CPU time accordingly for each of them. But, when one of your process requires higher priority to get more CPU time, you can use nice and renice command as explained in this tutorial.

The process scheduling priority range is from -20 to 19. We call this as nice value.

A nice value of -20 represents highest priority, and a nice value of 19 represent least priority for a process.

By default when a process starts, it gets the default priority of 0.

1. Display Nice Value of a Process

The current priority of a process can be displayed using ps command.

The “NI” column in the ps command output indicates the current nice value (i.e priority) of a process.

We’ll launch a test program called test.pl which will be used to demonstrate nice and renice command. This test program will do certain tasks, and will be running for a while.

$ perl test.pl

If you execute ps command as shown below, you can notice that this test.pl program has the default nice value of 0 (look at the NI column in the following output).

$ ps -fl -C "perl test.pl"
F S UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  STIME TTY          TIME CMD
0 R bala 6884  6424 99  80   0 -  1556 -      13:45 pts/3    00:05:54 perl test.pl

2. Launch a Program with Less Priority

Instead of launching the program with the default priority, you can use nice command to launch the process with a specific priority.

In this example, test.pl is launched with a nice value of 10.

$ nice -10 perl test.pl

Note: Remember that -10 in the above command sets the priority of a process to 10. The – in nice command stands for the hypen, which we use to pass options to the command.

So, to pass nice value of 5, you’ll say -5. To pass nice value of 6, you’ll say -6.

As you see below, this program is now launched with a nice value of 10, which means this will run at a lower priority when compared to other programs that are launched by default.

$ ps -fl -C "perl test.pl"
F S UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  STIME TTY          TIME CMD
0 R bala 7044  6424 99  90  10 -  1556 -      13:58 pts/3    00:00:03 perl test.pl

3. Launch a Program with High Priority

You can also launch a program with a higher priority. Negative nice value will increase the priority a the process. So, the value has to be specified with a — (two hyphens) in front of the nice command as shown below.

# nice --10 perl test.pl

So, to pass nice value of -5, you’ll add two hyphens in front of 5. To pass nice value of -6, you’ll add two hyphens in front of 6.

As you see below, this program is now launched with a nice value of -10, which means this will run at a higher priority when compared to other programs that are launched by default.

# ps -fl -C "perl test.pl"
F S UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  STIME TTY          TIME CMD
4 R root 3534  3234 99  70 -10 -  1557 ?      19:06 pts/1    00:00:56 perl test.pl

Note: Regular users are not allowed to launch a program with a higher priority. Only root user is allowed to launch a program with high priority.

As a regular user, if you increase the priority, you’ll get the following error message from nice command.

$ nice --10 perl test.pl
nice: cannot set niceness: Permission denied

Note that after printing the above error message, the program would still continue to run with the default priority (i.e : 0).

4. Change the Priority with option -n

The process priority can be adjusted with the help of -n option.

Increase the priority:

# nice -n -5 perl test.pl

Decrease the priority:

# nice -n 5 perl test.pl

5. Change the Priority of a Running Process

The priority of an already running process can be changed using renice command.

In this example, the program test.pl is already running with a nice value of -10.

# ps -fl -C "perl test.pl"
F S UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  STIME TTY          TIME CMD
4 R root 3534  3234 99  70 -10 -  1557 ?      19:06 pts/1    00:00:56 perl test.pl

We can change the nice value of the above program to -19 as shown below. Pass the process id of the above program to -p option.

# renice -n -19 -p 3534

Verify that the nice value got changed to -19.

# ps -fl -C "perl test.pl"
F S UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  STIME TTY          TIME CMD
4 R root 3534  3234 99  70 -19 -  1557 ?      19:06 pts/1    00:00:56 perl test.pl

6. Change the Priority of All Processes that Belongs to a Group

Using -g option you can modify the priority of all processes that belongs to a group. The following command will change the nice value of all the process that belongs to geekstuff to 5.

# renice -n 5 -g geekstuff

7. Change the Priority of All Processes Owned by User

Renice allows to alter the priority of all the processes owned by a specific users as shown below.

# renice -n 5 -u bala

The above command will change the priority of all the processes owned by user bala. It will assign a nice value of 5 to all the processes that belongs to user bala.

# ps -fl -C "perl"
F S UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  STIME TTY          TIME CMD
0 R bala 2720  2607 99  85   5 -  1556 -      14:34 pts/2    00:05:07 perl test.pl
0 R bala 2795  2661 99  85   5 -  1556 -      14:39 pts/3    00:00:09 perl 2.pl
Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • Jalal Hajigholamali August 1, 2013, 8:47 am

    Hi,

    thanks a lot
    Very nice and useful article.
    Thanks again

  • Claudio August 1, 2013, 11:38 am

    Thanks.
    Never heard of that feature, but it’s good that I know it now.

  • Devi Killada August 1, 2013, 9:24 pm

    Learnt a new concept….hope i use it in future experiences…

  • Raj August 2, 2013, 12:09 am

    Nice article

  • Sabindra August 2, 2013, 4:24 am

    Very useful article. Thanks a lot.

  • Bob August 2, 2013, 8:35 am

    thanks. Learnt something new today

  • sanjeev kumar August 2, 2013, 9:10 pm

    thanks a lot …..
    nice tutorial about nice

  • Appu Joseph August 3, 2013, 12:45 am

    Simply superb 🙂 Explained in a easy way to understand.

  • Rajeesh August 3, 2013, 7:51 am

    Hi ,

    This is a useful information.

    Thanks

  • gewg_ August 3, 2013, 8:04 pm

    You can tweak the niceness using htop too.
    F7 bumps down the nice level (higher priority); F8 bumps it up (lower priority).

  • PSDK August 5, 2013, 11:11 pm

    So Nice,
    Thanks for sharing this useful information.

  • Harendra Jha August 7, 2013, 11:17 pm

    Hi Sirji,

    Very nice and useful article.
    Thanks

  • Alex August 10, 2013, 2:57 am

    Thanks a lot for examples
    But i didnt get a difference between 2-3 and 4 items
    I mean – 4. Change the Priority with option -n – isnt that the same as “Launch a Program with Less/Higher Priority” ?

  • sasha October 23, 2013, 4:20 am

    your code doesnt work
    the ni is not changing even after firing nice command with -10 priority

  • Mohammed Razal January 22, 2014, 8:50 pm

    Awesome article , TGS make things very simple there 🙂

    Thanks,
    Razal

  • ulf February 11, 2014, 3:58 pm

    When I watch videos in 720p or higher on my old macbook, sometimes the decoding lags a bit, if there are other processes with high cpu needs running at the same time (in my case it’s unrar). Renicing VLC to a lower value (and therefore a higher priority) did the trick.
    I first check the PID of VLC with ps and the renice it. I don’t use the -n on renice. When you use it without the -n the value isn’t added to the attribute value, it is set to it instead. renice -3 -p 1234 renices the PID 1234 to -3 regardless of it’s former value.

  • Manisha November 21, 2014, 12:24 am

    As per point no 5, the command gives error while changing nice value of running process as below:

    renice -n -1 -p 18007
    renice: -1: bad value
    18007: old priority 0, new priority 0

    But without using -n it runs successfully:
    renice -1 -p 18007
    18007: old priority 0, new priority -1

  • Roshan Singh January 21, 2015, 3:01 am

    Nice article….

  • panda September 8, 2015, 5:43 am

    nice article to read….thanks

  • prajakta ne November 13, 2015, 1:39 am

    Nice article
    it helps a lot

  • Mariano December 21, 2015, 10:02 am

    I am sorry, this command the only thing it does is SUBTRACT to the original priority the value reniced. So if TOP command shows the PR = 15 and NI = 0, when you nice the program to -10, the PR = 5, NI = -10. I need how to change the PR, not nice

  • Wellington Torrejais da Silva May 29, 2017, 11:46 am

    Thanks!