2

I'm trying to export the output of the "top" command (unix) with PHP. Invoking and reading the command/output is pretty simple using the "exex" function but the question is: is the "top" command output (order, names, etc.) always the same? Or is it related to the distribution I'm using?

Is there any way to parse these information "generally" on UNIX systems?

Thanks in advance

1
  • There are a lot of really powerful, machine readable UNIX tools out there. If you describe what you're really after, you'll get a better answer. Commented Apr 1, 2014 at 17:12

2 Answers 2

2

You could use ps instead, with arguments -A and -o like so:

ps -Ao %cpu,%mem,user,comm

which would give you the output exactly like you specified when you called the command:

 0.0  0.1 root     udisks-daemon
 0.0  0.0 root     udisks-daemon
 0.0  0.1 root     gdm-simple-slav
11.0  0.4 root     Xorg
 0.0  0.1 root     NetworkManager
 0.0  0.1 root     polkitd
 0.0  0.1 root     gdm3

Arguments:

-A     Select all processes.

-o format
              User-defined format.  format is a single argument in the
              form of a blank-separated or comma-separated list, which
              offers a way to specify individual output columns.  The
              recognized keywords are described in the STANDARD FORMAT
              SPECIFIERS section below.  Headers may be renamed (ps -o
              pid,ruser=RealUser -o comm=Command) as desired.  If all
              column headers are empty (ps -o pid= -o comm=) then the
              header line will not be output.  Column width will
              increase as needed for wide headers; this may be used to
              widen up columns such as WCHAN (ps -o pid,wchan=WIDE-
              WCHAN-COLUMN -o comm).  Explicit width control (ps opid,
              wchan:42,cmd) is offered too.  The behavior of ps -o
              pid=X,comm=Y varies with personality; output may be one
              column named "X,comm=Y" or two columns named "X" and "Y".
              Use multiple -o options when in doubt.  Use the PS_FORMAT
              environment variable to specify a default as desired;
              DefSysV and DefBSD are macros that may be used to choose
              the default UNIX or BSD columns.

All STANDARD FORMAT SPECIFIERS that you could use, you can find in the man page of ps, but I've copied them also here for convenience:

https://gist.github.com/ivankovacevic/9918272

Sign up to request clarification or add additional context in comments.

1 Comment

Note that ps and top don't give the same output, ps gives the average use over the lifetime of the process, which might be useless depending on your use case. More info: unix.stackexchange.com/a/58541
-1

As mentioned by Lucas, ps is probably printing what you would expect.

If your Unix flavour is Linux, I think pidstat (from the sysstat package) is better suited for your needs (not to mention, documented in a clearer way, in my opinion).

1 Comment

ps and top gives not the similar results

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.