06. Changing process priorities

As mentioned before, each process takes up a certain amount of CPU time. For every moment the CPU is busy running one process, another process loses valuable running time.

As you can imagine, there will be some instances when you are under time constraints and want to prioritize one job over others. Or other times you'll want to push a process back so that it can run quietly in the background while you tend to other things. To control such priorities, we may launch a program with the nice command, or modify a currently running process with renice.

Niceness

The term niceness comes from how "nice" the process is, in terms of how much CPU time it hogs. A nice process (as you would imagine) doesn't take too much CPU time and shares the CPU liberally with other processes.

This range of values goes from -20 to 20, where the lower the niceness, the more prioritized the process is. Thus, a "mean" process (one that hogs system resources) would have values close to -20, while a nicer process is closer to 20. The default niceness of any program is 10.

Syntax for nice

There are several ways you can call on nice:

$ nice -12 python longProgram.py
$ nice -n 12 python longProgram.py
$ nice --adjustment=12 python longProgram.py

As you can tell, we call nice, then specify any arguments before we input a command.

Syntax for renice

We can use the renice command to alter the priority of an already running process without disrupting its operation.

$ renice priority [[-p] pids] [[-g] pgrps] [[-u] users]

You may specify a process via its PID with the -p option. If you want to change the priorities of all processes under a certain group or user, use the -g and -u options.

$ renice 5 -p 21649
# Prioritizes the process with pid 21649
$ renice 19 -u SarahMarsh
# Deprioritizes all processes belonging to user SarahMarsh

Take your Linux skills to the next level!

Command Line Kung Fu

Take your Linux skills to the next level! Try Linux & UNIX

Command Line Kung Fu is packed with dozens of tips and practical real-world examples. You won't find theoretical examples in this book. The examples demonstrate how to solve actual problems. The tactics are easy to find, too. Each chapter covers a specific topic and groups related tips and examples together.

$ Check price
14.9914.99Amazon 4.5 logo(27+ reviews)

More Linux & UNIX resources

Aching back from coding all day?

Prism Glasses

Aching back from coding all day? Try Back Problems

Ever feel achy from sitting crunched up on your computer table? Try lying down with these optical glasses that allow you to work on your laptop while lying flat on your back. This is the perfect solution with those with limited mobility or those who wish to prevent neck cramps and back strains.

$ Check price
4.454.45Amazon 4 logo(128+ reviews)

More Back Problems resources

Ad