Pro Terminal Commands: 10 Uses for ps on macOS


By Jamie Cox from Melbourne, USA (Zenith Z-19 TerminalUploaded by Mewtu) [CC BY 2.0], via Wikimedia Commons

For system administrators, ps on macOS is a frequently-used tool. The command stands for “process status,” and that’s largely what it does. It reports currently-running processes with a variety of filters and views. Run the command with no flags, and it will show only the processes associated with the currently-running terminal. Start adding additional flags, and you can see different sets of processes from across the system and owners. That’s what makes ps on macOS so useful.

1. Show All Processes

ps -ef

Shows all running processes with full data about every process. This data includes columns showing UID, PID, parent PID, recent CPU usage, process start time, controlling terminal, elapsed CPU usage, and the associated command. To show the usernames associated with these processes, add the -u flag to your command. You can also replace the -e flag with -A, which has an identical effect.

2. Filter by User

ps -U userName

Filters ps results to only show processes owned by the specified username. Can also be used with a lower-case u to search by UID instead of username.

3. Get a Process PID By Name

ps -e | grep -m1 processName | awk '{print $1}'

Searches for a specified process name and returns only the PID of that process. The search is not case sensitive, but all process names are in lower case at any rate.

4. Filter by Process ID

If you know the process ID of the running process you want to show, you can filter for it specifically with the -p flag. This can take multiple PIDs as arguments, separated by a single comma and no space.

5. Grep within Results

ps -ef | grep worker

If you want more flexibility to search within the results from ps, you can pipe the results to grep. While this is more of a combination of commands than a pure ps command, it’s a regular part of any administrator’s tool belt. With grep, you can search using regular expressions for pattern-matching and more.

6. Display Specific Columns

ps -e -o pid,user,pcpu,pmem,comm

The -o flag sets specific output display options for the ps command’s results. Run ps -L or man ps to see a full list of standard display options for ps. Specifying specific columns may prevent long lines from wrapping. Run with the -w flag to use the expanded character line length.

7. Sort Processes by Usage

ps -er -o pid,pcpu,comm

Sorts commands by CPU usage instead of combined terminal and PID. This command also uses the -o command to display specific columns, which is not strictly necessary for sorting. To sort by memory usage, use the -m flag instead of -r.

8. Rename Column Headers

ps -e -o pid=Process,ruser=RealUser,comm=Command

When using the -o command to create a user-specified output appearance, columns can be renamed. Add an = (equal) sign and the desired name, using a -o flag for each renamed header. Headers can also be hidden in specific columns by leaving a blank after the equal sign. You can mix and match with renamed and default name columns. Just be sure to use a -o flag for each renamed column as shown below:

ps -e -o pid,pcpu=CPU -o pmem=RAM,comm

10. Show All Root Processes

ps -f -U root -u root

Execute a search for all processes running with real and effective root identifications. This shows them in the full-length format thanks to the -f flag. You can combine it with the -o flag to customize output.

Conclusion

While the UNIX style flags fit in better with other Terminal commands, the BSD commands can display information in different and, sometimes, more useful formats. If you’re interested in learning about the BSD-style flags, check out the ps man page.

You might also like the following posts:

Pro Terminal Commands: Use fd on macOS to Find Files

Getting Started with Terminal: Install and Use wget

Getting Started with Terminal: Hide Files on macOS

 


Like it? Share with your friends!

Alexander Fox

0 Comments

Your email address will not be published.

Send this to a friend