Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am getting strange output including my old makefile output and process tree when running below command on linux

ps axuwfw

Also, what is the usefulness of the following command and how does it works?

ps axuwfw | grep -v grep | grep -i screen

ps axuwfw | grep -v grep | grep -i tmux

The above two commands return 431 and 7 in the example from here, but in my system it returns some previously ran older makefile output and truncated process tree.

share|improve this question

1 Answer 1

up vote 6 down vote accepted
ps axuwfw

This is a combination of several BSD-style options (all of which are in the manual page, man ps):

  • a: include processes from other users
  • x: include processes with no terminal
  • u: show user-oriented information fields
  • w: wide output (132 columns instead of 80)
  • f: "forest": tree view of processes
  • w: even wider output (no limit at all)

In other words, it simply shows currently running processes.

However, if you see old processes there, check their "STAT" field – if it says Z, the process has exited but its parent hasn't "reaped" it yet; the process is a "zombie". This usually indicates that the parent process is either slightly buggy, or has hung/crashed.

Also, note that not all operating systems accept these options. On SysV it might be ps -ef.

ps axuwfw | grep -v grep | grep -i screen

The | is a pipe. It redirects one command's output to the other's input. This should make it more obvious that the above is not just one weird magical command; it is three commands connected.

  1. The first command, ps axuwfw, was already explained.

  2. grep, is for searching/filtering text. grep foo would only print input lines that contain the text "foo". With the -v option, the second command – grep -v … – does the opposite – prints lines that do not match. (Again, refer to man grep or grep --help.)

    This particular invocation is necessary because all processes in a pipeline run at the same time, so the ps… would show all grep… processes – and ps axu | grep screen would cause the grep… to also match its own entry in addition to the desired ones. So a second grep… is added to explicitly ignore lines that have "grep" in them.

    (Of course, there are better tools, like pgrep -alf screen, but many people still use this kind of ps|grep|grep… pipeline just because it's easy to combine knowing just ps and grep.)

  3. The third command, grep -i screen, again just extracts lines that contain "screen".

    As described in both the manual page and the grep --help output, adding the -i option makes the search case-insensitive, so it also outputs lines that have "Screen" or "SCREEN" in them.

In short, the full command just prints those ps… lines that talk about the "screen" program.

"The above two commands return 431 and 7 in the example from here…"

They don't. They return information about processes named "screen" or "tmux". The number given in the example is the count of processes returned.

The slideshow's author didn't include the full output (which isn't really relevant); they only included the total counts as a summary, demonstrating that there are 60 times more "screen" users than "tmux" users.

If you want the command to actually count lines for you, you can a) add the -c (count) option to the last grep… command, or b) pipe it through | wc -l. (Either works.)


"…but in my system it returns garbage."


It returns some previously ran older makefile output and truncated process tree

That's not garbage – it's exactly the output you asked it for.

share|improve this answer
    
This answer is really informative. The information about the STAT field in ps output is really new for me. Thanks! –  Manav Apr 25 at 9:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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