Tech Junkie Blog - Real World Tutorials, Happy Coding!: Linux Process Management: Controlling Process Priority With nice And renice Command

Monday, March 28, 2022

Linux Process Management: Controlling Process Priority With nice And renice Command

 In Linux you can control the priority of a process with the commands nice and renice.  There are limitations if you are a non-root user. The nice and renice commands have the values in the range of -20 to +19. The higher the numbers the higher the priority, or the nicer the process is meaning the less CPU it would use, so it's kind of the opposite of what you are thinking.  So it's like nice guys finished last?

So if we run the sleep process again let's see what happens by default

sleep 1500&

[root@cent7 jhuynh]# sleep 1500&
[1] 3401
[root@cent7 jhuynh]# ps -l
F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
4 S     0  3358  3315  0  80   0 - 68658 poll_s pts/0    00:00:00 sudo
4 S     0  3365  3358  0  80   0 - 58056 do_wai pts/0    00:00:00 su
4 S     0  3368  3365  0  80   0 - 29107 do_wai pts/0    00:00:00 bash
4 S     0  3401  3368  0  80   0 - 27014 hrtime pts/0    00:00:00 sleep
0 R     0  3402  3368  0  80   0 - 38332 -      pts/0    00:00:00 ps

As you can see the priority(PRI) is set to 80 percent by default

Now let's be a nice guy and assign the sleep process to the nicest value -19

[root@cent7 jhuynh]# nice -n 19 sleep 1500&
[2] 3438
[root@cent7 jhuynh]# ps -l
F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
4 S     0  3358  3315  0  80   0 - 68658 poll_s pts/0    00:00:00 sudo
4 S     0  3365  3358  0  80   0 - 58056 do_wai pts/0    00:00:00 su
4 S     0  3368  3365  0  80   0 - 29107 do_wai pts/0    00:00:00 bash
4 S     0  3401  3368  0  80   0 - 27014 hrtime pts/0    00:00:00 sleep
4 S     0  3438  3368  0  99  19 - 27014 hrtime pts/0    00:00:00 sleep
0 R     0  3439  3368  0  80   0 - 38332 -      pts/0    00:00:00 ps

As you can see the new sleep process is set to priority 99 meaning only run the process if processes with the a lower number is ran first.  It's like being the 99th person in line.

If you run it at the highest nice value, not so nice. Let's see what happens

[root@cent7 jhuynh]# nice -n -20 sleep 1500&
[2] 3546
[root@cent7 jhuynh]# ps -l
F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
4 S     0  3358  3315  0  80   0 - 68658 poll_s pts/0    00:00:00 sudo
4 S     0  3365  3358  0  80   0 - 58056 do_wai pts/0    00:00:00 su
4 S     0  3368  3365  0  80   0 - 29107 do_wai pts/0    00:00:00 bash
4 S     0  3543  3368  0  99  19 - 27014 hrtime pts/0    00:00:00 sleep
4 S     0  3546  3368  0  60 -20 - 27014 hrtime pts/0    00:00:00 sleep
0 R     0  3547  3368  0  80   0 - 38332 -      pts/0    00:00:00 ps

You can also reassign the priority of an existing process with renice command, with the renice command you have to specify the process id

[root@cent7 jhuynh]# renice -n 5 -p 3543
3543 (process ID) old priority 19, new priority 5
[root@cent7 jhuynh]# ps -l
F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
4 S     0  3358  3315  0  80   0 - 68658 poll_s pts/0    00:00:00 sudo
4 S     0  3365  3358  0  80   0 - 58056 do_wai pts/0    00:00:00 su
4 S     0  3368  3365  0  80   0 - 29107 do_wai pts/0    00:00:00 bash
4 S     0  3543  3368  0  85   5 - 27014 hrtime pts/0    00:00:00 sleep
4 S     0  3546  3368  0  60 -20 - 27014 hrtime pts/0    00:00:00 sleep
0 R     0  3589  3368  0  80   0 - 38332 -      pts/0    00:00:00 ps

Linux was nice enough to tell you that the process has been changed from priority of 19 to 5, so now the process is assigned a priority value of 85.  Still the nicest priority!

The caveat on the renice command is that if you are not a user with root privileges, you cannot set a higher priority than the original priority of a process. 

If you are root you can also control other user's priority settings by editing the /etc/security/limits.conf. Priority is the last item or settings that you can set limits on, so move to the end of the line and type in the following for user limit, if you want group limits you just prefix it with the @ sign

#*               soft    core            0
#*               hard    rss             10000
#@student        hard    nproc           20
#@faculty        soft    nproc           20
#@faculty        hard    nproc           50
#ftp             hard    nproc           0
#@student        -       maxlogins       4

# End of file
techjunkie - priority 5

Now the next time techjunkie sets a priority he will be limited to priority number 5 nice value.  Since he is not a root user he cannot renice the process to be anything higher than 5.  Therefore he is a pretty nice guy.



No comments:

Post a Comment

Search This Blog