5

There is a top command to show running processes. I would like to print sorted list of unique values in field COMMAND. There is my solution for this task

$ top -b -n 1 | tail -n+8 | awk '{print $12}' | cut -d'/' -f 1 |  sort | uniq

However, this is too wordy command for so simple purpose. Is there a simpler solution for the task?

PS: I can use ps instead of top, but it returns too noisy format:chrome vs /opt/google/chrome/chrome --type=renderer --enable- features=LinuxObsoleteSystemIsEndOfTheLine<LinuxObsoleteSystemIsEndOfTheLine --lang=en-US --force-... this is just beginning of command.

6

I strongly recommend pouring over through the documentation for ps.

These might be an interesting start:

ps -eo comm=,

or

ps -eo cmd=,

You probably want the first one.

If you want absolutely fine-grained control, read the Name: line from /proc/nnnnnn/status (or parse the string between the parens in stat) where nnnnn represents all-numbers. For the full commandline, read /proc/nnnnnn/cmdline, and translate the NUL bytes in the file to space characters.

Note that ps on BSD (provided as part of that ecosystem) is a little different to Linux's version (supplied to Linux by the procps-ng project) and the two interpret their options a little differently. Worth noting if you're ever on *BSD.

  • The output with chrome and the full path etc is the commandline (cmd in ps) which shows everything while comm is the shortname out of the status procfs file. For what you want to see, comm is what you are after. The c key or the -c command line in top toggles the cmd and comm output, in case you wanted to change it there too. Note, that top remembers the toprc settings even in batch mode. This means if you toggle the comm/cmd option, save toprc then run in batch mode you get a better output. I always recommend using the -o flags in ps if you are looking for specific fields. – Craig Small Mar 16 '16 at 10:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.