Introduction
In a multi-task operating system like Linux, the kernel decides how much CPU assigns to each process according to its priority level. Managers can influence that decision through commandsniceandrenicewhich allow for the adjustment of the implementation priority both when launching a process and while it is under implementation. This article explains the concept of priority, shows the syntax and practical use of both commands, and offers recommendations for safe and effective use in production environments.
What is the priority of processes?
The priority is expressed as an integer that typically goes from-20(highest priority) a19(minimum priority). A lower value indicates that the process will receive more CPU time than others with higher values. Only the userrootyou can assign negative values; common users can increase the value (make the process less priority) but not decrease it below zero. This mechanism prevents an application from homing resources and degrade the experience of the user or other critical services.
Use of commandnice
niceis used to start a process with a priority other than the default value (usually 0). The basic syntax is:
nice -nvalue command[arguments]
Wherevalueis the priority adjustment to be applied. For example, to release a low priority file compression:
nice -n 12 tar -czf copia.tgz / home / user / documents
In this case, the processtaryou will receive less CPU time, leaving more resources available for interactive applications such as the desktop or a web server. If priority needs to be raised (onlyroot):
nice -n -5 mysqld _ safe &
It is important to remember thatniceonly affects the start time; it does not change the priority of a process already under way.
Practical examples ofnice
- Perform a background antivirus scan without affecting the user's performance:
nice -n 15 clamscan -r /home - Run a report generation script at 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 commandrenice
Whileniceacts when launching a process,reniceallows to change the priority of a process that is already running, identifying it by your PID, by user name or by group. The essential syntax is:
renice -nvalue-pPID
To reduce the priority of a PID 3421 process to a value of 10:
renice -n 10 -p 3421
To increase priority (requires root privileges):
renice -n -5 -p 3421
It is also possible to apply the change to all user processes:
renice -n 8 -u juan
Or a group:
renice -n 12 -g staff
These operations are carried out in real time, without the need to stop and restart the task, which is very useful on servers where the load can vary unpredictable.
Key differences betweenniceandrenice
- Time of implementation:
niceonly at the beginning;reniceat any time while the process is active. - Permissions: Both require root for negative values; only users can increase the value (do less priority) in both cases.
- Flexibility:
reniceallows to adjust priorities of process groups or a full user, something thatniceHe can't do it directly.
Good practices and security considerations
- Reserve negative values (
-20 a-1) para tareas del sistema o de administración; evitar que usuarios normales los asignen. - Monitoring the impact of changes with tools like
top,htoporpidstatbefore and after implementationniceorrenice. - Use
nicein tasks programmed bycronso that maintenance jobs do not compete with peak time load. - To document each priority adjustment made on production servers, including motif, applied value and time, to facilitate audits and problem solving.
- Test changes in a staging environment or a virtual machine before applying them to critical systems.
Conclusion
Domainniceandreniceis essential for any Linux administrator who seeks to balance the performance and stability of their systems. By understanding how the process priority works and knowing when to use each command, you can ensure that critical tasks get the necessary resources without sacrificing the response capacity of the environment. Practice with examples in a laboratory and observing the effects in real time helps to internalize these concepts and apply them with confidence in production.


