Faqts : Business : Programming : Shopping For You : PHP : Common Problems : Tips and Tricks

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

15 of 15 people (100%) answered Yes
Recently 8 of 8 people (100%) answered Yes

Entry

How can I get the results of a unix command into PHP?

Jun 16th, 1999 07:00
Nathan Wallace, ZioBudda, Richard Lynch, Christian Schmidt


For example, to see which ipop3d users are online:
    ps uax | grep ipop3d

1.  Make sure your Apache user ('www' or 'nobody' or whatever) can
execute ps aux and grep (and |, I guess), and that those functions are
in the path (PATH) for that user.

2.  You can run the command in a couple of different ways:

Use popen() and fgets().
http://php.net/manual/html/function.popen.html

OR

Something like this:

<?php
  $cmd = 'ps aux | grep ipop3d'
  $line1 = exec($cmd, $results, $error);
  if ($error){
    echo "'$cmd' resulted in Unix error code: $error<BR>\n";
    exit;
  }
  while (list(,$line) = each($results)){
    // do something with each result line here
  }
?>