ps

2024-02-12

Understanding the Basics of ps

The 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.

ps

This will likely show a limited set of information, including the process ID (PID), the terminal associated with the process (TTY), and the command.

Refining Your ps Output with Options

To get more detailed information, you need to utilize ps’s numerous options. Here are some:

ps -a
ps -e
ps -f
ps -u john
ps -x
ps -p 1

(Note that PID 1 is typically the init process, the ancestor of all other processes.)

Combining Options for Powerful Queries

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 john

To list all processes, including those without a controlling terminal, in full format:

ps -ef

Sorting and Filtering Your Output

Further 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 firefox

To sort all processes by CPU usage (requires the -o option and the %CPU field):

ps -eo pid,%cpu,%mem,cmd --sort=-%cpu | head -n 10

This command shows the top 10 CPU-consuming processes. The --sort=-%cpu sorts in descending order of CPU usage.

Exploring Other Useful ps Options

Beyond 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.