Linux psnice Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, we will explore the psnice command in Linux, which is used to adjust the priority of running processes. The psnice command allows you to increase or decrease the priority of a process, which can be useful for managing system resources and optimizing performance. We will cover the basics of the psnice command and provide practical use cases to demonstrate its functionality.

The lab will guide you through adjusting the priority of running processes using the psnice command. You will learn how to check the current priority of a process, increase its priority, and decrease its priority. These techniques can be valuable for managing system resources and improving the performance of critical applications.

Linux Commands Cheat Sheet


Skills Graph

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

Introduction to the psnice Command

In this step, we will explore the psnice command in Linux, which is used to adjust the priority of running processes. The psnice command allows you to increase or decrease the priority of a process, which can be useful for managing system resources and optimizing performance.

First, let's check the current priority of a running process using the ps command:

ps -o pid,ni,cmd -p $(pgrep -n bash)

Example output:

  PID   NI CMD
 1234   0  /bin/bash

The NI column shows the nice value of the process, which ranges from -20 (highest priority) to 19 (lowest priority). The default nice value for new processes is 0.

Now, let's use the psnice command to increase the priority of the current bash process:

sudo psnice -n -5 -p $(pgrep -n bash)

This command sets the nice value of the current bash process to -5, which increases its priority.

Let's verify the change:

ps -o pid,ni,cmd -p $(pgrep -n bash)

Example output:

  PID   NI CMD
 1234  -5  /bin/bash

As you can see, the nice value of the bash process has been changed to -5, indicating a higher priority.

Adjusting Process Priority with psnice

In this step, we will learn how to use the psnice command to adjust the priority of running processes.

First, let's start a new process that runs indefinitely:

sleep 1000 &

This will start a sleep process that runs for 1000 seconds in the background.

Now, let's check the priority of the sleep process:

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

Example output:

  PID   NI CMD
 1235   0 sleep 1000

As you can see, the sleep process has a nice value of 0, which is the default priority.

Let's decrease the priority of the sleep process using the psnice command:

sudo psnice -n 5 -p $(pgrep -n sleep)

This command sets the nice value of the sleep process to 5, which decreases its priority.

Verify the change:

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

Example output:

  PID   NI CMD
 1235   5 sleep 1000

Now, let's increase the priority of the sleep process:

sudo psnice -n -5 -p $(pgrep -n sleep)

This command sets the nice value of the sleep process to -5, which increases its priority.

Verify the change:

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

Example output:

  PID   NI CMD
 1235  -5 sleep 1000

As you can see, we have successfully adjusted the priority of the sleep process using the psnice command.

Practical Use Cases of the psnice Command

In this step, we will explore some practical use cases for the psnice command.

One common use case is to prioritize important processes over less critical ones. For example, let's say you have a web server running on your system, and you want to ensure that the web server process has a higher priority than other background processes.

First, let's start a background process that simulates a less important task:

while true; do echo "Background process running"; sleep 1; done &

This will start a background process that runs indefinitely, printing a message every second.

Now, let's find the process ID of the web server process (replace apache2 with the name of your web server process):

WEB_SERVER_PID=$(pgrep -n apache2)

We can then use the psnice command to increase the priority of the web server process:

sudo psnice -n -5 -p $WEB_SERVER_PID

This sets the nice value of the web server process to -5, giving it a higher priority than the background process.

You can verify the priority changes by running the ps command:

ps -o pid,ni,cmd -p $WEB_SERVER_PID

Example output:

  PID   NI CMD
 1236  -5 /usr/sbin/apache2 -k start

Another practical use case for psnice is to temporarily reduce the priority of a resource-intensive process, such as a long-running backup or data processing job, to ensure that other critical processes can run smoothly.

For example, let's say you have a data processing job running in the background:

python data_processing.py &

You can use psnice to temporarily reduce the priority of this process:

sudo psnice -n 5 -p $(pgrep -n python)

This will allow other important processes to have a higher priority and access more system resources, while the data processing job continues to run in the background at a lower priority.

Summary

In this lab, we explored the psnice command in Linux, which is used to adjust the priority of running processes. We learned how to check the current priority of a process using the ps command, and how to increase or decrease the priority of a process using the psnice command. We also discussed practical use cases of the psnice command, such as managing system resources and optimizing performance.

The key takeaways from this lab are:

  1. The psnice command allows you to adjust the priority of running processes, with the nice value ranging from -20 (highest priority) to 19 (lowest priority).
  2. You can use the psnice command to increase or decrease the priority of a process, which can be useful for managing system resources and optimizing performance.
  3. The ps command can be used to check the current priority of a running process, with the NI column showing the nice value.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like