How to Check if a Process is Running in Linux?

Learn via video courses
Topics Covered

In the Linux operating system, processes are fundamental units of execution that allow multiple tasks to run simultaneously. Whether you are an administrator, developer, or simply a curious user, there may be occasions when you need to check whether a specific process is running on your Linux system. Fortunately, Linux provides several methods and command-line tools to accomplish this task. In this guide, we will explore various techniques to determine if a process is currently active.

We will begin with the most basic and widely used command, "ps," which stands for "process status". The "ps" command displays a snapshot of the currently running processes on your system. By specifying different options and arguments, you can filter and search for specific processes by their names, process IDs (PIDs), or other criteria. We will explore different options of the "ps" command to identify running processes efficiently.

Additionally, we will cover the "pgrep" command, which simplifies the process of finding and identifying processes by their names or other attributes. This command is particularly useful when you know the name of the process you are interested in and want a quick way to verify its status.

List Running Processes in Linux

Using the "ps" Command

To list running processes in Linux using the ps command, you can use the following options:

  1. To display a snapshot of currently running processes:

    This will show a list of all processes running on the system, including their process IDs (PIDs), CPU and memory usage, and other details.

  2. To display a full list of running processes, continuously updating the output:

    This option provides a continuous stream of running processes, refreshing the output as new processes start or stop.

  3. To display a process tree, showing the hierarchical relationship between processes:

    This option presents the processes in a tree-like structure, illustrating their parent-child relationships.

Using the "pgrep" Command

To list running processes in Linux using the pgrep command, you can simply execute the following command:

This command will list all running processes along with their respective process IDs (PIDs). The -l option is used to display the process names along with the PIDs.

  • If you want to search for a specific process by name, you can replace the . in the command with the desired process name. For example, to list processes with the name "nginx", you would use:

    This will show all running processes with "nginx" in their name, along with their PIDs.

The pgrep command provides additional options that you can explore by referring to its manual page (man pgrep). It allows for more advanced process search and filtering based on various criteria.

Using the "pidof" Command

To list running processes in Linux using the pidof command, you can execute the following command:

Replace <process_name> with the name of the process you want to check. This command will display the Process IDs (PIDs) of all running processes that match the specified name.

For example, to list the PIDs of running processes with the name "nginx", you would use:

The output will show the PIDs of all running "nginx" processes, separated by spaces.

  • If no processes are running with the specified name, the command will produce no output.

  • The pidof command provides a straightforward way to list PIDs of running processes based on their names. However, note that it only returns the PIDs and does not provide detailed information about the processes.

using-the-pidof-command

Using the "proc" File System

To list running processes in Linux using the /proc file system, you can navigate through the /proc directory to access information about each running process. Here's how you can do it:

  1. Open a terminal

  2. Navigate to the /proc directory by executing the following command:

  3. Use the 'ls' command to list the directories corresponding to running processes. Each directory in /proc with a numeric name represents a running process, where the directory name is the process ID (PID). Execute the following command:

    This command will list the directories that have numeric names (representing PIDs) followed by a forward slash.

  4. You can then explore the directories for more information about each process. For instance, you can use the cat command to view details about a specific process. Replace <PID> with the desired process ID:

This will display information such as the process name, PID, parent process ID, memory usage, and more.

  • Using the /proc file system gives you direct access to information about running processes on your Linux system. However, interpreting the information requires understanding the structure and content of the files in the /proc directory.

using-the-proc-file-system

Using the "top" Command

To list running processes in Linux using the top command, follow these steps:

  1. Open a terminal.

  2. Execute the top command:

  3. The top command will display a dynamic and interactive view of running processes, with real-time updates. The processes are listed in a table format, showing information such as the process ID (PID), CPU and memory usage, execution time, and more.

  4. By default, the processes are sorted by CPU usage, with the most resource-intensive processes listed at the top. You can press the following keys to customize the display:

    • Press M to sort by memory usage.
    • Press P to sort by CPU usage.
    • Press T to sort by execution time.
    • Press q to exit the top.
  5. The top command also provides various interactive features. For example, you can highlight a specific process using the arrow keys and view more detailed information about it.

    • The top command is a powerful tool for monitoring and managing running processes in Linux. It provides real-time insights into resource usage and allows you to interactively explore and manage processes.

using-the-top-command

Using the "htop" Command

To list running processes in Linux using the htop command, you can follow these steps:

  1. Open a terminal.

  2. If htop is not installed on your system, you can install it using the package manager of your Linux distribution. For example, on Ubuntu or Debian-based systems, you can use the following command:

  3. Once htop is installed, run the command:

  4. The htop command will launch an interactive and colorful process viewer. It provides a comprehensive view of running processes, including CPU and memory usage, process IDs (PIDs), parent-child relationships, and more.

  5. By default, the processes are sorted by CPU usage, with the most resource-intensive processes listed at the top. You can use the arrow keys to navigate and scroll through the process list.

  6. htop also provides various interactive features and options. For example, you can press the following keys to perform actions:

    • Press F2 or S to customize the display settings.
    • Press F3 or / to search for processes by name.
    • Press F4 or t to filter processes by various criteria.
    • Press F9 or k to send a signal (kill, terminate) to a selected process.
    • Press q to exit htop.
    • The htop command offers an advanced and user-friendly interface for monitoring and managing running processes in Linux. It provides real-time updates and numerous customization options to suit your needs.

Using the "atop" Command

To list running processes in Linux using the atop command, you can follow these steps:

  1. Open a terminal.

  2. If atop is not installed on your system, you can install it using the package manager of your Linux distribution. For example, on Ubuntu or Debian-based systems, you can use the following command:

  3. Once atop is installed, run the command:

  4. The atop command will launch an interactive and comprehensive process monitoring tool. It displays a wide range of information, including CPU and memory usage, disk activity, network activity, and process details.

  5. By default, the processes are sorted by resource usage, with the most active processes listed at the top. You can use the arrow keys to navigate through the process list.

  6. atop provides various interactive features and options. For example, you can press the following keys to perform actions:

    • Press c to sort processes by CPU usage.
    • Press m to sort processes by memory usage.
    • Press d to sort processes by disk activity.
    • Press n to sort processes by network activity.
    • Press q to exit atop.
  7. You can also use command-line options to customize the output and specify the interval at which the data is updated. For example, to display processes sorted by CPU usage every 2 seconds, you can use the following command:

    • The atop command provides an extensive overview of system activity, including detailed information about running processes. It offers real-time updates and various customization options for advanced process monitoring in Linux.

How to Kill Running Processes in Linux?

Using the "kill" Command

To kill running processes in Linux using the kill command, you can follow these steps:

  1. Identify the process ID (PID) of the process you want to kill. You can use tools like ps, pgrep, or htop to find the PID. For example, to list all running processes with their PIDs, you can use the following command:

  2. Once you have the PID of the process you want to kill, execute the kill command followed by the PID. For example, to kill a process with PID 12345, you would use:

    By default, the kill command sends the SIGTERM signal to the process, which requests the process to terminate gracefully. If the process does not respond to the SIGTERM signal, you can use the SIGKILL signal, which forcefully terminates the process. To send the SIGKILL signal, you can use the -9 option with the kill command:

Note:
Using the SIGKILL signal should be the last resort, as it does not allow the process to perform any cleanup actions.

  1. If you want to kill multiple processes simultaneously, you can specify multiple PIDs separated by spaces. For example:

    This command will send the termination signal to both processes with PIDs 12345 and 67890.

  2. If you know the process name instead of the PID, you can use the pkill command to kill processes by name. For example, to kill processes with the name "nginx", you can use:

This command will send the termination signal to all processes whose name matches "nginx".

  • The kill and pkill commands are used to terminate running processes in Linux. Remember to use them with caution and ensure that you are targeting the correct processes.

Using the "pkill" Command

To list running processes in Linux using the pkill command, you can execute the following command:

Replace <process_name> with the name or part of the name of the process you want to list. This command will display a list of running processes whose names match the specified pattern. The -l option is used to show the process names along with their corresponding process IDs (PIDs).

For example, to list processes with the name "nginx", you would use:

The output will show the process names and their PIDs.

  • If there are no running processes that match the specified name, the command will produce no output.

  • The pkill command allows you to search and list processes based on their names. It provides a convenient way to find and manage running processes in Linux.

Using the "killall" Command

To kill running processes in Linux using the killall command, you can follow these steps:

  1. Identify the process or processes you want to kill. You can use tools like ps, pgrep, or htop to find the names of the processes.

  2. Once you have the name of the process you want to kill, execute the killall command followed by the process name. For example, to kill a process named "nginx", you would use:

    By default, the killall command sends the SIGTERM signal to the process, which requests the process to terminate gracefully. If the process does not respond to the SIGTERM signal, you can use the -9 option with the killall command to send the SIGKILL signal, forcefully terminating the process:

    • Using the SIGKILL signal should be the last resort, as it does not allow the process to perform any cleanup actions.
  3. The killall command will terminate all processes with the specified name. If there are multiple processes with the same name, all of them will be killed.

  4. If you want to kill processes based on a pattern or a partial name match, you can use wildcards. For example, to kill all processes that start with "nginx", you would use:

This command will terminate all processes with names starting with "nginx".

  • The killall command provides a convenient way to kill running processes in Linux based on their names or patterns. However, it is important to exercise caution when using this command to ensure that you are targeting the correct processes.

Learn More

To learn more about some other essential commands in Linux you can check out the following articles:

Conclusion

  • Use the pgrep command to search for processes by name and obtain their process IDs (PIDs). If the command returns a PID, the process is running.
  • Employ the ps command along with grep to filter and display specific process information. Searching for a process name within the output indicates its running status.
  • Utilize the pidof command to find the PID of a running process by its name. If the command returns a PID, the process is running.
  • Explore the /proc file system by navigating to the corresponding process directory using the process ID (PID). If the directory exists, it indicates that the process is running.