The ps command is one of the Linux process management commands. It’s main task is to help with displaying the information related to running processes, but not in real-time. With the ps command, there are numerous flags that you can use to extract different information about the running processes and this post will give 20 real life examples to get you started.
For each flag, we will give an example and display the output of running the command. The aim is to ensure you understand what each flag does. Let’s begin!
20 Real Life Examples of Using the Linux ps Command
As mentioned, the Linux ps command is handy in retrieving information about the running processes in Linux. To ensure you get comfortable using it, go through each presented example below to see how you can use its different flags.
1. The ps Command Without Arguments
The obvious way of using the ps command is to run it without any arguments. Doing so will display the running processes for the current shell.
2. Viewing All the Running Processes
By adding the ps -A or -e flags, your output will contain all the running processes.
3. Exclude Processes Associated with the Terminal
The ps -a flag lets you view all the processes without including those associated with the terminal.
4. View All Current Processes
You can use the ps command to display all current processes by adding the -ax flag in your command.
5. View All Processes in BSD Format
Sometimes, you may want to view all the running processes but using the BSD format. In that case, you will execute the below command.
6. View a Full Format Listing
When you want the output displayed in a full format listing, you will require to add the -ef or -eF.
7. View Processes for a Particular User
When you run the ps command, the default behavior is that, it will display all the running processes for all the users. However, you can filter the result to only show the processes associated with a given user by using the -u flag.
Assuming we want to only view processes associated with user, ‘linuxmeta,’ our command will be as follows.
8. Display Processes Based on their Thread
If you want to get information about the thread of a given process, you can specify its PID to retrieve the information. Below is an example.
9. Get All Processes Running as root
We’ve already seen how you can specify to only view the processes running for a particular user. In the case of root, you can view all the processes with the command below.
10. View Group Process
You can also specify to display information about the running processes based on their group. Specify the group’s name as shown below.
11. Using the ps Command with grep
There are several instances when you may need to use the ps command with grep. For instance, if you want to filter the running processes where Firefox is involved, you would run your command as follows.
12. Get Process PID using the ps Command
To retrieve the process ID of a process, you can use the -C flag with the ps command and the output will give the PID as the first column in the output. Ensure you also specify the target process for which you want to get its PID.
13. Viewing Processes Using their PID
When you know the PID of a process, you can use it to retrieve the details of that process.
14. Specify What to Display in the Result
With the ps command, you can specify what metrics to include in the output. For this example, we want to display the PID, username, command’s name, and the memory usage.
15. Rename the Columns
The previous example demonstrates how you can specify what columns to include in your result. For this case, we’ve demonstrated how you can rename the columns you wish to display.
16. Show the Elapsed Time for a Process
You can include the elapsed column in your output to show how long each displayed processes has been running.
17. Display Child Process
This example demonstrates how you can display the child processes for a parent process. For our case, we’ve specified to include the PID, user, memory usage, and the name of the command in the output.
18. Viewing Process Hierarchy
When you want to see how processes are forked, you can get the information of the parent and child by displaying them in a hierarchy format. We’ve used systemd for our example.
19. Memory Leakage
Still, with the ps command, you can view the memory leakage of different processes by adding the sort option with the pmem flag.
20. Display Process Threads
To get the details about the threads alongside the target process, specify the -p and -L flags as shown below.
Conclusion
The Linux ps command is very handy for managing your processes. With the command, you can view the running processes and combine the results for different activities depending on your needs. Reference the provided 20 real life examples to understand how you can use the Linux ps command.