Priority of process in Linux | nice value
Last Updated :
14 Oct, 2024
In Linux, every running program is considered a process. Each process requires space in RAM and CPU time to execute. Processes have assigned priorities, which dictate the order in which they are executed by the CPU. This priority is managed using the nice value, a parameter that determines how much CPU time a process gets. we will discuss how the nice and renice commands are used to manage process priorities. The running instance of the program is process, and each process needs space in RAM and CPU time to be executed, each process has its priority in which it is executed. Now observe the below image and see the column.
nice and renice Commands
The nice command is used to start a process with a specified nice value, while the renice command is used to alter priority of the running process.
Nice Command Syntax
nice [OPTION] [COMMAND [ARG]...]
where,
- OPTION: Set the nice value (e.g., -n).
- COMMAND: Command to be executed with the specified priority.
- ARG: Arguments passed to the command.
Renice Command Syntax
renice [PRIORITY] [-p PID]
where,
- PRIORITY: The new priority to be assigned to a process.
- -p PID: The Process ID (PID) of the process you want to modify.
top command output The column NI represents nice value of a process. It's value ranges from -20 to 20(on most unix like operating systems).
-20:- most priority Process
20: - least priority Process
One important thing to note is nice value only controls CPU time assigned to process and not utilization of memory and I/O devices.
Common Options Used with the Commands
Option | Description |
---|
-n | Set the nice value for a new process. |
---|
-p | Modify the nice value for a process using its PID. |
---|
Usage of nice command
Now let's assume the case that system has only 1GB of RAM and it's working really slow, i.e. programs running on it(processes) are not responding quickly, in that case if you want to kill some of the processes, you need to start a terminal, if you start your bash shell normally, it will also produce lag but you can avoid this by starting the bash shell with high priority.
For example:
nice -n -5 bash
First observe output of top without setting nice value of any process in below image:
nice value of top is 0Now start a bash shell with nice value -5, if you see the highlighted line, the top command which is running on bash shell has nice value set to -5
nice value of bash shell is -5Usage of renice command
To alter priority of running process, we use renice command.
renice value PID
value is new priority to be assigned PID is PID of process whose priority is to be changed One thing to note is you can't set high priority to any process without having root permissions though any normal user can set high priority to low priority of a process. We will see one example of how you alter priority of process.
nice value of gnome terminal is 0You can observe that nice value of process(PID = 2371) is 0, now let's try to set the new priority of 5 to this process.
renice 5 2371
Output:
2371 (process ID) old priority 0, new priority 5
You can also see this priority using top command(see highlighted line in image).
process 2371 has nice value 5Conclusion
The nice and renice commands are useful tools for managing process priorities in Linux. By adjusting the nice values, you can control how much CPU time is allocated to processes, which helps in managing system performance. However, keep in mind that normal users can only decrease a process's priority, while increasing the priority (i.e., setting a negative nice value) requires root privileges.
Similar Reads
Linux Commands Cheat Sheet Linux, often associated with being a complex operating system primarily used by developers, may not necessarily fit that description entirely. While it can initially appear challenging for beginners, once you immerse yourself in the Linux world, you may find it difficult to return to your previous W
13 min read
Linux/Unix Tutorial Linux is one of the most widely used open-source operating systems. It's fast, secure, stable, and powers everything from smartphones and servers to cloud platforms and IoT devices. Linux is especially popular among developers, system administrators, and DevOps professionals.Linux is:A Unix-like OS
10 min read
25 Basic Linux Commands For Beginners [2025] While performing a task, we all need shortcuts. Shortcuts help us to complete a task quickly. Linux comes with such commands which are one to two words, using that commands, you can perform several operations in no time. As a beginner, you must be aware of those basic Linux commands to complete an o
13 min read
grep command in Unix/Linux The grep command is one of the most useful tools in Linux and Unix systems. It is used to search for specific words, phrases, or patterns inside text files, and shows the matching lines on your screen. Syntax of grep Command in Unix/LinuxThe basic syntax of the `grep` command is as follows:grep [opt
6 min read
Sed Command in Linux/Unix With Examples The SED command (short for Stream Editor) is one of the most powerful tools for text processing in Linux and Unix systems. It's commonly used for tasks like search and replace, text transformation, and stream editing.With SED, you can manipulate text files without opening them in an editor. This mak
8 min read
AWK command in Unix/Linux with examples Awk is a scripting language used for manipulating data and generating reports. The awk command programming language requires no compiling and allows the user to use variables, numeric functions, string functions, and logical operators. Awk is a utility that enables a programmer to write tiny but eff
8 min read
Introduction to Linux Shell and Shell Scripting If we are using any major operating system, we are indirectly interacting with the shell. While running Ubuntu, Linux Mint, or any other Linux distribution, we are interacting with the shell by using the terminal. In this article we will discuss Linux shells and shell scripting so before understandi
8 min read
How to Find a File in Linux | Find Command The find command in Linux is used to search for files and directories based on name, type, size, date, or other conditions. It scans the specified directory and its sub directories to locate files matching the given criteria.find command uses are:Search based on modification time (e.g., files edited
9 min read
ZIP command in Linux with examples In Linux, the zip command compresses one or more files or directories into a single.zip archive file. This saves disk space, keeps data organized, and makes it simple to share or backup files. It's among the most used compression utilities, particularly when sharing large files via email or storing
6 min read
What is Linux Operating System The Linux Operating System is a type of operating system that is similar to Unix, and it is built upon the Linux Kernel. The Linux Kernel is like the brain of the operating system because it manages how the computer interacts with its hardware and resources. It makes sure everything works smoothly a
13 min read