Linux ps Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the Linux ps (process status) command to monitor and manage running processes on your system. The ps command is a fundamental tool for system administrators and developers, providing detailed information about the processes currently running, including their process ID, user, CPU and memory usage, and more. You will start by understanding the basic usage of the ps command, then learn how to filter processes by user, and finally explore monitoring CPU and memory usage of running processes. This lab covers essential system monitoring and management skills that are crucial for effectively managing Linux-based systems.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/TextProcessingGroup(["`Text Processing`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/TextProcessingGroup -.-> linux/grep("`Pattern Searching`") linux/SystemInformationandMonitoringGroup -.-> linux/ps("`Process Displaying`") linux/SystemInformationandMonitoringGroup -.-> linux/top("`Task Displaying`") linux/SystemInformationandMonitoringGroup -.-> linux/free("`Memory Reporting`") subgraph Lab Skills linux/grep -.-> lab-422866{{"`Linux ps Command with Practical Examples`"}} linux/ps -.-> lab-422866{{"`Linux ps Command with Practical Examples`"}} linux/top -.-> lab-422866{{"`Linux ps Command with Practical Examples`"}} linux/free -.-> lab-422866{{"`Linux ps Command with Practical Examples`"}} end

Understand the ps Command

In this step, you will learn about the ps (process status) command, which is a fundamental Linux command used to display information about running processes on the system.

The ps command provides a snapshot of the current processes running on the system, including their process ID (PID), user, CPU and memory usage, and other relevant information.

Let's start by running the basic ps command:

ps

Example output:

  PID TTY          TIME CMD
 1234 pts/0    00:00:00 bash
 5678 pts/0    00:00:00 ps

The output shows the current shell process (bash) and the ps command itself.

To see more detailed information about running processes, you can use the following options:

ps -ef

Example output:

UID        PID  PPID  C STIME TTY          TIME CMD
labex     1234  4321  0 10:30 pts/0    00:00:00 /bin/bash
labex     5678  1234  0 10:30 pts/0    00:00:00 ps -ef

The -e option displays all processes, and the -f option provides a full-format listing, including the user ID (UID), process ID (PID), parent process ID (PPID), CPU usage (C), start time (STIME), terminal (TTY), CPU time (TIME), and the command (CMD).

You can also filter the output by specific criteria, such as the user running the processes:

ps -u labex

Example output:

  PID TTY          TIME CMD
 1234 pts/0    00:00:00 bash
 5678 pts/0    00:00:00 ps

This command shows all processes owned by the labex user.

Filter Processes by User

In this step, you will learn how to filter the list of running processes by the user who owns them.

The ps command provides several options to filter the process list, one of which is the -u or --user option. This option allows you to display processes owned by a specific user.

Let's start by listing all processes owned by the labex user:

ps -u labex

Example output:

  PID TTY          TIME CMD
 1234 pts/0    00:00:00 bash
 5678 pts/0    00:00:00 ps

This command shows all processes owned by the labex user.

You can also use the grep command to filter the output of the ps command further. For example, to find all processes owned by the labex user that are running the bash command:

ps -ef | grep -i labex | grep -i bash

Example output:

labex     1234  4321  0 10:30 pts/0    00:00:00 /bin/bash

In this command, the ps -ef lists all processes, the first grep -i labex filters the output to show only processes owned by the labex user, and the second grep -i bash filters the output to show only processes running the bash command.

The -i option in the grep commands makes the search case-insensitive, so it will match both uppercase and lowercase occurrences of "labex" and "bash".

Monitor CPU and Memory Usage

In this step, you will learn how to use the ps command to monitor the CPU and memory usage of running processes.

To view the CPU and memory usage of all processes, you can use the ps command with the -o option to customize the output:

ps -eo pid,user,%cpu,%mem,cmd

Example output:

  PID USER     %CPU %MEM COMMAND
 1234 labex     2.0  0.1 /bin/bash
 5678 labex     0.5  0.2 ps -eo pid,user,%cpu,%mem,cmd

This command displays the following information for each process:

  • PID: The process ID
  • USER: The user who owns the process
  • %CPU: The percentage of CPU used by the process
  • %MEM: The percentage of memory used by the process
  • COMMAND: The command that started the process

You can also sort the output by CPU or memory usage to identify the processes consuming the most system resources:

ps -eo pid,user,%cpu,%mem,cmd --sort=-%cpu

Example output:

  PID USER     %CPU %MEM COMMAND
 5678 labex     2.0  0.2 ps -eo pid,user,%cpu,%mem,cmd --sort=-%cpu
 1234 labex     1.5  0.1 /bin/bash

This command sorts the output by descending CPU usage, so the process using the most CPU is displayed first.

Similarly, you can sort by memory usage:

ps -eo pid,user,%cpu,%mem,cmd --sort=-%mem

Example output:

  PID USER     %CPU %MEM COMMAND
 5678 labex     2.0  0.2 ps -eo pid,user,%cpu,%mem,cmd --sort=-%mem
 1234 labex     1.5  0.1 /bin/bash

This command sorts the output by descending memory usage, so the process using the most memory is displayed first.

Summary

In this lab, you learned about the ps (process status) command, which is a fundamental Linux command used to display information about running processes on the system. You started with the basic ps command and explored options to display more detailed information, such as the -e and -f options. You also learned how to filter the process list by the user who owns the processes, using the -u or --user option. Additionally, you covered how to monitor the CPU and memory usage of running processes.

The lab provided a comprehensive understanding of the ps command and its practical applications in managing and monitoring running processes on a Linux system.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like