Skip to content
System

Nice under Linux: calming a resource-intensive process without abruptly stopping it

Avatar illustré de Julien Macé 3 min read
Tux ajuste la priorité d’un processus Linux gourmand sur un serveur
Nice et renice permettent de baisser la priorité d’un processus sans l’arrêter brutalement.

When a Linux process starts consuming too much CPU, the first reaction should not always be killIf the service is actually working, abruptly shutting it down can cause a task to break, a backup to corrupt, or an ongoing process to be interrupted. In this case, nice And renice allow you to lower its priority without stopping it.

Note: These commands don’t magically reduce the load. They change the CPU priority seen by the Linux scheduler. This is useful for giving the server some breathing room, but if an application loops or blocks a database, you’ll still need to read the logs and understand the cause.

Tux adjusts the priority of a resource-intensive Linux process on a server
Nice and renice allow you to lower the priority of a process without abruptly stopping it.

Understand the nice value before modifying a process

Under Linux the value nice generally goes from minus twenty has nineteenThe lower the value, the higher the priority of the process. The higher it is, the more room it leaves for other tasks.

  • minus twenty : very high priority, to be reserved for controlled cases;
  • zero : default priority of many processes;
  • ten Or fifteen : lower priority, practical for a long, non-urgent task;
  • nineteen : very low priority.

A typical user can usually lower the priority of their own processes. Raising the priority of a process requires root privileges. This is intentional: setting a priority too high on a shared server can disrupt the rest of the system.

Run a command with a lower priority

The order nice It is used when you launch a program. Simple example:

nice -n 10 command

For heavy compression, this can result in:

nice -n 10 tar -czf backup.tar.gz /srv/data

The task is always running, but it avoids taking over other processes too aggressively. I mainly use it for background processing: compression, conversion, scanning, custom scripts, or long operations that don’t need to finish within a minute.

If you run the command from a cron script or an SSH terminal, still keep an eye on the load. A lower priority is no substitute for minimal monitoring.

View the current priority with ps, top, or htop

Before changing anything, identify the process involved. An output p.s. targeted is often enough:

ps -eo pid,ni,pri,pcpu,pmem,comm --sort=-pcpu | head

The column NEITHER displays the value “nice”. The column PRI gives a priority calculated by the kernel. For quick live reading, you can also open:

top

Or use htop to read the server loadIn htop, sorting by CPU and then checking the PID already prevents the user from making a lot of bad decisions.

Modify a process already launched with renice

If the process is already running, nice is no longer sufficient. We must use reniceFirst, retrieve the PID, then lower the priority:

renice +10 -p 1234

Here, one thousand two hundred thirty-four must be replaced with the actual PID. On a process that belongs to another user, or if you want to increase its priority, go through sudo :

sudo renice -5 -p 1234

I advise against setting negative values ​​without a specific reason. On a production server, this is rarely the best first response. Lowering the priority of a resource-intensive process is often healthier than giving undue CPU access to a poorly diagnosed service.

Verify that the change has been applied.

After a renice, reread the value NEITHER :

ps -o pid,ni,pri,pcpu,pmem,comm -p 1234

You can also look at the overall load:

uptime

If the server remains slow, don’t conclude too quickly that renice is not working. The bottleneck could be the disk, memory, an application lock, or a service that is restarting in a loop. In this case, check the logs with journalctl and application logs with tail.

When nice is not enough

nice And renice These measures affect CPU priority. They don’t limit memory, fix erratic SQL queries, or replace a real resource limit. If you need to properly limit a service, look instead at systemd, cgroups, or application configuration.

If the process is clearly blocked or dangerous, it may need to be stopped. In this case, use the proper method: identify the service, read the logs, attempt a normal shutdown, and only then use [the appropriate method]. kill without stopping the bad process.

The manual pages of nice, renice And p.s. They detail the available options. In practice, remember this rule above all: lower the priority of a long-running task if it is hindering the server, but do not use it to mask a real load problem.

Share Facebook X WhatsApp LinkedIn Pinterest Reddit Email