5+ nice & renice command examples in Linux [Cheat Sheet]


CheatSheet

Reviewer: Deepak Prasad

Introduction to nice and renice command

An instance of a program is called a process. When you run any program, it creates a new process. In a Linux system, multiple processes are running at any given time. Each running process has a priority that indicates how fast the system executes it. Processes with a higher priority are usually completed first than with a lower priority.

In Linux, nice command helps to run a program with modified scheduling priority. You can set the priority of the process before it is started using nice command. But you cannot change the priority of the running process with nice command. Therefore, we have renice command to modify the priority of the running process.

 

How is nice command different from chrt command?

If you are not aware already, chrt command is another way of manipulating the real-time attributes of a process including priority.

"nice" is an historic utility which was used in the early days of batch computing to be "nice" to other users and give up some CPU time. It's still in use and useful and applies only to processes which run with the SCHED_OTHER policy on Linux.

"chrt" is a tool to change scheduling policy (SCHED_OTHER, SCHED_FIFO, SCHED_RR) and the priority of a process/task. With chrt you can either start a process with such a policy or modify an already running process/tasks policy. You need to have the permissions to do that.

So the main difference is that "nice" can only operate within the nice levels of the SCHED_OTHER policy while "chrt" can change the policy and the priority of a process/task.

 

How to check the nice value of running process

The nice value ranges from -20 (highest priority) to +19 (lowest priority).

You can check the nice value of the running process using ps command with -l option.

ubuntu@golinux:~$ ps -l
F S   UID     PID    PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
0 S  1000    1608    1598  0  80   0 -  4845 do_wai pts/0    00:00:00 bash
0 R  1000    3337    1608  0  80   0 -  5013 -      pts/0    00:00:00 ps

The NI column represents the nice value, whereas the PRI column represents the priority value.

You can also use the top command to view the nice value of processes.

$ top

top command to check nice value

 

Syntax to use nice command

The default nice value for processes created by a user is 0.

ubuntu@golinux:~$ nice
0

The syntax for the nice command is:

nice [OPTION] [COMMAND [ARG]...]

 

Different examples to use nice and renice command

1. nice command to set the priority of process

You can use -n or --adjustment option to set the nice value of the process.

$ nice -n nice_value command

OR

$ nice --adjustment=nice_value command

Sample Output:

To set the nice value of top command to 10, you can run the command below. It will execute the top command with a nice value of 10.

$ nice -n 10 top

set the nice value of top with nice command

You can also set the nice value without -n option using a hyphen.

$ nice -nice_value command

 

2. nice command to increase the priority of a process

You can use -<-nice_value> argument to increase the priority of any process. You can only set the negative nice if you run the command with root or sudo user.

$ nice -<-nice_value> command

Sample Output:

Here, we assigned -10 nice value to ps -l command.

nice command to set negative nice value

Or, you can specify a nice value with -n flag.

$ nice -n <nice_value> command

nice command to set negative value

 

3. nice command to decrease the priority of a process

We can again use -n argument to decrease the priority of a process by using -n <nice_value> along with the process to start. Alternatively we can use -<+nice_value> to decrease the priority.

Sample Output

nice command in Linux

 

4. renice command to change priority of existing process

You can change the priority of the running process with its process ID. The super-user can alter the priority of any process and set the priority to any value. -p or --pid option is used to specify process ID.

$ sudo renice -n nice_value -p process_id

OR

$ sudo renice -n nice_value --pid process_id

Sample Output:

renice command to change priority of process

 

5. renice command to change the priority of all processes of a specific group

You can modify the priority of all processes belonging to a specific group using the following command.

$ sudo renice -n nice_value -g group_id

OR

$ sudo renice -n nice_value --pgrp group_id

Sample Output:

ubuntu@golinux:~$ sudo renice -n 5 -g 218
218 (process group ID) old priority -1, new priority 5

 

6. renice command to change the priority of  all programs of a specific user

The below command changes the priority of all processes owned by the specific user.

$ sudo renice nice_value -u <name>|<id>

OR

$ sudo renice nice_value --user <name>|<id>

Sample Output:

ubuntu@golinux:~$ sudo renice -n 10 -u ubuntu
1000 (user ID) old priority -11, new priority 10

 

Conclusion

The nice and renice commands enable users to change or alter the priority of processes in a Linux system. If you still have any confusion, do let us know in the comment section.

 

What's Next

5 practical examples to list running processes in Linux

 

Further Reading

man page for nice command
man page for renice command

 

Rohan Timalsina

Rohan Timalsina

He is proficient in a wide range of skills, including Page Builder Plugins such as Elementor, Beaver Builder, Visual Composer, and Divi Builder. His expertise extends to Front End Development with HTML5/CSS3, JavaScript, Bootstrap, and React.js. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment