1

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?

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

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
0
0

A resounding ps -eco comm

To wrap that good in double-quotes, here is this regex monster:

ps -eco comm | sed "s/.*:......//" | sed "s/^/\"/" | sed "s/$/\"/" | grep -v COMMAND

Took me about a day or two to write, and had bugs when using ps axc until @wildcard showed me the light of -eco comm. I'm planning to create a task killer with it one day I will call it MadsKills. Anyhow remove the last two pipes to sed to get rid of the quotes.

On my system this gives:

"(mysqld)"
"-zsh"
"iTerm2"
"login"
"-zsh"
"ssh"
"-zsh"
"ps"
"sed"
"sed"

Works great on macOS and linux also.

7
  • 1
    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 '19 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 '19 at 3:07
  • 2
    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 '19 at 6:00
  • Or use ps axc | sed -n 's|^.*:..\... \(.*\)|"\1"|p' for hacky, without requiring grep -v :-) – nohillside Jul 30 '20 at 11:21
  • thanks @wildcard I have incorporated your idea and also added grep to remove COMMAND from the list. – Tomachi Jul 30 '20 at 11:23

You must log in to answer this question.

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