Getting a list of currently running processes

I'd like to get a list of all the applications currently running on my machine using the command line. It should be similar to what you get when you open up Activity Monitor and choose to save the list.

I noticed that the ps -a command does something similar, but it cuts off the names of the processes.

I'm only interested in the name of the processes, so I don't need all the columns that Activity Monitor outputs.

Update: I should have been more clear... I don't want the entire path to the process to be displayed. I just want the name of the process, as it appears in Activity Monitor. For example, ps causes lanchd to be displayed as /sbin/launchd rather than as it is displayed in Activity Monitor (i.e. just lanchd). This is a problem because processes that have very long paths end up being truncated (depending on my terminal window's size), and I can't even see the process name.


ps -axc -o comm
  • -a displays processes from all users (or root)
  • -x includes processes that don't have a controlling terminal
  • -o comm outputs command paths or names
  • -c makes -o comm output names only

tell app "System Events" to name of application processes would list only processes launched from an application bundle.


Right command, wrong flags.

  • Use ps -Ao comm if you want process names only.
  • Use ps -Ao command if you want process names with arguments.

You mentioned that you'd like to save it to a file. In the simplest case (the one that you are most concerned about), use

ps ax > /path/to/outfile.txt

which will print a list of all running processes (with the full name) to the file designated by the path. The ">" redirects the printed output (in overwrite mode) to the file. Simply issuing ps ax will print it to the screen.

Hint: If you don't feel like typing out a full directory, drag a folder to the terminal to get the path for you and manually complete with the name of the file

In case you end up wanted some of the other columns at some point... Also, check the man page for ps by typing man ps at the command line and look at other options. Here's another example which prints the cpu-usage and memory for each process (in percentages) followed by the command (process):

ps ax -o %cpu -o %mem -o command > /path/to/outfile.txt

Use

# Sort by cpu usage with the 'r' option
ps arx -o %cpu -o %mem -o command > /path/to/outfile.txt

# Sort by memory usage with the 'm' option
ps arm -o %cpu -o %mem -o command > /path/to/outfile.txt

EDIT

In order to print just the command, there are probably more durable approaches, but in a pinch I'd just do what follows. You'll probably notice a bit of cruft near the end related to strings that weren't meant for basename (like -bash), but the rest of what you want will be there with this quickie.

ps ax -o comm | xargs -I % basename % > /path/to/outfile.txt

NOTE: Some processes that are running are not necessarily executable names. They could be a straight call to sh with a string of commands that follow.