By running ps aux I can get list running processes with command associated with them, e.g.

USER               PID  %CPU %MEM      VSZ    RSS   TT  STAT STARTED      TIME COMMAND
_windowserver      142   2.0  1.3  4559968 224672   ??  Ss    7:11AM  33:21.95 /System/Library/PrivateFrameworks/SkyLight.framework/Resources/WindowServer -daemon
lrogalsk          6419   1.9  0.5  2703580  77588   ??  S     2:15PM   0:40.88 /Applications/Utilities/Terminal.app/Contents/MacOS/Terminal

Unfortunately, COMMAND includes command line arguments and absolute path of file, both of which may contain spaces. How can I get process name in deterministic manner?

  • 2
    Did you look at man ps? – nohillside Aug 29 '17 at 14:32

Apparently I'm blind.

 -c      Change the ``command'' column output to just contain the executable name, rather than the full
         command line.

ps auxc works just fine.

USER               PID  %CPU %MEM      VSZ    RSS   TT  STAT STARTED      TIME COMMAND
lrogalsk         18670  15.6  1.4  3913100 241624   ??  Ss   12:12PM   0:09.93 com.apple.WebKit.WebContent

To see a list of process names only, wrapped in quotes, try this:

ps axc | sed "s/.*:......//" | sed "s/^/\"/" | sed "s/$/\"/"

Took me about a day or two to write. I'm planning to create a task killer with it. Remove the last two pipes to sed to get rid of the quotes. On my system this gives:

  • That’s painfully hacky. If all you want is the process name, use the appropriate flags per “man ps” to output only the process name. – Wildcard Mar 5 at 6:29
  • @wildcard this is macos - what flags are these to only show process name? So I can send to killall – Tomachi Mar 7 at 3:07
  • Look at the man page. ps -co comm or ps -eco comm are some starting points. But also, you don't need to write a task killer. Just kill the task you want to kill. Also see pkill and pgrep. – Wildcard Mar 7 at 6:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .