How To Set Linux Process Priority Using Nice and Renice Commands

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19
At a glance
Powered by AI
The key takeaways are that Linux uses process priority and nice/renice commands to determine which processes get CPU time. Process priority is determined by a process' nice value, with higher priorities getting CPU time first. The kernel scheduler is responsible for choosing the next process to run based on priorities.

Process priority in Linux determines the order in which ready processes are executed, with higher priority processes getting CPU time before lower priority ones. It helps ensure important processes get resources they need to run smoothly.

The nice and renice commands are used to modify the priority of a process. Nice is used to launch a process with a specific priority, while renice changes the priority of a running process.

3/9/2019 How to Set Linux Process Priority Using nice and renice Commands

MONITORING TOOLS  0 I  TecMint :     

 BEGINNER'S GUIDE FOR LINUX 


How to Set Linux Start learning Linux in minutes 

Process Priority
51 Useful Lesser Known Commands for Linux Users
https://www.tecmint.com/set-linux-process-priority-using-nice-and-renice-commands/ 1/19
3/9/2019 How to Set Linux Process Priority Using nice and renice Commands

Using nice and renice


Commands
by Aaron Kili | Published: September 13, 2017 |
September 13, 2017

SPONSORED SEARCHES

linux renice Try Netflix WATCH NOW

configuration management sys One Month Free

 Download Your Free eBooks NOW - 10 Free  Vi/Vim Editor BEGINNER'S GUIDE 
Linux eBooks for Administrators | 4 Free Shell Learn vi/vim as a Full Text Editor 
Scripting eBooks

In this article, we’ll brie y


explain the kernel scheduler
(also known as the process
scheduler), and process
priority, which are topics
SHARE
beyond the scope of this
guide. Then we will dive into +
a little bit of Linux process
management: see how to 
run a program or command 0
 Linux Foundation Certi cation 
with modi ed priority and
also change the priority of  Exam Study Guide to LFCS and LFCE

running Linux processes. 0


 
Read Also: How to Monitor 
Linux Processes and Set How to Add Linux Host to Nagios Monitoring
0
Server Using NRPE Plugin
Process Limits on a Per-
User Basis  How to Install Nagios 4.3.4 on RHEL, CentOS and
Fedora
Understanding the
Linux Kernel 51 Useful Lesser Known Commands
Install Cacti (Network Monitoring) on
for Linux
RHEL/CentOS Users and Fedora 24-12
7.x/6.x/5.x
https://www.tecmint.com/set-linux-process-priority-using-nice-and-renice-commands/ 2/19
3/9/2019 How to Set Linux Process Priority Using nice and renice Commands

Scheduler Google Chrome 72 Released – Install on


RHEL/CentOS and Fedora
The kernel scheduler is a
unit of the kernel that How to Install Ubuntu 16.10/16.04 Alongside With
determines the most Windows 10 or 8 in Dual-Boot
suitable process out of all
runnable processes to
execute next; it allocates
processor time between the
runnable processes on a
system. A runnable process
is one which is waiting only
for CPU time, it’s ready to be
executed.

The scheduler forms the


core of multitasking in Linux,
using a priority-based
scheduling algorithm to
choose between the
runnable processes in the
system. It ranks processes
based on the most
deserving as well as need Linux System Administrator Bundle
with 7-Courses (96% off)
for CPU time.

Understanding Add to Cart - $69


Process Priority  Ending In: 3 days

and Nice Value


Computer Hacker Professional
Certi cation Course (96% Off)
The kernel stores a great
deal of information about Add to Cart - $59
processes including process  Ending In: 4 days
priority which is simply the
51 Useful Lesser Known Commands for Linux Users
https://www.tecmint.com/set-linux-process-priority-using-nice-and-renice-commands/ 3/19
3/9/2019 How to Set Linux Process Priority Using nice and renice Commands

scheduling priority attached


to a process. Processes LINUX EBOOKS
with a higher priority will be
Introducing Learn Linux In One Week and Go
executed before those with from Zero to Hero
a lower priority, while RedHat RHCE/RHCSA Certi cation Preparation
processes with the same Guide
priority are scheduled one Linux Foundations LFCS/LFCE Certi cation
after the next, repeatedly. Guide
Post x Mail Server Setup Guide for Linux
There are a total of 140
Ansible Setup Guide for Linux
priorities and two distinct
Django Setup Guide for Linux
priority ranges implemented
Awk Getting Started Guide for Beginners
in Linux. The rst one is nice
Citrix XenServer Setup Guide for Linux
value (niceness) which
ranges from -20 (highest
priority value) to 19 (lowest     
priority value) and the
Never Miss Any Linux Tutorials,
default is 0 , this is what we
Guides, Tips and Free eBooks
will uncover in this guide.
Join Our Community Of 150,000+ Linux Lovers
The other is the real-time
and get a weekly newsletter in your inbox
priority, which ranges from 1
to 99 by default, then 100 to YES! SIGN ME UP
139 are meant for user-
space.

One important characteristic


of Linux is dynamic priority-
based scheduling, which
allows nice value of a
processes to be changed
(increased or decreased)
depending on your needs, as
we’ll see later on.

51 Useful Lesser Known Commands for Linux Users


https://www.tecmint.com/set-linux-process-priority-using-nice-and-renice-commands/ 4/19
3/9/2019 How to Set Linux Process Priority Using nice and renice Commands

How to Check
Nice Value of
Linux Processes
To see the nice values of
processes, we can use
utilities such as ps, top or
htop.

To view processes nice


value with ps command in
user-de ned format (here
the NI column shows
niceness of processes).

$ ps -eo pid,ppid,ni,co

View Linux Processes Nice Values

Alternatively, you can use


top or htop utilities to view
Linux processes nice values
as shown.

$ top
$ htop

51 Useful Lesser Known Commands for Linux Users


https://www.tecmint.com/set-linux-process-priority-using-nice-and-renice-commands/ 5/19
3/9/2019 How to Set Linux Process Priority Using nice and renice Commands

Check Linux Process Nice Values using

Top Command

Check Linux Process Nice Values using

Htop Command

Difference Between
PR or PRI and NI
From the top and htop
outputs above, you’ll notice
that there is a column called
PR and PRI receptively
which show the priority of a
process.

This therefore means that:

NI – is the nice value,


which is a user-space
concept, while
PR or PRI – is the
process’s actual priority,
as seen by the Linux
kernel.

How To Calculate PR
or PRI Values
51 Useful Lesser Known Commands for Linux Users
https://www.tecmint.com/set-linux-process-priority-using-nice-and-renice-commands/ 6/19
3/9/2019 How to Set Linux Process Priority Using nice and renice Commands

Total number of priorit


Real time priority rang
User space priority ran

Nice value range (NI): -20 to


19

PR = 20 + NI
PR = 20 + (-20 to + 19)
PR = 20 + -20 to 20 +
PR = 0 to 39 which is s

But if you see a rt rather


than a number as shown in
the screen shot below, it
basically means the process
is running under real time
scheduling priority.

Linux rt Process

How to Run A
Command with a
Given Nice Value
in Linux
Here, we will look at how to
prioritize the CPU usage of a
program or command. 51 Useful Lesser Known Commands for Linux Users
If you
https://www.tecmint.com/set-linux-process-priority-using-nice-and-renice-commands/ 7/19
3/9/2019 How to Set Linux Process Priority Using nice and renice Commands

have a very CPU-intensive


program or task, but you
also understand that it
might take a long time to
complete, you can set it a
high or favorable priority
using the nice command.

The syntax is as follows:

$ nice -n niceness-valu
OR
$ nice -niceness-value
OR
$ nice --adjustment=nic

Important:

If no value is provided,
nice sets a priority of 10
by default.
A command or program
run without nice
defaults to a priority of
zero.
Only root can run a
command or program
with increased or high
priority.
Normal users can only
run a command or
program with low
priority.

51 Useful Lesser Known Commands for Linux Users


https://www.tecmint.com/set-linux-process-priority-using-nice-and-renice-commands/ 8/19
3/9/2019 How to Set Linux Process Priority Using nice and renice Commands

For example, instead of


starting a program or
command with the default
priority, you can start it with
a speci c priority using
following nice command.

$ sudo nice -n 5 tar -c


OR
$ sudo nice --adjustmen

You can also use the third


method which is a little
confusing especially for
negative niceness values.

$ sudo nice -5 tar -czf

Change the
Scheduling
Priority of a
Process in Linux
As we mentioned before,
Linux allows dynamic
priority-based scheduling.
Therefore, if a program is
already running, you can
change its priority with the
renice command in this
form:
51 Useful Lesser Known Commands for Linux Users
https://www.tecmint.com/set-linux-process-priority-using-nice-and-renice-commands/ 9/19
3/9/2019 How to Set Linux Process Priority Using nice and renice Commands

$ renice -n -12 -p 10
$ renice -n -2 -u apac

Change Process Priority

From the sample top output


below, the niceness of the
teamspe+ with PID 1055 is
now -12 and for all
processes owned by user
apache is -2 .

Still using this output, you


can see the formula PR = 20
+ NI stands,

PR for ts3server = 20 +
PR for apache processes

51 Useful Lesser Known Commands for Linux Users


https://www.tecmint.com/set-linux-process-priority-using-nice-and-renice-commands/ 10/19
3/9/2019 How to Set Linux Process Priority Using nice and renice Commands

Watch Processes Nice Values

Any changes you make with


renice command to a user’s
processes nice values are
only applicable until the next
reboot. To set permanent
default values, read the next
section.

How To Set
Default Nice
Value Of a
Speci c User’s
Processes
You can set the default nice
value of a particular user or
group in the
/etc/security/limits.conf
le. It’s primary function is
to de ne the resource limits
for the users logged in via
PAM.

51 Useful Lesser Known Commands for Linux Users


https://www.tecmint.com/set-linux-process-priority-using-nice-and-renice-commands/ 11/19
3/9/2019 How to Set Linux Process Priority Using nice and renice Commands

The syntax for de ning a


limit for a user is as follows
(and the possible values of
the various columns are
explained in the le):

#<domain> <type> <it

Now use the syntax below


where hard – means
enforcing hard links and soft
means – enforcing the soft
limits.

<username> <hard|soft>

Alternatively, create a le
under
/etc/security/limits.d/
which overrides settings in
the main le above, and
these les are read in
alphabetical order.

Start by creating the le


/etc/security/limits.d/tecmint-
priority.conf for user
tecmint:

# vi /etc/security/limi

51 Useful Lesser Known Commands for Linux Users


https://www.tecmint.com/set-linux-process-priority-using-nice-and-renice-commands/ 12/19
3/9/2019 How to Set Linux Process Priority Using nice and renice Commands

Then add this con guration


in it:

tecmint hard priority

Save and close the le. From


now on, any process owned
by tecmint will have a nice
value of 10 and PR of 30.

For more information, read


the man pages of nice and
renice:

$ man nice
$ man renice

You might also like to read


these following articles
about Linux process
management.

How to Find and Kill


Running Processes in
Linux
A Guide to Kill, Pkill and
Killall Commands to
Terminate a Process in
Linux
How to Monitor System
Usage, Outages and
Troubleshoot Linux
Servers
51 Useful Lesser Known Commands for Linux Users
https://www.tecmint.com/set-linux-process-priority-using-nice-and-renice-commands/ 13/19
3/9/2019 How to Set Linux Process Priority Using nice and renice Commands

CPUTool – Limit and


Control CPU Utilization
of Any Process in Linux

In this article, we brie y


explained the kernel
scheduler, process priority,
looked at how to run a
program or command with
modi ed priority and also
change the priority of active
Linux processes. You can
share any thoughts
regarding this topic via the
feedback form below.

If You Appreciate
What We Do Here On
TecMint, You Should
Consider:
1. Stay Connected to: Twitter |
Facebook | Google Plus

2. Subscribe to our email


updates: Sign Up Now
3. Get your own self-hosted
blog with a Free Domain at
($3.45/month).

51 Useful Lesser Known Commands for Linux Users


https://www.tecmint.com/set-linux-process-priority-using-nice-and-renice-commands/ 14/19
3/9/2019 How to Set Linux Process Priority Using nice and renice Commands

4. Become a Supporter - Make


a contribution via PayPal
5. Support us by purchasing
our premium books in PDF

format.
6. Support us by taking our
online Linux courses

We are thankful for your never


ending support.

Tags: linux monitoring

Aaron Kili View all Posts


Aaron Kili is a

Linux and F.O.S.S enthusiast, an
upcoming Linux SysAdmin, web
developer, and currently a
content creator for TecMint
who loves working with
computers and strongly
believes in sharing knowledge.

Your name can also be


listed here. Got a tip?
Submit it here to
become an TecMint
author.

51 Useful Lesser Known Commands for Linux Users


https://www.tecmint.com/set-linux-process-priority-using-nice-and-renice-commands/ 15/19
3/9/2019 How to Set Linux Process Priority Using nice and renice Commands

PREVIOUS NEXT STORY


STORY How to Secure
Learn Python Nginx with
Programming SSL and Let’s


With This 8- Encrypt in
Course FreeBSD
BONUS Bundle

 YOU MAY ALSO LIKE...

4

Swatchdog
– Simple
Log File
Watcher in
Real-Time
in Linux
1 AUG, 2018

51 Useful Lesser Known Commands for Linux Users


https://www.tecmint.com/set-linux-process-priority-using-nice-and-renice-commands/ 16/19
3/9/2019 How to Set Linux Process Priority Using nice and renice Commands

4

How to
Monitor
Nginx
Performanc
e Using
Netdata on
CentOS 7
22 JUN, 2018

 32

How To
Install
Elasticsear
ch,
Logstash,
and Kibana
(ELK Stack)
on
CentOS/RH
EL 7 51 Useful Lesser Known Commands for Linux Users
https://www.tecmint.com/set-linux-process-priority-using-nice-and-renice-commands/ 17/19
3/9/2019 How to Set Linux Process Priority Using nice and renice Commands

6 SEP, 2016

GOT SOMETHING TO SAY? JOIN THE


DISCUSSION.

Comment

Name * Email *

Website

Notify me of followup comments via e-


mail. You can also subscribe without
commenting.

Post Comment

This site uses Akismet to reduce spam. Learn


how your comment data is processed.

51 Useful Lesser Known Commands for Linux Users


https://www.tecmint.com/set-linux-process-priority-using-nice-and-renice-commands/ 18/19
3/9/2019 How to Set Linux Process Priority Using nice and renice Commands

LINUX MONITORING TOOLS LINUX INTERVIEW QUESTIONS OPEN SOURCE TOOLS

How to Use ‘fsck’ to Repair 10 Useful ‘Interview 10 Best Free and Open
File System Errors in Linux Questions and Answers’ on Source Software (FOSS)
Linux Shell Scripting Programs I Found in 2016
ngrep – A Network Packet
Analyzer for Linux 10 Useful “Squid Proxy 9 Best Tools to Access
Server” Interview Questions Remote Linux Desktop
How to Install ‘atop’ to and Answers in Linux
Monitor Logging Activity of 9 Tools to Monitor Linux
Linux System Processes 10 Useful Interview Disk Partitions and Usage in
Questions and Answers on Linux
Iotop – Monitor Linux Disk Linux Commands
I/O Activity and Usage Per- 8 Best Screen Recorders for
Process Basis 10 Basic Interview Desktop Screen Recording
Questions and Answers on in Linux
screenFetch – An Ultimate Linux Networking – Part 1
System Information 6 Best PDF Page Cropping
Generator for Linux 10 Interview Questions and Tools For Linux
Answers on Various
Commands in Linux


Tecmint: Linux Howtos, Tutorials & Guides © 2019.
All Rights Reserved.     
The material in this site cannot be republished
either online or o ine, without our permission.

51 Useful Lesser Known Commands for Linux Users


https://www.tecmint.com/set-linux-process-priority-using-nice-and-renice-commands/ 19/19

You might also like