Linux Command ps Usage and 5 Options

The Linux command ps is an essential tool for monitoring the system by displaying the processes that are currently running. Managing and monitoring system processes in Linux is crucial, and the ps command plays a key role in this. In this post, we will cover the basic usage of the ps command, explore key options, and demonstrate how to use it effectively in real-life scenarios.

What is the Linux command ps?

The ps command, short for “process status,” is used to display the status of processes currently running in a Linux system. A process refers to a program that is being executed and handled as a single task by the system. In Linux, multiple processes run simultaneously, and the ps command allows you to check how specific processes are running.

Basic usage

If you run the ps command without any options, it will only display processes that are running in the current terminal (session). The default information provided includes:

ps
ShellScript
Figure 1. Linux command ps: showing processes running in the current session
Figure 1. Linux command ps: showing processes running in the current session

Here is what each column represents:

  • PID: Process ID, which is a unique number identifying each process.
  • TTY: The terminal to which the process is connected.
  • TIME: The CPU time used by the process.
  • CMD: The command that was used to execute the process.

Useful options

The ps command can output more detailed information or filter specific processes by using various options. Below are some frequently used options.

View all processes: ps -e

By default, the ps command only shows processes running in the current terminal. To display all system processes, you can add the -e option.

ps -e
ShellScript

The columns displayed with the ps -e option are the same as when running the command without any options. The CMD column only shows the basic command and does not display any options or parameters used.

Figure 2. Linux command ps: using the -e option to view all processes
Figure 2. Linux command ps: using the -e option to view all processes

View all processes in detail: ps aux

The ps aux command shows all processes with detailed information, including options and arguments used to run the commands.

ps aux
ShellScript

Running ps aux outputs a variety of information in the following order: USER, PID, %CPU, %MEM, VSZ, RSS, TTY, STAT, START, TIME, COMMAND. This option is more useful for system monitoring than the -e option, as it provides more detailed data.

Figure 3. Linux command ps: using the aux option to view all processes in detail
Figure 3. Linux command ps: using the aux option to view all processes in detail

Additional columns provided when using aux include:

  • USER: The name of the user who executed the process.
  • %CPU: The percentage of CPU time used by the process.
  • %MEM: The percentage of memory used by the process.
  • VSZ: The amount of virtual memory used by the process.
  • RSS: The amount of memory currently used by the process.
  • STAT: The process status (e.g., ‘S’ for sleeping, ‘R’ for running).
  • START: The time when the process started.

View processes by a specific user: ps -u username

To view processes run by a specific user, use the -u option.

ps -u username
ShellScript

For example, running ps -u ito will display processes run by the user ito. If you replace ito with root, you can see the processes run by the root user.

Figure 4. Linux command ps: using the -u option to view processes by a specific user
Figure 4. Linux command ps: using the -u option to view processes by a specific user

Find a specific process: ps -p PID

If you know the process ID (PID) of a specific process, you can use the -p option to view details about that process.

ps -p 2484
ShellScript

This command will display information about the process with PID 2484.

Figure 5. Linux command ps: viewing a process with a specific PID
Figure 5. Linux command ps: viewing a process with a specific PID

View processes in a tree structure: ps -ef --forest

To see the relationships between processes in a tree structure, use the --forest option. This option shows how processes are related in a parent-child hierarchy.

ps -ef --forest
ShellScript

This command helps visualize all system processes and their parent-child relationships. The -e option displays all processes, while the -f option shows a full-format listing, as shown in the following figure. In this case, PIDs 3, 4, and 5 are shown to have PID 2 as their parent process.

Figure 6. Linux command ps: viewing processes in a tree structure
Figure 6. Linux command ps: viewing processes in a tree structure

The -f option provides additional information, such as:

  • PPID: The Parent Process ID, showing the PID of the parent process.
  • C: CPU time used by the process.
  • STIME: The time the process started (equivalent to the START column).

How to use the ps command effectively

The ps command is invaluable for system administrators who need to monitor system health or troubleshoot problematic processes. For example, when running multiple applications on a server, you can identify processes consuming excessive memory or CPU and manage resources efficiently.

  • Checking memory usage: Use the ps aux command to check memory usage and terminate processes that consume too many resources.
  • Checking CPU usage: Identify processes using a high amount of CPU with the ps aux command to resolve performance issues.

Additionally, the ps command can be combined with other commands via the pipeline (|) for even more powerful usage. For instance, you can find and terminate a specific process by combining grep and kill.

ps aux | grep python
kill -9 PID
ShellScript

In this example, we searched for a Python process and terminated process number 3047 using the kill command. Once terminated, the screen where the process was running will display “Killed,” indicating that the process was stopped.

Figure 7. Using the Linux command ps to find and terminate a process
Figure 7. Using the Linux command ps to find and terminate a process

Precaution

While the ps command is powerful, there are some important precautions to keep in mind. Always ensure that you understand what a process does before terminating it. Stopping the wrong process can destabilize the system or interrupt critical services.

Additionally, when running the ps aux command, a large amount of information can be displayed. Use tools like grep to filter only the information you need.

Summary

The Linux command ps is a crucial tool in Linux for viewing currently running processes and is essential for troubleshooting and system monitoring. From basic usage to various options like ps aux, ps -e, and ps -u, this command can be a powerful resource when used correctly. Understanding and applying the ps command effectively will greatly assist in system management.

References

Leave a Comment