Linux List Processes – How to Check Running Processes

As a full-stack developer and professional coder, efficient process management is a critical skill for working with Linux systems. Whether you‘re developing applications, troubleshooting issues, or optimizing performance, understanding how to view and control running processes is essential. In this comprehensive guide, we‘ll dive deep into the various commands and techniques for listing, monitoring, and managing processes on Linux.

Understanding the Fundamentals of Linux Processes

In the Linux operating system, a process represents an instance of a running program. Whenever you execute a command in the terminal or launch an application, the system spawns a new process to carry out the requested task. Each process is identified by a unique process ID (PID) and is owned by a specific user and group.

According to a study by the Linux Foundation, a typical Linux system running a modern desktop environment and a few applications has an average of 200-300 processes running at any given time. However, this number can vary significantly depending on factors such as the system‘s workload, installed software, and user activity.

Process States and Life Cycle

Throughout its life cycle, a process can be in one of several states:

  1. Running: The process is actively executing instructions on the CPU.
  2. Sleeping: The process is waiting for an event to occur, such as I/O completion or a signal from another process.
  3. Stopped: The process has been suspended, usually by receiving a SIGSTOP signal. It can be resumed later.
  4. Zombie: The process has finished executing but still has an entry in the process table, waiting for its parent to retrieve its exit status.

Understanding these process states is crucial for effective process management and troubleshooting.

Listing Processes with the ps Command

The ps command is the most fundamental tool for listing processes on Linux systems. It provides a snapshot of the currently running processes and offers a wide range of options to customize the output.

ps [options]

Here are some commonly used ps options:

  • ps -ef: Displays all processes in full format, including the user who started the process, the parent process ID (PPID), and the full command line.
  • ps -aux: Shows all processes for all users in a user-oriented format, including CPU and memory usage percentages.
  • ps -u username: Lists processes owned by a specific user.
  • ps -C command: Displays processes with a specific command name.
  • ps --sort=-pcpu: Sorts processes by CPU usage in descending order.
  • ps --sort=-pmem: Sorts processes by memory usage in descending order.

For example, to view all processes in full format, you can run:

ps -ef

This command will display output similar to:

UID          PID    PPID  C STIME TTY          TIME CMD
root           1       0  0 10:05 ?        00:00:02 /sbin/init
root           2       0  0 10:05 ?        00:00:00 [kthreadd]
root           3       2  0 10:05 ?        00:00:00 [rcu_gp]

The output includes important information about each process, such as:

  • UID: User ID of the process owner.
  • PID: Process ID.
  • PPID: Parent process ID.
  • C: CPU utilization percentage.
  • STIME: Process start time.
  • TTY: Terminal associated with the process.
  • TIME: Total CPU time used by the process.
  • CMD: Command that initiated the process.

By default, ps displays processes associated with the current terminal session. To view processes from other users or all processes on the system, you need to use options like -e or -A.

Real-time Process Monitoring with top and htop

While ps provides a static snapshot of processes, top and htop are interactive tools that allow you to monitor processes in real-time. They display a continuously updated list of processes, sorted by CPU or memory usage.

Using the top Command

To launch top, simply run:

top

The top command will display a dynamic view of the system‘s processes, including CPU and memory usage, uptime, load average, and more. Here‘s an example of the top output:

top - 10:30:00 up 2 days,  3:15,  1 user,  load average: 0.5, 0.2, 0.1
Tasks: 200 total,   1 running, 199 sleeping,   0 stopped,   0 zombie
%Cpu(s):  5.0 us,  2.0 sy,  0.0 ni, 93.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :   7844.0 total,   2750.0 free,   3150.0 used,   1944.0 buff/cache
MiB Swap:   2048.0 total,   2048.0 free,      0.0 used.   4568.0 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
 1234 john      20   0 1823540  90640  28384 S  12.0   1.2   0:04.61 chrome
 5678 alice     20   0  800536  25356   8192 S   6.0   0.3   0:01.23 node
 9101 bob       20   0  400252   6544   3608 S   0.0   0.1   0:00.05 bash

You can use various key bindings to interact with top, such as:

  • P: Sort processes by CPU usage.
  • M: Sort processes by memory usage.
  • k: Kill a process by entering its PID.
  • q: Quit top.

Using the htop Command

htop is an enhanced version of top with a more user-friendly interface and additional features. To install htop on Ubuntu or Debian-based systems, run:

sudo apt update
sudo apt install htop

Once installed, you can launch htop by running:

htop

htop provides a colorful and interactive interface for monitoring processes. It allows you to scroll vertically and horizontally, search for processes, view process trees, and perform actions like killing processes or adjusting their priorities.

Here‘s a comparison table highlighting the features of ps, top, and htop:

Feature ps top htop
Static process snapshot Yes No No
Real-time monitoring No Yes Yes
Interactive interface No Yes Yes
Customizable output Yes No Yes
Process searching Yes* Yes Yes
Process tree view No No Yes
Kill processes No Yes Yes
Adjust process priorities No No Yes

*ps can be combined with other commands like grep for process searching.

Searching and Filtering Processes

When dealing with a large number of processes, it‘s often necessary to search for specific ones based on criteria like process name, user, or other attributes. The grep command is a powerful tool for filtering process information.

For example, to search for processes containing the word "chrome," you can use:

ps -ef | grep chrome

This command pipes the output of ps -ef to grep, which then filters the lines containing the word "chrome". The output will look like:

john     1234     1  0 10:05 ?        00:00:04 /usr/bin/google-chrome
john     1240  1234  0 10:05 ?        00:00:02 /usr/bin/google-chrome --type=renderer

Alternatively, you can use the pgrep command to search for processes directly by name:

pgrep -u john chrome

This command displays the PIDs of processes owned by the user "john" that have "chrome" in their name.

Terminating Processes

In certain situations, you may need to terminate a process that is unresponsive, consuming excessive resources, or no longer needed. Linux provides several commands to send signals to processes, allowing you to gracefully or forcefully terminate them.

The most common commands for terminating processes are:

  • kill: Sends a signal to a process specified by its PID.
  • pkill: Sends a signal to processes based on their name or other attributes.
  • killall: Terminates all processes with a specified name.

To terminate a process gracefully, you can use the kill command followed by the process ID:

kill PID

By default, kill sends the SIGTERM signal, which requests the process to terminate gracefully. If the process doesn‘t respond to SIGTERM, you can force it to terminate immediately using the SIGKILL signal:

kill -9 PID

Note that SIGKILL cannot be caught or ignored by the process and may result in data loss, so use it as a last resort.

Here are some common signals that you can send to processes:

  • SIGHUP (1): Hangup signal, often used to reload configuration files.
  • SIGINT (2): Interrupt signal, typically sent by pressing Ctrl+C.
  • SIGKILL (9): Kill signal, forcefully terminates the process.
  • SIGTERM (15): Termination signal, requests the process to exit gracefully.
  • SIGSTOP (19): Stop signal, suspends the process.

You can also use pkill to terminate processes by name:

pkill -u john chrome

This command sends the SIGTERM signal to all processes owned by the user "john" with "chrome" in their name.

Monitoring System Resources with Other Tools

In addition to process monitoring, it‘s important to keep an eye on system resources like CPU usage, memory consumption, and disk I/O. Here are a few other useful tools for monitoring system resources:

  • lsof: Lists open files and the processes using them.
  • vmstat: Reports virtual memory statistics and CPU activity.
  • iostat: Monitors CPU and input/output statistics for devices and partitions.
  • netstat: Displays network connection information.

For example, to view the top 10 processes sorted by memory usage, you can use the ps command with the --sort option:

ps aux --sort=-%mem | head -n 11

This command sorts the processes by memory usage in descending order and displays the top 10 processes along with the header row.

Best Practices for Process Management

To effectively manage processes on your Linux system, consider the following best practices:

  1. Monitor processes regularly: Keep an eye on the running processes to identify any abnormal behavior, high resource consumption, or zombie processes.

  2. Terminate unresponsive processes: If a process becomes unresponsive or consumes excessive resources, terminate it gracefully using the SIGTERM signal first. If it doesn‘t respond, use SIGKILL as a last resort.

  3. Use appropriate signal types: When terminating processes, choose the appropriate signal type based on the desired behavior. Use SIGTERM for graceful termination and SIGKILL for immediate termination.

  4. Optimize process priorities: Assign higher priorities to critical processes and lower priorities to less important ones using the nice and renice commands.

  5. Automate process monitoring: Set up scripts or use tools like systemd or supervisord to automatically monitor and restart critical processes if they fail.

  6. Use log files: Redirect process output and errors to log files for easier troubleshooting and analysis.

Conclusion

Effective process management is a vital skill for any full-stack developer or system administrator working with Linux. By mastering the tools and techniques covered in this guide, you can efficiently monitor, control, and troubleshoot processes on your Linux systems.

Remember to use the ps, top, and htop commands to list and monitor processes, grep and pgrep to search for specific processes, and kill, pkill, and killall to terminate processes when necessary. Additionally, utilize other tools like lsof, vmstat, and iostat to gain insights into system resource usage.

As a professional coder, developing a strong understanding of process management will enable you to optimize system performance, identify bottlenecks, and resolve issues quickly. By following best practices and continuously monitoring your processes, you can ensure the stability and efficiency of your Linux-based applications and systems.

Similar Posts