renice Command in Linux



In Linux, it's crucial to keep your system running smoothly, especially when multiple processes are running simultaneously. This can be challenging when some tasks require more resources than others. One useful tool for managing this is the renice command.

With renice, you can change the priority of running processes. This practice makes sure that important tasks receive the resources they need without interruptions. Moreover, it guarantees that critical processes run efficiently, while less important tasks can take a backseat. Adjusting the process priorities enables you to maintain overall system performance and stability, even when the system is under heavy load.

Table of Contents

Here is a comprehensive guide to the options available with the renice command −

What is renice Command in Linux?

The renice command in Linux lets you change the priority of a running process. Priority controls how much CPU time a process gets compared to others. Linux uses a "nice value" to set this priority, ranging from -20 (highest priority) to 19 (lowest priority). By using renice, you can adjust a process's nice value to influence how much CPU time it gets.

This is especially helpful in multi-user or multi-tasking environments, where some processes need more resources than others. For example, you can give a higher priority to a database server while lowering the priority of a background task to keep your system responsive.

Syntax of renice Command

The basic syntax for using the renice command is as follows −

renice [OPTION] PRIORITY [PID...]

Here, PRIORITY represents the new priority (nice value) you want to assign to the process. It can be a positive or negative integer. PID is the Process ID you want to renice. If no PID is specified, renice changes the priority of processes based on the user.

renice Command Options

The renice command offers several options that let you easily adjust the priority of processes. Here are some of the most commonly used ones −

OptionDescription
-n prioritySet the priority for a process, either absolutely or relatively, depending on your system’s settings. This option is optional but must come first if used. Refer to the NOTES section for more details.
--priority prioritySet a specific priority level for the process. This is the default behavior when no other option is provided.
--relative priorityAdjust the priority by a certain amount, either increasing or decreasing it, relative to the current value.
-g, --pgrpUse the following arguments as process group IDs.
-p, --pidSpecifies the PID(s) of the process(es) you want to renice.
-u, --userRenices all processes running under a specific user.
-h, --helpDisplays the help message with information about the command options.
-V, --versionDisplay the version information of the renice command.

You can learn more about the command usage by accessing its manual page −

man renice
renice Command in Linux1

How to Install renice Command in Linux?

The renice command is part of the procps package, which is pre-installed on most modern Linux distributions. However, if for some reason it's not installed, you can install it manually using the following commands −

sudo apt install procps #for Debian/Ubuntu
sudo yum install procps-ng #for CentOS/Fedora

You can confirm the availability of the renice command on your system by checking its version −

renice --version
renice Command in Linux2

How to Use renice Command in Linux?

Using the renice command in Linux is simple. Let's consider a few common use cases to learn how it works on Linux −

How to Renice a Specific Process by PID?

To change the priority of a specific process, run the renice command with the following syntax −

renice [new_priority] -p [pid]

For example, the following command increases the priority of the process with PID 957 −

sudo renice -10 -p 957

The output confirms that the old priority of process 957 has been updated from 0 to -10 −

renice Command in Linux3

How to Renice Processes by User?

You can use the following command to adjust the priority for all processes belonging to a specific user −

renice [new_priority] -u [username]

For example, the following command lowers the priority of all processes running under the user “linuxuser” −

sudo renice 10 -u john

How to Renice Processes by Process Group?

You can also renice all processes belonging to a specific process group. For this purpose, you can use the renice command with the -g option −

renice [new_priority] -g [pgid]

For example, use the following command to change the priority of processes in the group with PID 1000 −

sudo renice -5 -g 1000

It changes the priority of all processes in group 1000 to -5, which means they will get higher priority for CPU time.

Difference between renice and nice Command in Linux

The nice and renice commands in Linux both deal with process priorities, but they work in different ways −

CommandPurposeWhen to UseExample
niceStarts a new process with a specified priority.Use when launching a new process and you want to set its priority.The “nice -n 10” command starts a process with lower priority.
reniceChanges the priority of an already running process.Use when you need to adjust the priority of a process that’s already running.The “renice -n 5 -p 321” command changes the priority of the process with PID 321

In short, nice is used when launching a new process, while renice is used to adjust the priority of a running process.

Best Practices to Use the renice Command

The renice command can be really useful, but it’s important to use it carefully to avoid impacting your system’s performance.

Here are a few tips to keep in mind −

  • Don’t overuse high priorities, as giving too many processes high priority can slow down your system. Only assign high priority to critical tasks.
  • Changing the priority of system processes, especially those tied to the kernel or essential services can make your system unstable, so avoid renicing system processes unless absolutely necessary.
  • After adjusting process priorities, regularly check your system’s performance. Use tools like top, htop, or ps to ensure everything is running smoothly.
  • If you need to renice multiple processes or automate the process, consider adding renice commands to your shell scripts, but be sure to manage the order in which you adjust priorities.

Conclusion

The renice command is a powerful tool in Linux for managing process priorities. It enables you to ensure that important tasks receive the resources they need while less critical tasks take a backseat.

By adjusting the "nice value", you can optimize CPU time allocation, helping maintain system responsiveness and stability, especially in resource-intensive environments. However, it’s essential to use renice with caution as overusing high priorities or modifying system processes can lead to instability.

By following the best practices and regularly monitoring system performance, you can avail the full potential of renice without disrupting your system's efficiency.