I have been using the Unix ‘ps‘ command for years to view my system processes. For some reason, I always forgot which command line switch to use. This morning, when Quicksilver locked up and my system is currently backed up so my MacBook ran like molasses in the winter. Not able to fire up Activity Monitor, I turned to my terminal and issue the following command:
ps -aux | fgrep -i quicksilver
To my disappoinment, the command combo did not return a single line. Simply speak, the ps command gave me a list of processes; then, the fgrep command picked out just the line for the Quicksilver process. So, what went wrong here? The culprit was in the flags I used. My terminal is limited to a traditional 80-column screen, but the -u flag displayed the list in long format and truncated part of the processes’ names. As the result, the name Quicksilver did not show up in the list at all. Clearly, this is a case where I incorrectly used the flags, so I made a point of learning a couple of flags from the ps(1) man page to make it right. After a couple of minutes, I succeeded with the following command:
ps -cx | fgrep -i quicksilver
The new ps flags made a big difference. Finally, I was able to locate the process ID (pid) of Quicksilver and kill it off. Let me explain some of the ps flags.
The -c flag
This flag tells ps to display only the name of the process instead of the full (lengthy) pathnames. It is the key to prevent ps from clipping text beyond the 80th column limit.
The -x flag
This flag tells ps to display all the processes that do not associate with any terminal. My translation: almost all the processes that I care about. Without it, ps only displays a handful of processes, not very useful.
The -o flag
This flag tells ps which column to display. The default output may show more than what I care to know. By using the -o flag, I am able to eliminate irrelevant columns. For example, to display just the process ID and the process name, I issue the following command:
ps -cx -o pid,command | sort -f -k 2
The -k 2 flag of the sort command express our wish to sort the second column (the process name). Meanwhile, the -f flag tells sort to perform case-insensitive sorting. For a list of column names, consult the man page for ps(1).
In summary, flags make a big different on how ps (and other commands) perform. Learn to use them correctly and you have a powerful tool in hand. Otherwise, your command line journey will be full of grievances.