Jul 292011
 

bashOn Linux there is a way to set/change the priority of processes, the user can act to give greater or lesser priority to its own processes.
For example you are running a backup with rsync or doing a tar, but you do not want these processes use all your CPU, in these cases you can make use of the nice command.



nice is a program found on Unix and Unix-like operating systems such as Linux. nice directly maps to a kernel call of the same name. For a given process, it changes the priority in the kernel’s scheduler. A niceness of −20 is the highest priority and 19 or 20 is the lowest priority. The default niceness for processes is inherited from its parent process, usually 0.

nice becomes useful when several processes are demanding more resources than the CPU can provide. In this state, a higher priority process will get a larger chunk of the CPU time than a lower priority process. If the CPU can deliver more resources than the processes are requesting, then even the lowest priority process can get up to 99% of the CPU.

Basic Syntax

nice -n [priority] [command] [arguments]

So you can give to any command a nice value when you first run it.
If you run the command on a terminal without command it will display the current niceness.

Examples:

# nice
0
 
# nice -n 19 gunzip /var/log/oldlogs*

Checking the priority of running processes

The easiest way to display priority for all running processes is to use the top command, you’ll see that there is a NI column, that’s the nice value for the process.

top

An alternative (if you know the pid number) to check a single process nice it’s using the ps command with the following syntax:

ps -o pid,comm,nice -p [PID]

Changing nice value to existing processes

As wrote the command nice it’s usable only when you run a command for the first time, but probably you want to also change the priority of an existing process, for this you must use the command renice

To change the priority of an existing process just do renice -n [nice value] -p [process id]

renice -n 10 -p 1234


Popular Posts:

Flattr this!

  5 Responses to “Be nice with your process on Linux”

  1. An alternative to changing the nice value is to use a daemon called Trickle (http://nwlinux.co/f8). It is really great at prioritizing applications on a more permanent basis. For instance, a user can make SSH first priority on a given box. This configuration is especially useful in a situation where DDoS attacks are prevalent as SSH has priority over Apache – you can login to adjust settings and so on.

  2. The latest Fedora 15 ignores nice command. I`m not sure if this is Fedora`s fault or maybe the recent kernel has a problem. Anyway it is worth mentioning.
    More here: http://forums.fedoraforum.org/showthread.php?t=266839

  3. […] ” “ Linuxaria: Be nice with your process on Linux […]

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

*