Skip to main content

Process Handling

Using nice and renice Commands to Change Process Priority in Linux

You can modify if a certain process should get priority in consuming CPU with nice and renice commands.

Every single process running in a Linux machine has a priority for its execution, which can be translated into how much CPU power will be given to that process to execute.

Of course, in Linux you can control as much as you want, so, you can also control how much priority something must have in the system when running it.

How to check the niceness or priority of a process?

If you run the ps command, you can see what processes are currently running on your Linux system, and what priority they currently have:

ps command listing processes and showing priorities

Now, if you want to run something with more priority (whereas priority would be more CPU power), you can then use the 'nice' command to run it and control how nice this particular process is to the rest of the system.

💡
The prioritization of niceness of this command goes from -20 until 19, where -20 means highest priority and in consequence, as much CPU power as possible, until 19 which is the lowest value possible and therefore the less CPU power assignation.

How to use nice command?

To use the nice command, you have to be root or have sudo capabilities in the system, as this is a very delicate command.

Assuming you are root, you would then type:

nice -n 12 <command>

This will tell the system that you want to run the <command>, using a priority of 12 (10 is the default if you don't enter any value). Basically, it will be low in the list of priorities and therefore will not suck all CPU power.

What is it useful for?

Imagine you have a process, that performs several calculations and can take several seconds or even minutes to run. The more power assigned to that process, the faster it will run and finish. This is where this command can become useful although dangerous.

If you have a script that runs numerous processes, in a shared environment or production server, running something with a higher priority might make the server unresponsive or faulty. It's because the CPU will be dedicated almost exclusively to running whatever process you have called with a high priority (or negative value), hence, it will become unresponsive or even kill the server, if your script has an infinite loop, for example.

How to change a priority on a running process with renice command?

Let's say you started running a process with the nice command, but after some time you want to change it, although you need the process to keep running, not kill it or restart it. This is where your command 'renice' comes in.

The 'renice' command can be used in a very similar way and using the same concept as the nice command, however, it is intended to change the priority that you have already called nicely 😄

So, if you are already running a command with nice, you can modify its priority with renice in the following fashion (again, running it as root user):

renice -n -10 <command>

This will change the previous priority of the process to become more important, therefore with more power now (-10).

You can even use it for all process which belong to a specific user, for example:

renice -n -10 -u helder

This will change the priority of all processes which belong to the user helder.

Similarly, you can use options such as -p and specify the Process ID (or pid) or -g and use the group id of processes.

Conclusion

You can see how Linux allows to control everything as possible on your system, and allows you to be able to use this in your favor.

Having the capability to assign and reassign priorities to your processes, gives us a lot of power to be able to control how our system manages processes, how efficient and also what to prioritize to be able to comply any necessity you might have.

Helder
@heldmar Montevideo, UY