On 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 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.
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.
# 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.
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
To change the priority of an existing process just do
renice -n [nice value] -p [process id]
renice -n 10 -p 1234