The nice and renice commands in Linux: process priority

Introduction

In a multitasking operating system like Linux, the kernel decides how much CPU to allocate to each process based on its priority level. Administrators can influence that decision using the nice and renice commands, which allow adjusting the execution priority both when launching a process and while it is running. This article explains the concept of priority, shows the syntax and practical use of both commands, and offers recommendations for using them safely and effectively in production environments.

What is process priority?

Priority is expressed as an integer that typically ranges from -20 (highest priority) to 19 (lowest priority). A lower value indicates that the process will receive more CPU time compared to others with higher values. Only the root user can assign negative values; regular users can increase the value (make the process less prioritized) but cannot decrease it below zero. This mechanism prevents an application from hogging resources and degrading the user experience or other critical services.

Using the nice command

nice is used to start a process with a priority different from the default value (usually 0). The basic syntax is:

nice -n value command [arguments]

Where value is the priority adjustment you want to apply. For example, to launch a file compression with low priority:

nice -n 12 tar -czf copia.tgz /home/usuario/documentos

In this case, the tar process will receive less CPU time, leaving more resources available for interactive applications such as the desktop or a web server. If you need to raise the priority (only root):

nice -n -5 mysqld_safe &

It is important to remember that nice only affects the moment of startup; it does not modify the priority of a process already running.

Practical examples of nice

  • Perform an antivirus scan in the background without affecting user performance: nice -n 15 clamscan -r /home
  • Run a report generation script during the night: nice -n 10 php generar_informe.php
  • Start a video transcoding task with minimum priority: nice -n 19 ffmpeg -i entrada.mkv -c:v libx264 salida.mp4

The renice command

While nice acts when launching a process, renice allows changing the priority of a process that is already running, identified by its PID, username, or group. The essential syntax is:

renice -n value -p PID

To reduce the priority of a process with PID 3421 to a value of 10:

renice -n 10 -p 3421

To increase the priority (requires root privileges):

renice -n -5 -p 3421

It is also possible to apply the change to all processes of a user:

renice -n 8 -u juan

Or to a group:

renice -n 12 -g staff

These operations are performed in real time, without needing to stop and restart the task, which is very useful on servers where load can vary unpredictably.

Key differences between nice and renice

  • Time of application: nice only at startup; renice at any moment while the process is active.
  • Permissions: Both require root for negative values; only users can increase the value (make the process less prioritized) in both cases.
  • Flexibility: renice allows adjusting priorities of process groups or an entire user, something nice cannot do directly.

Best practices and security considerations

  • Reserve negative values (-20 to -1) for system or administrative tasks; avoid letting normal users assign them.
  • Monitor the impact of changes using tools such as top, htop, or pidstat before and after applying nice or ren

    This post is also available in ESPAÑOL.

Leave a Reply

Your email address will not be published. Required fields are marked *

Esta obra está bajo una Licencia Creative Commons Atribución 4.0 Internacional para Francesc Roig francesc@vivaldi.net .