Linux

Manipulate process priority with nice


Modern operating systems are multi-user and multitasking, which means that multiple users and multiple tasks can be using the computer at any given time. Typically you'll have one person using a desktop system running any number of applications or many users using many applications on a server.

The amount of time devoted to tasks largely depends on how intensive the task is. Some tasks require higher priority than others; for instance, if you were compiling a large software package you didn't need immediately, that priority should probably be lower than your Web browser or e-mail client.

Each process has a niceness value associated with it, which is what the kernel uses to determine which processes require more processor time than others. The higher the nice value, the lower the priority of the process. In other words, the "nicer" the program, the less CPU it will try to take from other processes; programs that are less nice tend to demand more CPU time than other programs that are nicer.

The priority is noted by a range of -20 (the highest) to 20 (the lowest). Using ps, you can see the current nice value of all programs:

$ ps axl
F   UID   PID  PPID PRI  NI    VSZ   RSS WCHAN  STAT TTY        TIME COMMAND
4     0     1     0  16   0   2648   112 -      S    ?          0:01 init [3]
1     0     2     1  34  19      0     0 ksofti SN   ?          0:02 [ksoftirqd/0]
5     0     3     1  10  -5      0     0 worker S<   ?          0:00 [events/0]
...

You can see that init has a nice value of 0, while other kernel tasks associated with PID 2 and 3 have a nice value of 19 and -5 respectively.

Typically, a program inherits its nice value from its parent; this prevents low priority processes from spawning high priority children. Having said that, you can use the nice command (as root or via sudo) with the command you wish to execute in order to alter its nice value. Here is a short illustration:

# ps axl | grep axl | grep -v grep
4     0 30819 30623  15   0   4660   772 -      R+   pts/0      0:00 ps axl
# nice -10 ps axl | grep axl | grep -v grep
4     0 30822 30623  30  10   4660   772 -      RN+  pts/0      0:00 ps axl

You can see there that the nice value, represented by column six, has been altered. You can also use the renice command to alter running processes. In the following example, vim was started to edit the file foo and began with a default nice value of 0. Using renice, we can change its priority:

# ps axl | grep vim | grep -v grep
0     0 30832 30623  16   0  15840  3140 -      S+   pts/0      0:00 vim foo
# renice -5 30832
30832: old priority 0, new priority -5
# ps axl | grep vim | grep -v grep
0     0 30832 30623  15  -5  15840  3140 -      S<+  pts/0      0:00 vim foo

Here, we have adjusted the priority of vim, giving it a slightly higher priority. Renice operates on the process ID, so using grep, we determined that vim is process ID 30832 and saw that the nice value was 0. After executing renice, the nice value is now -5.

Standard caveats apply: Only root can alter the nice priority of programs. So if you find that your compilation is taking too much CPU from other activities, consider renicing the parent process via root. Subsequent children should have a better nice value, or you can even start the compilation (or any other activity) with nice, specifying an appropriate nice value. You can also use renice to renice all programs belonging to a process group or user name/ID.

Delivered each Tuesday, TechRepublic's free Linux NetNote provides tips, articles, and other resources to help you hone your Linux skills. Automatically sign up today!

About

Vincent Danen works on the Red Hat Security Response Team and lives in Canada. He has been writing about and developing on Linux for over 10 years and is a veteran Mac user.

7 comments
S,David
S,David

I have found nice to be one of those things that sound useful, but aren't, like "digital rights management" and the CANSPAM act. On a single user system you could do some good, maybe. My experience with many *ix flavors is that too many things niced low and other stuff stops working, and on a busy system stuff niced high never gets run. I believe it is better to leave things at the default nice and let the system handle it, at least in a production environment. On a desktop, where you are the only one there, by all means, play with it, but don't be surprised if the system turns around and bites you on the bottom for doing so.

travisn000
travisn000

Nice tip.. another way to tuen performance on a linux desktop is to adjust swapiness. Also, for those that like ubuntu on the desktop, you should really also take a look at PCLinuxOS.. it is also debian based, but requires less configuration out of the box, and also has all the great gui's for system configuration task from Mandiriva / Mandrake. It is a great OS for those you may be trying to convert!

TJ111
TJ111

This is one of those awesome little features that linux has hidden among it that you'd never know about until you read something like this. Working with servers, this would be a great way to ensure that processes are prioritized properly to reduce server load. And at home, maybe I can squeeze a little more performance out of CS:S when I run it in Ubuntu. Ahhh, linux...

thephoton
thephoton

You just can't beat Linux for the desktop - more applications, utilities, and control than you will ever know what to do with. And, with Open Source if you don't like it, you at least have a shot at making it better by either writing it yourself or posting a request for a change at the host site. You are probably running Linux on some of your hardware and don't even know it - many routers, NAS systems, and other hardware run small versions of Linux. Microsoft may have the market right now, but if people really gave Linux a shot then M$ would soon be gone. Try Ubuntu if you think Fedora is a little to big or you want to try a Live CD (Linux versions which boot and run off the CD.) Lastly, you don't need anything close to the hardware that is required to make a Windows system function. Buy that a new $300 or used $150 system and give it a try, you may never turn the Windows machine back on.

alaniane
alaniane

few computer users are going to bother with prioritizing their processes. Maybe a few will try and kill a process. Windows is written for average users not for geeks. I like Linux and the abilities it gives me, but I am programmer not an office admin or an accountant. Being able to fine-tune how you tweak a system does not appeal to a majority of the computer users out there. What will win the common users is making the OS easy to work with. Linux has made a lot of progress in becoming more user-friendly for non-geeks; however, it needs to work on marketing its product to non-geeks. Marketing was what Microsoft Windows successful. There were other GUIs for PC machines before Windows became dominant. GeoWorks is a good example. In fact GeoWorks provided more builtin apps than Win 3.0; however, it failed to market its GUI.

zefficace
zefficace

Priority is a nice thing to control, but the kill command that just shoot down a bad process is great. In any case, it's a lot better than Windows asking you if you want to stop an unresponsive process... it takes ages, and clicking "yes" seems to be a placebo more than anything else.

jlwallen
jlwallen

i tend to use killall over kill simply because i don't have to look up the PID first. so "killall -9 firefox-bin" is better than going "ps aux|grep firefox" and then "kill PID".

Editor's Picks