Nice Command

Photo by Tim Cheung on Unsplash

Nice Command

The nice command in Unix-like operating systems is used to launch a process with a modified scheduling priority, known as a “niceness value.” The niceness value affects how the kernel schedules CPU time for that process relative to others.

How it works:

1. Niceness Value:

  • Niceness values range from -20 (highest priority) to 19 (lowest priority).

  • By default, most processes start with a niceness of 0.

  • A higher niceness value means the process is more “nice” to others, allowing them to have more CPU time, while a lower niceness value means the process demands more CPU time.

2. Command Syntax:

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

  • [OPTION]: You can set the niceness value using -n or specify it directly.

  • [COMMAND [ARG]...]: The command and its arguments you want to execute with the specified niceness.

3. Examples:

  • Run a process with a higher niceness (lower priority):

nice -n 10 ./my_program

This starts my_program with a niceness of 10.

  • Check the current process’s niceness:

ps -o pid,ni,comm

This displays the process ID (PID), niceness (NI), and command.

  • Run a process with a higher priority (lower niceness, e.g., -10):

sudo nice -n -10 ./my_program

Only superusers can set a niceness value below 0.

4. Changing Niceness of Running Processes:

Use the renice command to adjust the niceness of an already running process:

renice -n 5 -p 12345

This changes the niceness of the process with PID 12345 to 5.