2024-02-12
psThe ps command displays information about currently running processes. Its output can be customized extensively using various options. The simplest usage is just typing ps into your terminal, which provides a basic snapshot of your current processes. However, this often isn’t enough for detailed analysis.
psThis will likely show a limited set of information, including the process ID (PID), the terminal associated with the process (TTY), and the command.
ps Output with OptionsTo get more detailed information, you need to utilize ps’s numerous options. Here are some:
-a (or -e): Shows all processes. -a shows processes for the current terminal, while -e displays every process running on the system.ps -a
ps -e-f (full format): Provides a more detailed view, including the process’s parent process ID (PPID), session ID (SID), and more.ps -f-u [username]: Shows processes owned by a specific user. Replace [username] with the actual username.ps -u john-x: Displays processes without a controlling terminal. This is useful for finding daemons and background processes.ps -x-p [PID]: Shows information about a specific process given its PID.ps -p 1(Note that PID 1 is typically the init process, the ancestor of all other processes.)
The real power of ps comes from combining these options. For instance, to see all processes running as the user ‘john’ in a full format:
ps -fu johnTo list all processes, including those without a controlling terminal, in full format:
ps -efFurther customization is achievable through piping the output of ps to other commands like grep (for filtering) and sort (for sorting):
To find all processes related to the ‘firefox’ browser:
ps aux | grep firefoxTo sort all processes by CPU usage (requires the -o option and the %CPU field):
ps -eo pid,%cpu,%mem,cmd --sort=-%cpu | head -n 10This command shows the top 10 CPU-consuming processes. The --sort=-%cpu sorts in descending order of CPU usage.
ps OptionsBeyond the options covered, ps offers many more for fine-grained control. Consult the man ps page for a complete list and detailed explanations. Remember to look at and experiment to understand how ps can best serve your system administration needs.