How to View Running Processes in Bash

In Bash, there are several commands available to view running processes on your system. These commands provide information about the processes currently executing, including their process IDs (PIDs), resource usage, and more. Understanding how to view and manage processes is essential for system administration and troubleshooting.

1. Using the ps Command

The ps (process status) command is one of the most commonly used commands to display information about running processes. By default, it shows processes running in the current shell.

Basic Usage of ps

ps

In this example:

  • The command displays a list of processes running in the current shell session, along with their PIDs and other information.

Common Options for ps

  • ps -e or ps -A: Show all processes running on the system.
  • ps -f: Show full-format listing, including additional details like the user and command line.
  • ps aux: Display all processes with detailed information, including user, CPU, and memory usage.

Example of Using ps with Options

ps aux

In this example:

  • The command displays a detailed list of all processes running on the system, including the user, PID, CPU usage, memory usage, and command line.

2. Using the top Command

The top command provides a dynamic, real-time view of running processes. It updates the display periodically, showing the most resource-intensive processes at the top.

Basic Usage of top

top

In this example:

  • The command opens an interactive interface displaying the current processes, along with their CPU and memory usage.
  • You can press q to exit the top interface.

Common Keyboard Shortcuts in top

  • h: Display help.
  • k: Kill a process by entering its PID.
  • q: Quit the top interface.

3. Using the htop Command

htop is an enhanced version of top that provides a more user-friendly interface with color coding and easier navigation. It may need to be installed separately on some systems.

Basic Usage of htop

htop

In this example:

  • The command opens the htop interface, displaying running processes with a more visually appealing layout.
  • You can use the arrow keys to navigate, and press F10 to exit.

4. Using the pgrep Command

The pgrep command allows you to search for processes based on name and other attributes. It returns the PIDs of matching processes.

Example of Using pgrep

pgrep bash

In this example:

  • The command searches for all processes with the name "bash" and returns their PIDs.
  • This is useful for quickly finding specific processes without displaying all running processes.

5. Using the kill Command

While not directly for viewing processes, the kill command is often used in conjunction with process viewing commands to terminate processes by their PID.

Example of Using kill

kill <pid></pid>

In this example:

  • Replace <PID> with the actual process ID you want to terminate.
  • This command sends a termination signal to the specified process.

Conclusion

Viewing running processes in Bash is crucial for monitoring system performance and managing resources. By using commands like ps, top, htop, pgrep, and kill, you can effectively manage and troubleshoot processes on your system. Understanding these commands will enhance your ability to maintain a healthy and efficient operating environment.