The ps command is a fundamental utility in Linux and
Unix-like operating systems used to display information about the
currently running processes. It provides a snapshot of the system's
active processes, allowing users and administrators to monitor system
performance, identify resource-intensive applications, and
troubleshoot issues. Understanding how to effectively use the
ps command is crucial for system management and
debugging.
To get a comprehensive view of all processes running on the system,
including those not associated with a terminal, use the
aux options:
ps aux
This command displays processes for all users (a),
including those without a controlling terminal (x), and
shows processes in a user-oriented format (u).
Visualizing the parent-child relationships between processes can be
very helpful. The axjf options provide a process tree
view:
ps axjf
This format shows the process hierarchy, making it easier to understand how processes are spawned.
You can list processes belonging to a specific user or a set of users. To list processes owned by a particular username:
ps -aufoouser
To display processes for a list of usernames:
ps -f -u username1, username2, .... ,usernameN
The -f option provides a full-format listing.
Filtering by Process ID (PID) or Parent Process ID (PPID) is useful for targeting specific processes or their children.
To display processes with given PIDs:
ps -f -p 25001, 4567, 789
To display processes with a particular parent ID (e.g., 5589):
ps -f -ppid 5589
The ps command allows for highly customized output. You
can specify exactly which fields you want to display:
ps -eo pid,user,command
This command will only show the Process ID, User, and Command for each process.
To quickly see all processes owned by the currently logged-in user:
ps -U $USER
Sorting processes by resource usage, such as CPU or memory, is essential for performance monitoring. To sort processes based on memory usage in descending order:
ps aux --sort pmem
You can also sort by CPU usage using pcpu.
To view the environment variables for each process, you can set the
COLUMNS environment variable to a large value and use the
axel options:
COLUMNS=10240 ps axel
This provides detailed information about each process's environment.
The ps command is a powerful tool for understanding and
managing processes on your system. By mastering these common options,
you can significantly enhance your ability to monitor and maintain
your Linux environment.