How to Use Nice and Renice Command in Linux?

Learn via video courses
Topics Covered

How to Use Nice and Renice Command in Linux?

There are hundreds of processes running in a system at any given time, most of which are created by the Linux operating system and some of which are created by the user who is currently signed in. The system assigns a priority to each operating process, which governs how quickly it is executed. Processes with a higher priority are typically completed earlier than those with a lower priority.

The nice & renice commands in Linux are used to adjust a process' priority, which in turn affects how quickly it is performed in the system.

In Linux, the Nice command is used to prioritize the execution of processes. It allows users to control the priority levels of processes, determining how much system resources a particular process should receive. By assigning a "niceness" value to a process, users can influence its priority and resource allocation. The process is given more priority the lower the pleasant value is.

Learning About the Use of Kernel Scheduler

A kernel scheduler is a component of the kernel which divides up the available execution time among the processes that a system can run and chooses the best process out of all of them to execute next. Any process that is ready to be executed and is only awaiting CPU time is said to be runnable.

The scheduler, which uses a scheduling( which is based on a priority) mechanism to select between processes that a system can run, is the central component of Linux multitasking. It ranks processes according to who deserves more CPU time and who needs it the most.

Nice Value and Process Priority Understanding

A lot of details about the processes are stored in the kernel, including information about a process's scheduling priority, or simply "process priority". While activities with the same priority are repeatedly scheduled one after the other, higher-priority processes will be carried out before lower-priority ones.

Linux implements two different priority ranges and a total of 140 priorities. In this tutorial, we'll discover that the niceness value, the first one, ranges from -20 (the greatest priority value) to 19 (the lowest priority value). The real-time priority is the other; it has a default range of 1 to 99 and a user-space range of 100 to 139.

Working with Nice Command in Linux

How Can You Check a Running Process's Nice Value?

To check the nice value of a running process, you can make use of the "ps" command with the "-l" option. Open a terminal and execute the following command:

Replace <process_id> with the actual ID of the process you want to check. The nice value will be displayed under the "NI" column.

value under ni column

To check the nice value of processes, you may also use the top command.

nice value of running process

For processes launched by an ordinary user, the niceness value by default is 0. In fact, by running the nice command without any inputs, you can quickly get the nice value for your terminal or shell.

default niceness value

How to Set the Priority of a Process?

To set the priority of a process, you can use the nice command in linux along with the desired nice value. The normal range of good values is between -20 (highest priority) and +19 (lowest priority). The following is the syntax for setting the priority:

set priority of process

Replace <nice_value> with the desired priority level and <command> with the actual command or process you want to execute with that priority. For example, to launch the myprocess command with a nice value of 10, you would run:

The process will start with the specified priority.

Working with Renice Command in Linux

How Can You Change a Running Process Priority?

The Renice command can be used to alter a process's priority while it is already running. Open a terminal and execute the following command:

Replace <nice_value> with the new priority level you want to assign and <process_id> with the ID of the running process you wish to modify. For example, to increase the priority of a process with ID 1234 to 5, you would run:

change running process priority

The process's priority will be adjusted accordingly.

How to Change the Priority of All Programs of a Specific Group and User?

To change the priority of all programs associated with a particular group and user, you can use the Renice command with additional options. Open a terminal and execute the following command:

Replace <nice_value> with the new priority level, <group_id> with the ID of the group, and <user_id> with the ID of the user whose processes you want to modify. For example, to decrease the priority of all processes associated with group "staff" and user "john" to 15, you would run:

change priority of all progrma of specific user

All processes belonging to the specified group and users will have their priorities adjusted accordingly.

So basically, the Nice and Renice commands in Linux provide users with the ability to control the priority and resource allocation of processes. By understanding how to check, set, and change the priority levels using these commands, users can optimize system performance and manage resource allocation efficiently.

Conclusion

To summarize, here are the key points about using the Nice and Renice commands in Linux:

  • Using Nice Command:
    • The Nice command in linux allows users to prioritize process execution.
    • The nice value determines the priority of a process, with lower values indicating higher priority.
    • To check the nice value of a running process, use the ps -l <process_id> command.
    • Set the priority of a process by using the nice -n <nice_value> <command> syntax.
  • Using Renice Command:
    • The Renice command modifies the priority of a running process.
    • Use the renice <nice_value> -p <process_id> command to change the priority of a specific process.
    • To change the priority of all programs belonging to a specific group and user, use the renice <nice_value> -g <group_id> -u <user_id> command.

In conclusion, the Nice and Renice commands in Linux provide users with control over process priority and resource allocation. By utilizing these commands, users can optimize system performance and manage resources efficiently.