Modern servers are designed to be highly versatile — often hosting multiple applications and handling an extensive range of other critical operations. However, as running processes demand ever-increasing amounts of server resources, system administrators frequently face the challenges of reduced server performance and increased latency. With hundreds of processes running on a production server at any given time, the importance of effective process management cannot be overstated.
In this article, we explore the concepts of process prioritization and examine process scheduling. You will learn how to use the Linux nice and renice commands to manage server resources by assigning and adjusting process priorities.
Key points
As we explore use of the nice and renice Linux commands, we will address following themes regarding their use:
- Overview of the nice and renice commands in Linux
- About process scheduling and prioritization
- A review of process priorities in Linux
- Determining the priorities of running processes
- A thorough look at the Linux nice and renice commands
- Using Linux nice command to launch processes with altered priorities
- Boosting your server’s performance with Liquid Web as your hosting provider
Process scheduling and prioritization at the core of process management
Each task or operation running on a server is represented by a process. A process is an instance of a running program, identified by its own unique number and allocated its own namespace and system resources. All running processes are managed by the operating system, which is responsible for scheduling, resource allocation, and tracking process states.
On a typical Linux server, there can be hundreds of processes running at any given time. These includes critical system processes that ensure smooth operation and user processes that serve various functions. In the context of limited system resources, effective process management becomes essential to ensure that all processes receive enough memory and CPU time.
Linux uses a combination of scheduling and prioritization to run multiple processes, creating the appearance of multitasking. As part of the operating system, the process scheduler controls the execution of all system and user processes, assigning and adjusting process priorities and allocating resources.
The scheduler typically divides CPU time into small units and allocates them to processes based on their priority. This approach allows each process to run for a brief period of time before switching to the next, preventing any single process from monopolizing the CPU.
All running processes are assigned priority levels based on their resource requirements and the current system load. Processes with higher priorities consume more CPU time compared to lower-priority processes. This prioritization ensures critical operations receive the necessary resources to execute, even when the system is overwhelmed with requests.
While process priorities are typically assigned when a process is scheduled for execution, Linux also employs dynamic priority allocation. This feature allows the operating system to adjust process priorities in real time based on the process behavior and system load, optimizing resource distribution and maintaining system performance.
Process priorities in Linux
Linux process priorities are managed through the concept of “niceness,” which determines the scheduling priority of processes. Each process is allocated a Linux nice value, which is then used to determine the process’s actual priority upon execution.
The Linux nice values range from -20 to 19, where -20 represents the highest Linux process priority, and 19 represents the lowest. Essentially, the nice value determines how “nice” a process is to other users, which is why a higher number represents a lower priority. A higher “niceness” means the process is “nicer” to other processes, allowing them to receive more CPU time. A lower “niceness” indicates a higher priority, giving the process more system resources.
Typically, processes start with a Linux nice value of 0, representing the standard — or neutral — priority. Higher priority processes receive negative nice values, while lower priority processes are assigned positive nice values. The ‘niceness’ of a process can be dynamically adjusted to accommodate changing system demands.
Although the Linux nice value significantly influences the scheduling priority, it does not represent the actual priority of a process. It serves as a factor in a more complex algorithm employed by the Linux scheduler to calculate and maintain Linux process priority levels dynamically. However, as the nice Linux value plays a critical role in determining the priority of a process, it is typically representative of it.
Determining the priorities of running processes
You can use the Linux ps command (Linux process command) to determine the priority of a running process and its Linux nice value. Using the -l flag to display the long format, the ps command will print a list of running processes along with their Linux nice and priority levels represented by the NI and PRI values, respectively. You can also use grep to specify the name of the process you are looking for:
# ps -axl | grep process_nameIn the Linux process list below, each process has a priority (PRI) of 20, and a Linux nice (NI) value of 0, which is the standard niceness of most processes. The priority (PRI) value reported by the ps command is determined by various aspects, including the Linux nice value of the process, its state, and the current system load. The priority value is not static and can change dynamically depending on the scheduling policies and other factors:
PID PPID PRI NI VSZ RSS WCHAN STAT TTY TIME COMMAND
1430542 7205 20 0 27148 6948 - S ? 0:01 dovecot/pop3-login
1430543 7205 20 0 27164 6908 - S ? 0:00 dovecot/imap-login
1430544 7205 20 0 16268 5032 do_epo S ? 0:00 dovecot/config
1430545 7205 20 0 14228 3336 do_epo S ? 0:00 dovecot/statsYou can use the –sort option to sort through the Linux process list displayed by the ps command. Using –sort=-nice allows you to sort the output by the “niceness” NI column in descending order, with processes with the highest Linux nice value displayed first.
Sorting the list of processes by the percentage of CPU utilization using the –sort=-pcpu option allows you to see which processes consume the most CPU time at a particular moment. Those processes typically have higher nice Linux values.
Other Linux process commands and monitoring utilities, such as top, atop, and htop, can also be used to determine the Linux nice value and priority level of the system and user processes. These tools provide a dynamic, real-time view of running processes and their resource utilization levels — and are typically used to investigate the load on a Linux server.
Linux nice and renice commands
Linux nice and renice commands are used to launch processes with altered priorities and adjust the Linux nice values of running processes. The nice command can be issued to start a process with a priority level you specify, while renice modifies the priority of a process that is already running.
Nowadays, manually adjusting Linux process priorities is something system administrators will find themselves doing less than they used to. Modern servers are considerably more powerful and not as near resource starved as they used to be. Moreover, high availability environments leverage load balancing and provide other performance benefits. However, companies employing machine learning (ML) and artificial intelligence (AI) and other resource-intensive technologies may still need to fine-tune process management to ensure optimal performance.
The Linux nice and renice commands give you full reign when it comes to ensuring that critical processes are running with an adequate level of prioritization. Using nice and renice to alter process priorities provides an effective way to prevent system halts and avoid unresponsive processes, providing a proactive solution before resorting to killing processes on Linux with kill and killall commands.
Using Linux renice command to adjust process priorities of running processes
You can use the Linux renice command to change the Linux process priority of a running process by modifying its Linux nice value. Increasing or decreasing the nice Linux value of a process will influence its CPU time allocation relative to other processes.
Basic syntax of the Linux renice command
The Linux renice command uses the following basic syntax:
renice [nice value] -p [process ID (PID)]The renice command takes the new nice value as an argument and requires you to specify the process ID (PID) of the process that will run with an altered priority level. The new Linux nice value can range from -20 (highest priority) to +19 (lowest priority). If the newly specified nice value is above zero, you can omit the + sign.
Note that the -n flag isn’t explicitly necessary with the renice command and therefore was not included here, since it is optional, and it may be confusing.
If you would like to increase or decrease the Linux nice value of a process by a relative amount, use the –relative option. The “niceness” of the process is incremented or decremented by the given value:
renice --relative [relative amount] -p [process ID (PID)]Changing the Linux nice value of a running process
Step #1. Obtain the process ID (PID)
First, you must identify the process ID (PID) of the process you wish to modify. You can obtain the PID using the ps, top, or pgrep commands. The following commands will list all running processes with the name you specify as process_name:
# ps faux | grep process_name
# pgrep -a process_nameIn the example below, we are listing MariaDB processes using the pgrep command:
# pgrep -a mariadb
15601 /usr/sbin/mariadbdStep #2. Adjust the Linux nice value of the process
Once you have the process ID, you can modify the Linux nice value of the process using the renice command. In the example below, we are lowering the priority of the process by increasing its Linux nice value to 10. You can specify multiple processes by providing multiple PIDs:
# renice 10 -p 1340300
1340300 (process ID) old priority 0, new priority 10Please note that you can only increase the Linux nice value of a running process, thereby lowering its priority, if the user you are logged in as owns the process or you are logged in as the root user. If you attempt to use the renice command to raise the priority of a running process, you will not be able to do so unless you are logged in as the root user or an administrative user with sudo privileges. An unprivileged user will receive a Permission denied error trying to lower the Linux nice value of a process, even if they own it:
# renice -10 -p 1340300
renice: failed to set priority for 1340300 (process ID): Permission deniedStep #3. Verify the updated priority level of the process
Once you have assigned the new Linux nice value to the process, you can verify its updated priority level using the ps command:
# ps -o pid,user,pri,ni,pcpu,cmd -p 1340300
PID USER PRI NI %CPU CMD
1340300 admin 9 10 0.0 vimThe –o flag defines the output format to include the process ID (PID), Linux nice value (ni), priority (pri), CPU utilization (pcpu), and the command (cmd) of the process specified using the -p flag, which accepts the process ID.
As you can see, after the Linux nice value has been adjusted, both niceness (NI) and priority level (PRI) have been modified. The Linux operating system has recalculated the priority level for the process after its nice Linux value was modified.
VPS hosting that’s economical
Mimicking the benefits of a dedicated server —enhanced security, scalability, and speed at a value
Changing the nice Linux nice value of all processes owned by a user
You can use renice to adjust process priorities for all processes owned by a particular Linux user with the help of the -u or –user option. The -u flag accepts both user IDs (UIDs) and usernames.
In the example below, all processes owned by the admin user with the UID 1002 get their Linux nice values increased to 10:
# renice 10 -u admin
# renice 10 -u 1002Using Linux nice command to launch processes with altered priorities
The Linux nice command allows you to launch processes with altered priorities, which affects process scheduling. Launched with an altered Linux nice value, a process receives a priority level that differs from what it would have received by default.
The Linux nice command uses the following basic syntax:
nice -n [nice value] [command/process]The -n flag is used to specify the Linux nice value ranging from -20 for most favorable scheduling to +19 for least favorable scheduling.
When launching a process with a positive Linux nice value, the + sign may be omitted. This is because for positive values only, the plus symbol is optional. For example, the following commands are treated the same by the system:
-n +10 report-generatornice -n 10 report-generatorFollowing the Linux nice value is the actual command you wish to launch with all the arguments if applicable. Once a process is launched with a custom priority level, you can alter it using the renice command if the need arises.
In the example below, we are launching a custom program (report-generator) used to generate a report that needs to be delivered as soon as possible. In order for the process to receive a higher priority, we use the Linux nice command specifying the nice value of -10.
# nice -n -10 report-generatorIf at any point server load averages begin to crawl up due to our program consuming a considerable amount of CPU time, we can use renice to increase the Linux nice value, lowering the priority of the process.
Boost your server’s performance with Liquid Web
Process scheduling and prioritization are fundamental concepts in modern operating systems, crucial for managing the way processes access and use system resources. Effective process management ensures that each process gets a fair share of system resources, thereby optimizing overall system performance and preventing any potential bottlenecks.
On Linux systems, process priorities are managed through the concept of “niceness,” which determines how favorably a process is treated in terms of CPU time allocation. A process with a low Linux nice value receives more CPU time, while a higher nice Linux value represents less favorable allocation of CPU time.
The Linux nice and renice commands are designed to alter Linux process priorities by assigning a process a custom nice value at startup or modifying the Linux nice value of a running process. The ability to adjust process priorities is an invaluable tool in ensuring system stability and maintaining optimal server performance.
Liquid Web delivers unrivaled performance, so you don’t have to compromise on speed. Whether you’re running mission-critical applications or managing a small website, Liquid Web hosting solutions are designed to meet the ever-growing requirements of your business. From GPU servers to bare metal hosting, you’re sure to find what you need.
Luke Cavanagh