In Unix ecosystem, a process is a running program. So, any program running on your Linux box is a process. One or more processes can be running on your Linux box at a time. We can display the information about active processes using ps command. By default, the ps command only shows current user's processes. You can use "ps -ef" (without quotes, of course) command to display all processes. To view the user of a process, we use "ps -u". Hope you got a basic idea about Linux processes. Let us now come to the today's topic - how to change the priority of a process in Linux using nice and renice commands.
As you might know, by default, Linux kernel considers all processes equally important and allocates the same amount of CPU time for each process. Sometimes, you might want to increase or decrease the priority of certain processes to utilize more CPU time. This is where the nice and renice commands comes in help. Nice command is used to run a process with an user defined priority whereas renice command is used to change the priority of a running process. Generally, nice and renice commands are used to change the priority than the default priority of a process.
Change The Priority Of A Process With Nice command
View the default priority of a Process
Whenever a process starts normally, it gets the default priority value of zero.
We use cmus program, a command line media player for the purpose of this guide.
Let us run it using command:
$ cmus &
Note: If a command is terminated by the control operator &, the shell executes the command in the background in a subshell.
Now let us see the running processes using command:
$ ps -al
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD 0 T 1000 13829 13704 0 80 0 - 39431 signal pts/0 00:00:00 cmus 0 R 1000 13848 13704 0 80 0 - 8148 - pts/0 00:00:00 ps
As you see in the above output, the niceness value is listed under the column heading "NI". So, it is clear that all processes gets the default priority value of zero.
Start a process with nice utility
If you start a process with nice command without any arguments, it gets the default value of 10. Here 10 is the niceness value or priority. Niceness values range from -20 (most favorable to the process) to 19 (least favorable to the process). To put this simply, the negative values (Eg. -20) gives higher priority to a process and positive values (Eg. 19) gives lower priority.
Now, let us stop the running program cmus with command:
$ kill -9 13829
Here, 13829 is the PID of cmus program. You will see this value under heading "PID" in the above output.
Now, run cmus program with 'nice' utility.
$ nice cmus &
Check the running processes with command:
$ ps -al
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD 0 T 1000 13957 13704 0 90 10 - 55815 signal pts/0 00:00:00 cmus 0 R 1000 13985 13704 0 80 0 - 8148 - pts/0 00:00:00 ps
See, now cmus process got the default niceness value of 10, which means its priority has been changed.
Start a process with nice utility with lower priority
Also, you can start the same process with specific niceness value, for example 15, like below.
Kill the process with with kill command:
$ kill -9 13957
Again, start the process with niceness value of 15:
$ nice -15 cmus &
Note: Here, do not confuse - (hyphen) with minus. We use - (hyphen) to pass a value. Hence, to assign a positive value, we use -15. Likewise, to assign a negative value, we use --15 (i.e double hyphen).
Let us check the niceness value using ps -al command:
$ ps -al
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD 0 T 1000 14060 13704 0 95 15 - 55815 signal pts/0 00:00:00 cmus 0 R 1000 14069 13704 0 80 0 - 8148 - pts/0 00:00:00 ps
See, now cmus process got the niceness value of 15.
In the above examples, we launched the processes with lower priority. Remember positive niceness value represents lower priority.
Start a Process with Nice utility with higher Priority
You already know that negative niceness value represents higher priority. Please note that regular users are not allowed to start a process with higher priority. You need to be root user to launch any process with higher priority.
So, to start a process using nice utility with lower priority, for example -15, run the following command:
# nice --15 cmus &
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD 4 S 0 14331 14306 0 80 0 - 20531 poll_s pts/0 00:00:00 sudo 4 S 0 14332 14331 0 80 0 - 13796 wait pts/0 00:00:00 su 4 S 0 14333 14332 0 80 0 - 5736 wait pts/0 00:00:00 bash 4 T 0 14358 14333 0 65 -15 - 55643 signal pts/0 00:00:00 cmus 0 R 0 14366 14333 0 80 0 - 8148 - pts/0 00:00:00 ps
Start processes with -n option
You can also use -n option to increase or decrease the priority.
To increase priority, run the following command as root user:
# nice -n -15 cmus &
To decrease priority, run the following command as normal user:
$ nice -n 15 cmus &
Change The Priority Of A Running Process With Renice command
What we have seen so far is we launched a process with an user defined priority. Now, we are going to change the priority of a running process using renice command.
Let us see the running processes using command:
$ ps -al
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD 0 T 1000 15059 14306 0 95 15 - 55815 signal pts/0 00:00:00 cmus 0 R 1000 15066 14306 0 80 0 - 8148 - pts/0 00:00:00 ps
As you see in the above output, cmus is running with lower priority value of 15.
To change the current niceness value of a running process, for example 18, run the following command:
$ renice -n 18 -p 15059
Here, 15059 is the process id of cmus process.
Sample output would be:
15059 (process ID) old priority 15, new priority 18
Verify the niceness value using command:
$ ps -al
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD 0 T 1000 15059 14306 0 98 18 - 55815 signal pts/0 00:00:00 cmus 0 R 1000 15074 14306 0 80 0 - 8148 - pts/0 00:00:00 ps
To change it to negative niceness value (to assign higher priority), run the following command as root user:
# renice -n -20 -p 14749
You can even change the priorities of all running group belongs to a particular group like below.
# renice -n -20 -g ostechnix
The above command changes the priority of all running processes which are belongs to a group called ostechnix.
And also, you can change the priority of running process owned by particular user, for example sk, using command:
# renice -n -20 -u sk
For more options, I suggest you to refer the man pages of nice and renice utilities.
$ man nice
$ man renice
- How To Find Out How Long A Process Has Been Running In Linux
- How To Display Process Information Using Procs On Linux
I hope you got a basic idea about what is a Linux process, and how to change its priority using nice and renice utilities.Hope it helps.
Thanks for stopping by!
Help us to help you:
- Subscribe to our Email Newsletter : Sign Up Now
- Support OSTechNix : Donate Via PayPal
- Download free E-Books and Videos : OSTechNix on TradePub
- Connect with us: Reddit | Facebook | Twitter | LinkedIn | RSS feeds
Have a Good day!!
How can you set the default priority for a particular ubuntu process? So anytime I launch this program, it will have a minimum default priority already assigned. I’m trying to do this for filezilla, but any installed process would be helpful.
The default priority (nice level) can be set via
/etc/security/limits.conf. Check this link -> https://stackoverflow.com/questions/3600216/how-to-set-default-process-scheduling-priority-nice-value-for-all-remotely-l