Linux nice Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, we will explore the Linux nice command, which allows you to adjust the priority of a process. We will start by understanding the concept of process priority and how the nice command works, then move on to practical examples of using nice to manage process scheduling. The lab covers checking the current niceness value of a process, running commands with different niceness values, and adjusting the priority of running processes. This lab provides a hands-on approach to understanding and utilizing the nice command for effective process management in a Linux environment.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux(("`Linux`")) -.-> linux/ProcessManagementandControlGroup(["`Process Management and Control`"]) linux/SystemInformationandMonitoringGroup -.-> linux/ps("`Process Displaying`") linux/ProcessManagementandControlGroup -.-> linux/bg_process("`Background Management`") subgraph Lab Skills linux/ps -.-> lab-422840{{"`Linux nice Command with Practical Examples`"}} linux/bg_process -.-> lab-422840{{"`Linux nice Command with Practical Examples`"}} end

Understand the nice Command

In this step, we will learn about the nice command in Linux, which allows you to adjust the priority of a process. The nice command is used to set the niceness value of a process, which determines the process's scheduling priority.

The niceness value ranges from -20 to 19, with -20 being the highest priority and 19 being the lowest priority. The default niceness value for a new process is 0.

Let's start by checking the current niceness value of a process:

## Check the niceness value of the current shell process
nice -n 0 echo $PPID

Example output:

22456

The output shows the process ID (PID) of the current shell process, which has a niceness value of 0.

Now, let's run a command with a different niceness value:

## Run a command with a niceness value of 5
nice -n 5 sleep 60 &

In this example, we've used the nice command to run the sleep 60 command with a niceness value of 5. The & at the end of the command puts the process in the background.

You can verify the niceness value of the sleep process using the ps command:

ps -p $(pgrep sleep) -o pid,ni

Example output:

  PID   NI
22457   5

The output shows that the sleep process has a niceness value of 5.

Adjust Process Priority with nice

In this step, we will learn how to adjust the priority of a process using the nice command.

First, let's start a new process with a higher priority (lower niceness value):

## Start a process with a niceness value of -5
nice -n -5 sleep 120 &

In this example, we've used the nice command to start the sleep 120 command with a niceness value of -5, which gives the process a higher priority.

You can verify the niceness value of the sleep process using the ps command:

ps -p $(pgrep sleep) -o pid,ni

Example output:

  PID   NI
22458  -5

The output shows that the sleep process has a niceness value of -5.

Now, let's start another process with a lower priority (higher niceness value):

## Start a process with a niceness value of 10
nice -n 10 sleep 120 &

In this example, we've used the nice command to start the sleep 120 command with a niceness value of 10, which gives the process a lower priority.

You can verify the niceness value of the sleep process using the ps command:

ps -p $(pgrep sleep) -o pid,ni

Example output:

  PID   NI
22459  10

The output shows that the sleep process has a niceness value of 10.

To change the niceness value of an existing process, you can use the renice command:

## Change the niceness value of the first sleep process to 0
renice -n 0 -p $(pgrep sleep | head -n 1)

You can verify the niceness value of the sleep process using the ps command:

ps -p $(pgrep sleep) -o pid,ni

Example output:

  PID   NI
22458   0
22459  10

The output shows that the first sleep process now has a niceness value of 0, while the second sleep process still has a niceness value of 10.

Practical Examples of using nice

In this final step, we will explore some practical examples of using the nice command.

Prioritizing a CPU-intensive Task

Imagine you have a CPU-intensive task that you want to run in the background without affecting the performance of your system. You can use the nice command to lower the priority of this task:

## Run a CPU-intensive task with a niceness value of 10
nice -n 10 python3 cpu_intensive.py &

In this example, we're running a Python script called cpu_intensive.py with a niceness value of 10, which means it will have a lower priority than other processes on the system.

Prioritizing an I/O-bound Task

If you have an I/O-bound task, such as a file transfer or a backup operation, you can use the nice command to increase the priority of this task:

## Run an I/O-bound task with a niceness value of -5
nice -n -5 rsync -aAXv /source /destination &

In this example, we're using the rsync command to perform a file transfer with a niceness value of -5, which means it will have a higher priority than other processes on the system.

Prioritizing a Background Task

If you have a background task that you want to run without affecting the performance of your system, you can use the nice command to lower the priority of this task:

## Run a background task with a niceness value of 5
nice -n 5 ./background_script.sh &

In this example, we're running a script called background_script.sh with a niceness value of 5, which means it will have a lower priority than other processes on the system.

Summary

In this lab, we learned about the nice command in Linux, which allows us to adjust the priority of a process. We started by understanding the nice command and how it sets the niceness value of a process, which determines its scheduling priority. We then practiced adjusting the priority of processes by running commands with different niceness values. By using the nice command, we can control the relative priority of processes running on a system, which can be useful for optimizing system performance or ensuring critical tasks have the resources they need.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like