How to list running processes on a Linux system?

LinuxLinuxBeginner
Practice Now

Introduction

Understanding and managing processes is a fundamental aspect of Linux system administration. This tutorial will guide you through the process of listing running processes on a Linux system, providing you with the necessary knowledge and tools to effectively monitor and analyze your system's activities.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/ProcessManagementandControlGroup(["`Process Management and Control`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/ProcessManagementandControlGroup -.-> linux/jobs("`Job Managing`") linux/SystemInformationandMonitoringGroup -.-> linux/ps("`Process Displaying`") linux/SystemInformationandMonitoringGroup -.-> linux/top("`Task Displaying`") linux/ProcessManagementandControlGroup -.-> linux/kill("`Process Terminating`") linux/ProcessManagementandControlGroup -.-> linux/killall("`Multi-Process Killing`") linux/ProcessManagementandControlGroup -.-> linux/pkill("`Pattern-Based Killing`") linux/ProcessManagementandControlGroup -.-> linux/wait("`Process Waiting`") linux/ProcessManagementandControlGroup -.-> linux/bg_process("`Background Management`") subgraph Lab Skills linux/jobs -.-> lab-417262{{"`How to list running processes on a Linux system?`"}} linux/ps -.-> lab-417262{{"`How to list running processes on a Linux system?`"}} linux/top -.-> lab-417262{{"`How to list running processes on a Linux system?`"}} linux/kill -.-> lab-417262{{"`How to list running processes on a Linux system?`"}} linux/killall -.-> lab-417262{{"`How to list running processes on a Linux system?`"}} linux/pkill -.-> lab-417262{{"`How to list running processes on a Linux system?`"}} linux/wait -.-> lab-417262{{"`How to list running processes on a Linux system?`"}} linux/bg_process -.-> lab-417262{{"`How to list running processes on a Linux system?`"}} end

Understanding Processes in Linux

In the Linux operating system, a process is an instance of a running program. Each process is assigned a unique identifier called a Process ID (PID) and has its own memory space, resources, and execution environment. Understanding processes is crucial for system administration, performance optimization, and troubleshooting.

What is a Process?

A process in Linux is a program that is currently running and being executed by the operating system. When a user runs a command or launches an application, the operating system creates a new process to handle the execution of that program. Each process has its own memory space, CPU time, and system resources, such as open files, network connections, and more.

Process Hierarchy

Processes in Linux are organized in a hierarchical structure, where each process can create child processes. The first process, called the "init" process, is the parent of all other processes on the system. Child processes inherit certain properties from their parent processes, such as environment variables and resource limits.

graph TD init[init] init --> process1[Process 1] init --> process2[Process 2] process1 --> child1[Child Process 1] process1 --> child2[Child Process 2] process2 --> child3[Child Process 3]

Process States

Processes in Linux can exist in different states, such as:

  • Running: The process is currently being executed by the CPU.
  • Waiting: The process is waiting for an event, such as I/O operation or user input, to occur.
  • Stopped: The process has been temporarily stopped, usually by a signal or user intervention.
  • Zombie: The process has terminated, but its parent process has not yet collected its exit status.

Understanding these process states is important for monitoring and troubleshooting system performance.

Process Management Commands

Linux provides several commands for managing and interacting with processes, such as:

  • ps: Displays information about running processes.
  • top: Provides a real-time view of running processes and system resource utilization.
  • kill: Sends a signal to a process, allowing you to terminate or control its execution.
  • pgrep: Finds or signals processes based on their name or other attributes.

Mastering these process management commands is essential for effective system administration and troubleshooting.

Listing Running Processes

Listing running processes is a fundamental task in Linux system administration and troubleshooting. Linux provides several commands and options to list running processes, each with its own set of features and use cases.

The ps Command

The ps (process status) command is the most commonly used tool for listing running processes. It provides a snapshot of the current state of running processes on the system.

Here's an example of using the ps command to list all running processes:

$ ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 10:30 ?        00:00:03 /sbin/init
root         2     0  0 10:30 ?        00:00:00 [kthreadd]
root         3     2  0 10:30 ?        00:00:00 [rcu_gp]
root         4     2  0 10:30 ?        00:00:00 [rcu_par_gp]
root         6     2  0 10:30 ?        00:00:00 [kworker/0:0H-kblockd]

The ps -ef command displays the full process listing, including the user ID (UID), process ID (PID), parent process ID (PPID), CPU utilization (C), start time (STIME), terminal (TTY), CPU time (TIME), and the command (CMD) that started the process.

The top Command

The top command provides a real-time, dynamic view of the running processes on the system. It displays information such as CPU and memory usage, as well as the list of running processes sorted by various criteria.

Here's an example of the top command in action:

$ top
top - 10:35:42 up 1 day, 23:05,  1 user,  load average: 0.00, 0.01, 0.05
Tasks: 167 total,   1 running, 166 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.2 sy,  0.0 ni, 99.8 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  2047820 total,   172604 free,   416640 used,  1458576 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.   997864 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
    1 root      20   0    4424    804    736 S   0.0  0.0   0:03.11 systemd
    2 root      20   0       0      0      0 S   0.0  0.0   0:00.02 kthreadd
    3 root      20   0       0      0      0 I   0.0  0.0   0:00.00 rcu_gp
    4 root      20   0       0      0      0 I   0.0  0.0   0:00.00 rcu_par_gp
    6 root      20   0       0      0      0 I   0.0  0.0   0:00.00 kworker/0:0H-kblockd

The top command provides a real-time view of system resource utilization and the list of running processes, allowing you to monitor and troubleshoot your system effectively.

The pgrep Command

The pgrep command is a powerful tool for finding processes based on various criteria, such as the process name, user, or other attributes. It can be used to quickly identify running processes without the need to parse the output of the ps command.

Here's an example of using pgrep to find all processes owned by the root user:

$ pgrep -u root
1
2
3
4
6

The pgrep -u root command will return the PIDs of all processes owned by the root user.

By combining the power of the ps, top, and pgrep commands, you can effectively list and manage running processes on your Linux system.

Analyzing Process Details

Once you have listed the running processes on your Linux system, you may want to analyze them in more detail to understand their behavior, resource usage, and other characteristics. Linux provides several commands and options to help you dive deeper into process information.

The ps Command with Additional Options

The ps command can be used with various options to display more detailed information about running processes. Here are some examples:

  • ps -aux: Displays all processes, including those not attached to a terminal.
  • ps -eo pid,user,comm,args: Displays the process ID, user, command, and full command line arguments.
  • ps -eo pid,ppid,cmd: Displays the process ID, parent process ID, and the command.

These options allow you to customize the output of the ps command to focus on the specific information you need.

The /proc Filesystem

The /proc filesystem is a virtual filesystem that provides detailed information about running processes. Each process has a directory under /proc with the process ID as the directory name. Within these directories, you can find various files that contain information about the process, such as its command line, environment variables, and resource usage.

For example, to view the command line arguments of a process with PID 1234, you can use the following command:

$ cat /proc/1234/cmdline

The /proc filesystem is a powerful tool for deep-level process analysis and troubleshooting.

The pstree Command

The pstree command displays a hierarchical tree-like view of running processes, showing the parent-child relationships between them. This can be useful for understanding the process structure and dependencies on your system.

Here's an example of using the pstree command:

$ pstree
systemd─┬─ModemManager───2*[{ModemManager}]
        ├─NetworkManager───2*[{NetworkManager}]
        ├─accounts-daemon───2*[{accounts-daemon}]
        ├─agetty
        ├─atd
        ├─cron
        ├─dbus-daemon
        ├─gdm3─┬─Xorg───3*[{Xorg}]
        │      └─gdm-session-wor─┬─gnome-shell───6*[{gnome-shell}]
        │                       └─ibus-daemon───3*[{ibus-daemon}]
        ├─irqbalance───2*[{irqbalance}]
        ├─kerneloops
        ├─ksmtuned───2*[{ksmtuned}]
        ├─lightdm───lightdm───Xorg───3*[{Xorg}]
        ├─lsmd───2*[{lsmd}]
        ├─lvmetad
        ├─multipathd───2*[{multipathd}]
        ├─polkitd───2*[{polkitd}]
        ├─rsyslogd───2*[{rsyslogd}]
        ├─snapd───5*[{snapd}]
        ├─systemd-journal
        ├─systemd-logind
        ├─systemd-network
        ├─systemd-resolve
        ├─systemd-udevd
        └─upowerd───2*[{upowerd}]

The pstree command provides a clear visual representation of the process hierarchy, making it easier to understand the relationships between running processes.

By combining the power of the ps, /proc filesystem, and pstree commands, you can thoroughly analyze and understand the processes running on your Linux system.

Summary

In this comprehensive tutorial, you have learned how to list running processes on a Linux system, analyze process details, and utilize various commands and techniques to effectively manage your system's processes. By mastering these skills, you can enhance your Linux system's performance, troubleshoot issues, and optimize your workflow.

Other Linux Tutorials you may like