Practical examples of using nice, renice and ionice in Linux

Last update: 17/12/2025
Author Isaac
  • Linux It uses priorities (PR) and niceness values ​​(NI) to decide which processes get more CPU time.
  • nice sets the priority when starting a command, while renice modifies it in processes that are already running.
  • ionice controls disk access priority and complements nice/renice when the bottleneck is I/O.
  • Only root It can assign high priorities (negative niceness or real-time classes), while regular users can only lower the priority.

Process priority management in Linux

When a Linux server becomes overloadedThe cause is almost always a few processes that are hogging CPU or disk resources. That's where they come in. nice, renice and ionice, three key tools to decide who really controls the system's resources.

Mastering these tools It allows you to do practical things like launching resource-intensive tasks without slowing everything down, lowering the priority of non-critical processes, or giving absolute priority to a vital service. Let's take a look, with plenty of examples, at how to use these features in the day-to-day administration of Linux systems.

Basic concepts: processes, PIDs, and priorities in Linux

In Linux, absolutely everything that runs It's a process: from the web server to your shell. Each process has a PID (Process ID), a unique numeric identifier that the kernel uses to manage it in the process tableThat table is queried with commands , the ps, top o htop.

Processes don't appear out of nowhere.but one generates the other through the famous sequence fork() + exec()The process that creates the new one is called fatherand the newborn is the hijoThis father-son relationship forms a hierarchical process treewhich you can visualize with tools like ps axjf o pstree.

The Linux kernel is multitaskingBut that doesn't mean a single core runs multiple processes simultaneously. What it does is alternate processes at high speedloading the context of one, running it briefly, saving it, and moving on to the next. To decide whose turn it is, it relies on a priority system.

Each process has an effective priority (PR) and an associated value called niceness (NI)You can see both fields clearly in the output of top o htop, in the columns PR y NI. While The lower the PR valueThe more priority the process has to access the CPU.

The NI niceness value is a "clue" for the planner, which indicates how "kind" a process is to others: the more high is NIThe more willing that process is to cede CPU to others, the higher the CPU usage. In the user range, NI ranges from -20 (very high priority) a 19 (low priority)Being 0 is the default priority for new processes.

Priority ranges in Linux: PR, NI, and real-time

Under the hood, Linux handles 140 priority levels, numbered from 0 to 139. These kernel improvements may affect scheduling; see What's new in Linux kernel 6.18These levels are divided into two main groups: real-time priorities y normal user space priorities.

Real-time priorities occupy the range of 0 to 99They are reserved for processes that cannot afford significant delays (for example, tasks related to hardware critical). In tools such as top o htop These processes usually appear with PR = rt or marked as "RT".

Normal user priorities They range approximately from 100 to 139But in most utilities you'll see a range 0-39 for PR, derived from NI. In this context the following relationship is used:

PR = 20 + NI

That means that if NI = 0The effective priority will be approximately PR = 20which is the average priority. If NI = -20, PR will be very low (0) and the process will have highest relative priorityIf NI = 19, PR will be close to 39, that is, very low priority.

It is important to clearly distinguish between NI and PRNI is the "nice" value that you directly manipulate with nice o renice, while PR is the priority that the kernel ultimately applies, calculated from NI and other internal factors such as process behavior and system load.

View processes and their priorities with ps, top, and htop

Before blindly setting prioritiesIt's important to know what's running on the machine. Linux offers several process monitoring tools for this purpose, each with its own approach.

The command ps shows a "still photo" of the current state of the processes. With the classic combination:

Command: ps aux

You will get a complete list with user, PID, CPU and memory usage, among other fields. If you want to focus on priorities and niceness, you can use something like:

  Filter Unknown Senders Not Engaged on iPhone

Recommended filter: ps -eo pid,ppid,ni,comm

In this case, the column ni It teaches you the value of niceness in each process. The field ppid It reflects the PID of the parent process, which helps you understand the process hierarchy we discussed earlier.

If you need real-time monitoring, top It's the standard Swiss Army knife. With:

Order: top -o %CPU

You will see the processes sorted by CPU usagewith columns like PR (effective priority) and NI (niceness). From the interface itself top you can use the key r to apply a kidneys interactive, first entering the PID and then the new nice value.

htop It's a souped-up version of topwith a more user-friendly and colorful interface. After installing it (for example with sudo apt-get install htop), simply run htop to view the process tree, CPU bars, RAM, and columns like PRI, NI y S (state of the process).

A very convenient advantage of htop It allows you to change the priority from the keyboard: you select a process and use F7 to increase priority (decrease NI) or F8 To lower it (increase NI), provided you have sufficient permissions (especially if you want to go to negative values).

Process states and their relationship to planning

At the exit of top o htop You will see a column S which indicates the process statusThis state also affects how the scheduler decides who to give CPU to at any given time.

The most common states in Linux are the following:

  • R (Running): the process is running on the CPU or ready to do so.
  • S (Interruptible sleep)The process is "asleep" waiting for an event (e.g., data being available) but can be woken up with signals.
  • D (Uninterruptible sleep): uninterrupted wait, normally blocked in an I/O operation with a device; does not respond to signals until that operation is complete.
  • T (Stopped): process stopped, often by a signal such as SIGSTOP o SIGTSTP.
  • Z (Zombie)The process has finished, but its parent has not yet picked up its status; it still appears in the process table.

Changing the niceness of a process It does not change its state (S, R, D, etc.), but it does influence how much CPU time it receives when it is ready to runA process in a state DFor example, it's waiting for I/O; adjusting `nice` there might not help much, and it's better to think about ionize, as discussed below.

Send signals to processes: kill, pkill, and killall

Priority management is usually combined by sending signals to problematic processes. In Linux, signals are special messages that tell a process to do something: terminate, restart, reload configuration, etc.

The most common command for sending signals es kill, which by default sends the signal TARGET TERMA typical use would be:

Example: kill 1234

This requests that the process with PID 1234 be terminated in an orderly fashion. If the process ignores the signal or is blocked, you can force its termination with:

Force: kill -9 1234

The option -9 send SIGKILLThe program cannot intercept this; the kernel kills the process without giving it a chance to clean anything up, so it must be used with caution.

If you prefer to work by process name, you have pkill y killall. For example: uterine

By name: pkill firefox

It will send the default signal (SIGTERM) to all processes whose name matches "firefox". And something like:

All: killall chrome

It will terminate all processes called "chrome". in the system. This is useful when you have multiple instances of the same program and want to close them all at once.

nice: execute commands with a specific priority

The command nice It is used to start processes with a specific niceness value, instead of using the default value of 0. It is the ideal tool when you know in advance that a task will be heavy and you want it not to interfere too much with the rest of the system.

Syntax:
nice -n valor_nice comando
nice valor_nice comando
nice --adjustment=valor_nice comando

If you do not specify any value, nice It usually applies a default NI = 10That is, it lowers the process priority slightly compared to normal. A simple example:

Typical use: nice -n 10 ./script_largo.sh

Here the script It will be launched with NI = 10This means it will have lower priority than processes with NI 0 when competing for CPU. This is a good practice for maintenance tasks or reports that are not urgent.

  Windows 10: How do I open the Amd Catalyst Control Center?

To load a command with positive priority (i.e., lower priority) you can also use the abbreviated form:

Short form: nice 15 comando_pesado

However, if you need to increase the priority (that is, using negative niceness), only the user root is authorized. For example:

With root: sudo nice -n -5 xclock &

This command launches xclock with NI = -5so that it will get more CPU time than other processes with NI >= 0. To express it in the long form, you could do:

Long form: sudo nice --adjustment=-5 xclock &

Also remember that a process launched without nice It inherits the default priority NI = 0, unless there are special configurations through PAM or system limits that modify that behavior for certain users or groups.

renice: change the priority of processes already running

While nice only acts when starting a command, renice allow modify the priority of processes that are already runningIt is the go-to tool when you detect that a live process is consuming too many resources or, conversely, you want to favor it.

The generic way to use renice is

Format: renice prioridad pids] pgrps] usuarios]

Where the first argument is the new value of niceness, and then you can indicate if you want to act on specific PIDs (-p), process groups (-g) or all processes of certain users (-u). For example, to change the priority of the process with PID 324 to NI = 10:

Caso: renice -n 10 -p 324

This command adjusts the niceness value. of process 324, usually showing you the old and new priority. If you wanted to change the priority of all the processes of a userYou could do something like:

With privileges: sudo renice -n -10 -u gacanepa

Here are all the processes for the user gacanepa They would then have NI = -10, provided we execute the command as root. Normal users can only increase the NI value (that is, lower priority) in their own processes, never make it more negative.

You can also change multiple targets at once., combining PIDs and users. For example:

Multiple objectives: renice 3 -p 12122 31313 -u Antonio Maria

In this case, NI is set = 3 This applies to processes with PIDs 12122 and 31313, and to all processes belonging to users Antonio and Maria. It is useful when you want to standardize the priority of a set of related jobs.

ionice: prioritize disk input/output

Many bottlenecks are not in the CPUbut in the subsystem of storageA process can have low CPU usage but saturate the disk with intensive read/write operations. In these cases, Nice and Renice are not enoughYou need ionize.

Although the original content mainly mentions CPUionice complements this control by allowing you to set the I/O priority of a process depending on the planning class of the I/O scheduler.

I/O Syntax: ionice -c clase -n nivel comando

The most common classes are:

  • 1 (real-time)Real-time I/O priority should almost never be used except in very controlled cases.
  • 2 (best-effort): default class, with levels from 0 (highest priority) to 7 (lowest priority).
  • 3 (idle)The process only performs I/O when the disk is idle.

For example, to run a backup If you don't want it to bother the rest of the system, you could do:

Backup: ionice -c 3 tar czf backup.tgz /datos &

In this way, the tar process will be programmed only when there is no other disk activity, greatly reducing the noticeable impact on other services. Combined with niceYou can make a heavy task almost "invisible" to users.

Support commands for listing, searching, and controlling processes

Adjusting priorities is only one part of process management. Before deciding what to touch, you need to locate and understand what's happening on the machine. Linux offers several very useful utilities for this.

In addition to ps, top y htopAs already mentioned, it is advisable to become familiar with:

  • pgrepwhich searches for processes by name and returns their PIDs. For example: pgrep apache It will show all active Apache server processes.
  • pstree, which presents the processes as a hierarchical tree, making it easier to see who is the parent of whom.
  • lsofwhich lists the files opened by a specific process: lsof -p PID It helps detect which ports or files are being used.
  • fuser, ideal for knowing which process is using a specific file or port: fuser -v -n tcp 8080 It tells you who occupies port 8080.
  Instagram Stories: How can I change the length of the music?

For advanced debugging You have tools such as strace, which traces the system calls of a real-time process using, for example, strace -p PIDVery useful when a process gets "stuck" and you want to know which syscall it's stuck on.

Adjust background processes: bg, fg, and nohup

The way you launch a process It also influences how you handle it afterward. From the shell, you can send it to the background and continue using the terminalor keep it in the foreground as you see fit.

If you start a command and press Ctrl+ZIt suspends and returns to the prompt. With the command bg You can resume it in the background so that it continues running without blocking the terminal, whereas with fg You bring it back to the forefront.

When you want a process to survive upon closing the session or the terminal, nohup He is your ally. For example:

Unlinked use: nohup script_largo.sh &

It will execute the script independently of the terminal.redirecting standard output to nohup.out (by default) and allowing it to continue running even if you close the connection SSH.

System service management and its relationship to priorities

Background services (daemons) Web servers, databases, and task queues are, after all, processes. To manage them in modern systems with systemdyou will almost always use systemctl:

systemctl start apache2

This command starts the apache2 serviceWhile systemctl stop, restart o status They control their life cycle. In older systems based on SysVinitThe command still exists service:

service apache2 restart

Although you don't usually change the niceness directly from systemctlYes, you can define CPU and I/O limits and parameters in systemd unit files (for example, using Nice= or cgroup control), achieving a similar effect to the use of nice/renice but in a persistent and declarative way.

General system monitoring: CPU, memory, and I/O

To decide what to prioritize with nice, renice, and ioniceSimply looking at a list of processes isn't enough; you need a broader understanding of the machine's state. Several very useful tools can help you with this.

The command uptime summarizes it at a glance There which time the system has been running, the number of connected users, and the medium load in the last 1, 5 and 15 minutes:

Summary: uptime

The average load is a clear clue This indicates whether you have too many processes competing for CPU time. Consistently high values, compared to the number of cores, suggest that it might be a good idea to lower the priority of non-critical tasks.

To view detailed performance statistics you have vmstat y iostat. For example: uterine

Sampling: vmstat 5

Displays every 5 seconds CPU usage, memory, running and waiting processes, among others. And with:

I/O Statistics: iostat

You will get information about disk I/OWaiting times and throughput are crucial for deciding when it makes sense to pull ionice in addition to nice/renice.

Configure default niceness for users and system limits

If you want to go a step further And to set default priorities for certain users or groups, you can do so by PAM and the archive /etc/security/limits.conf.

The basic syntax in limits.conf is

Template: <dominio> <tipo> <elemento> <valor>

Where the domain can be a user, a group or a wildcard, the type specifies whether the limit is "hard" or "soft" and the element can be, among others, the nice value by default. This way you can make certain users always start their processes with a specific NI without having to explicitly use nice.

Instead of directly modifying limits.confIt is also possible to create additional files in /etc/security/limits.d/These are loaded in alphabetical order and can overwrite the overall configuration. This approach is cleaner and easier to maintain.

When working with nice, renice, and ioniceDon't forget that you can always consult your manual with:

Manuals:
man nice
man renice
man ionice

To fully understand how Linux schedules processes and be able to confidently use commands such as nice, renice e ionice It gives you fine control over CPU and disk, allowing you to keep the system agile even under heavy load, prioritize critical services and relegate heavy tasks that can wait to the background without bothering anyone.

What's new in Linux 6.18
Related article:
Linux 6.18: all the new features of the new kernel