How to set process priorities with nice and renice under Linux

the nice and renice You can use commands to optimize how the kernel handles your processes by adjusting their priorities. Read this tutorial to learn how to use them in Linux and Unix-like operating systems like macOS.

It’s all a question of the process

There are many processes running in your Linux or Unix-like computer even before you even start the application you want to use. Most of these processes are important elements of Linux itself or supporting processes for your graphical desktop environment. A lot is happening behind the scenes. Of course, there are only a limited number of system resources and CPU times. The Linux kernel is the controller for all of these processes.

It is the kernel that has to decide which processes are currently receiving attention and resources and which have to wait. The kernel is constantly juggling processes and priorities to ensure that the computer is running as smoothly as possible and that all processes are getting their fair share. Some processes are given preferential treatment. They are so important to the general operation of the computer that their needs must come before your browser, for example.

The beautiful value

One of the criteria used to determine how the kernel handles a process is its nice value. Every process has a beautiful value. The nice value is an integer in the range -19 to 20. All standard processes start with a nice value of zero.

The trick here is that the higher the Nice value, the better your process is compared to the other processes. In other words, a high nice value tells the kernel that this process is happy to wait. A negative number is the opposite of nice. The larger the negative nice value, the more selfish the process. It tries to get as much CPU time as possible regardless of the other processes.


We can use that nice Command to set the nice value if a Process is started and we can use renice to adjust the beautiful value of an ongoing process.

The nice command

We can use that nice Command to adjust the nice value for a program when we start it. This allows us to increase or decrease the priority that the kernel gives the process relative to the other processes.

Let’s say a programmer has a program called. written ackermann . This calculates Ackerman functions. It is CPU and memory intensive. The programmer can start the program with the following command:

./ackermann

We can use that top Command to display the current program.

top

top runs in a terminal

We can get the details of the ackermann Program in top. The nice value is the number in the “NI column”. It was zeroed out as we would expect.

Let’s restart it and make it less demanding this time. We set a nice value of 15 for that ackermann Program as follows. Enter nice, a space, -15, another space, and then the name of the program you want to start. In our example, our fictional programmer used ./ackermann.

nice -15 ./ackermann

nice 15 command in the terminal window


Notice that “-15” is not minus fifteen. It’s positive fifteen. The “-” is required to say nice We pass a parameter. To enter a negative number, you must enter two “-” characters.

If we start now top again we can see the behavior change from ackermann.

top

top runs in a terminal

With a nice value of 15, ackermann does not consume most of the CPU time. GNOME and Rhythmbox both use more. We have reined in ackermann soon.

Now we do the opposite and give ackermann a negative nice value. Note the use of two “-” characters. To make an application more selfish and less nice you need to use sudo. Anyone can make their application more beautiful, but only superusers can make one more selfish.

sudo nice --10 ./ackermann

nice -10 command in the terminal window

Let’s run upstairs and see what a difference that made.

top

top runs in a terminal

This time ackermann has a nice value of -10. It’s back on top, consuming more CPU time than before.

The Renice Command

the renice With the command we can adjust the nice value of a running process. We don’t have to stop and restart with either nice. We can set a new value in no time.


the renice Command uses the process ID or PID of the process as a command line parameter. We can either extract the process ID from the “PID” column in top , or we can use ps and grep to find it for us as follows. Obviously you are typing your user’s name instead of. a dave and the name of the process you are interested in instead ackermann.

ps -eu dave | grep ackermann

Now that we have the PID we can use that renice. We will hire ackermann back to nicer behavior with a nice value of five. To change the nice value for a running process, you must use sudo. Notice that there is no “-” on that 5 Parameter. You don’t need one for positive numbers and you only need one, not two for negative numbers.

sudo renice -n 5 2339

We get confirmation that renice changed the nice value. It shows us the old value and the new value.

The kernel usually does an excellent job of handling priorities and distributing CPU time and system resources. However, if you have a long, CPU-intensive task to do and you don’t care when it finishes, increasing the number of this task will make your computer a little smoother. It will be nicer for everyone.

Linux commands
Filestar · pv · cat · tac · chmod · grep · difference · sed · With · man · pushed · popd · fsck · Test disk · seq · fd · pandoc · CD · $ PATH · awk · join · jq · wrinkles · unique · Journalctl · tail · stat · ls · fstab · echo · fewer · chgrp · chown · rev · look · Strings · Type · rename · Postal code · unzip · assemble · ummount · To install · fdisk · mkfs · rm · rmdir · rsync · df · gpg · weather · Nano · mkdir · from · ln · Patch · Convert · rclon · Scraps · srm
Processesalias · screen · above · kind · renice · progress · strace · system · tmux · chsh · story · at · Batch · for free · which · dmesg · chfn · User mod · ps · chroot · xargs · tty · pinkie finger · lsof · vmstat · Time out · Wall · Yes sir · kill · sleep · sudo · it is · Time · groupadd · User mod · groups · lshw · switch off · start anew · Stop · switch off · passwd · lscpu · crontab · date · bg · fg
Networkingnetstat · Ring · Trace route · ip · ss · who is · fail2ban · bmon · she · finger · nmap · ftp · curl · wget · who · who am I · w · iptables · ssh-keygen · ufw

Best Linux Laptops for Developers and Enthusiasts

Related Posts