Every Linux system has multiple processes running at the same time. They are scheduled for execution by kernel scheduler that determines the next process to be executed depending on multiple factors. It uses a priority-based scheduling algorithm to rank and run processes. Processes with higher priority are executed before those with lower priority. In most cases, it is advisable to leave process scheduling to the Linux system. But if you need to set process priority in Linux, that can also be done. In this article, we will learn how to set process priority in Linux.
How to Set Process Priority in Linux
There are two kinds of priority values associated with each process – one is a niceness value that ranges from -20(highest priority) to 19(lowest priority), and the other is real-time priority ranging from 1 to 99. When we mean to set process priority, we will be changing the niceness value of a process.
You can check niceness value of process, using ps, top or htop.
$ ps -eo pid,ppid,ni,comm
The NI column in following output shows the niceness value. You can also use top and htop which show niceness values by default.
If you have a long-running CPU intensive program that you don’t think will run anytime soon, then you can use nice command to set its priority. Here is the syntax of nice command.
$ nice -n niceness-value [command args] OR $ nice -niceness-value [command args] OR $ nice --adjustment=niceness-value [command args]
If you don’t provide any niceness-value, the above command will use a default priority of 10. Please note, only root users will be able to change nice values of programs. If you don’t use any nice default value, the programs get a default priority of 0.
Here are a couple of commands to set priority of commands using nice command.
$ sudo nice -n 5 tar -czf backup.tar.gz /home/ubuntu/data OR $ sudo nice --adjustment=5 tar -czf backup.tar.gz /home/ubuntu/data
If a program is already running, you can change its priority dynamically using renice command. The first command allows you to change priority using PID (1243) or using process name (apache).
$ renice -n -12 -p 1243 $ renice -n -2 -u apache
In this article, we have learnt how to set process priority in Linux. Linux is pretty good at scheduling processes and setting their priority values. In most cases, you don’t need to change process priorities. But if you have some long running or system intensive processes, then you can use nice or renice commands. If you are yet to run commands, then use nice to set their priority before they are run. If your commands are already running, use renice to change their priority.
Also read:
How to Set Default MySQL Data Directory in Linux
How to Fix firewall-cmd commant not found
How to Protect Hard & Symbolic Links
How to Run Shell Script on Another Server
How to Manage Systemd Services on Remote Server
The post How to Set Process Priority in Linux appeared first on Fedingo.
Discover more from Ubuntu-Server.com
Subscribe to get the latest posts sent to your email.