nice Command Examples in Linux

The nice command enables you to run a command with a different nice value than the default. The -n option increments the nice value by the given integer; if you don’t provide an integer, then the command will assume an increment of 10. By running nice without any options, you’ll see the default nice value. You must have the root user authority to run a command at a higher priority. Once lowered, the priority for any process cannot be increased by normal users, even if they own the process.

Syntax

The syntax of the nice command is:

# nice [-n {nice value increment}] [command]

Normally, when a process is started, it gets the default priority value of 0. The nice command is used to start a process with a different priority. Its syntax is as follows:

# nice -n [NICELEVEL] [command]

Here’s an example of setting a big job to run at nice level 7:

# nice −7 sort VeryLargeFile > outfile

If you run nice without a level, 10 is used. Normal processes (run without nice) run at level zero, which you can see by running nice with no arguments:

# nice
0

The superuser can also lower the nice level, increasing a process’s priority:

# nice --10 myprogram

(Yes, that’s “dash negative 10”.) To see the nice levels of your jobs, use ps and look at the “NI” column:

# ps -o pid,user,args,nice

The top command can also be used to display the priority of processes. It shows two columns, named NI (displaying the nice level mapping of processes) and PR (displaying the priority value mapping in a kernel to a larger priority queue).

Modyfying priority

Assigning a low nice value, that is, a higher priority, to a CPU-hungry process will impact the performance of other processes running on the same Linux system. Hence, only root is allowed to give a higher priority to a process, for example, setting negative nice values on running processes.

Normal users can only lower the priority of their processes, that is, they can set only a higher positive value than the existing nice value for a given process.

Related Post