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
Comments on this entry are closed.
Hi,
thanks a lot
Very nice and useful article.
Thanks again
Thanks.
Never heard of that feature, but it’s good that I know it now.
Learnt a new concept….hope i use it in future experiences…
Nice article
Very useful article. Thanks a lot.
thanks. Learnt something new today
thanks a lot …..
nice tutorial about nice
Simply superb 🙂 Explained in a easy way to understand.
Hi ,
This is a useful information.
Thanks
You can tweak the niceness using htop too.
F7 bumps down the nice level (higher priority); F8 bumps it up (lower priority).
So Nice,
Thanks for sharing this useful information.
Hi Sirji,
Very nice and useful article.
Thanks
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” ?
your code doesnt work
the ni is not changing even after firing nice command with -10 priority
Awesome article , TGS make things very simple there 🙂
Thanks,
Razal
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.
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
Nice article….
nice article to read….thanks
Nice article
it helps a lot
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
Thanks!