title: Running a python script from php with system( ) or even passthru( ) produces no output. Why?
body: <p>I need to run a python script (a log parser) on hundreds of log files, but I'm a PHP guy so I figured I'd write a little PHP script to grab the list of files from a directory and call the python script dynamically in a foreach loop.</p>
  
  <p>I've set variables in my PHP script using the full system paths to the python binary, the full path to the python script and checked that everything seems correct. I echo the output of the script I'm trying to run to check it over:</p>
  
  <pre><code>&lt;?php
  // batch.php (modified for SO post)
  $python = '/usr/bin/python';
  $script = '/mnt/data/scripts/the/python/script.py';
  // $folder contains the full system path to the dir containing
  // the files I need to pass as args to script.py
  $files = scandir($folder);
  foreach($files as $file){
      if($file=='..'||$file=='.')
      {
          continue;
      }
      $system = $python.' '.$script.' '.$folder.$file.' 2&gt;&amp;1';
      echo "Running ". $system ."\n";
     // I also tried passthru( )
     system($system);
  }
  </code></pre>
  
  <p>Next I do</p>
  
  <pre><code>php batch.php
  </code></pre>
  
  <p>All I get is the first line from PHP:</p>
  
  <pre><code>Running /usr/bin/python /mnt/data/scripts/the/python/script.py /path/to/data/file/one.log
  </code></pre>
  
  <p>I can copy, paste and run the output echoed in shell (after 'Running ') directly with python and there's my output, no problem, so I know the PHP script has no syntax problems.</p>
  
  <p>But when running the php script wrapping it, it produces no output other than my echo( ) statement from PHP. It just hangs there (I am thinking that my long-running Python script is actually working, but I'm not sure how to tell.) There's nothing in the error log, and the script never exits until I Ctrl-C.</p>
  
  <p>I've seen a lot of discussions about exec( ), system( ) and passthru( ) and from what I can tell I should be seeing output using system( ) but for some reason I'm not. </p>
  
  <p>I even tried to </p>
  
  <pre><code>root:~# ps aux | grep php 
  </code></pre>
  
  <p>and then </p>
  
  <pre><code>root:~# strace -p &lt;process_id&gt; 
  </code></pre>
  
  <p>of the PHP script, but all I get is</p>
  
  <pre><code>root:~# strace -p 14232
  Process 14232 attached - interrupt to quit
  read(4, 
  </code></pre>
  
  <p>Note: I added the 2>&amp;1 bit from <a href="https://stackoverflow.com/questions/8704519/php-script-cant-get-output-from-python-script">this question</a> but it didn't help; that references Apache however I'm running PHP on the CLI.</p>
  
  <p>Note: </p>
  
  <pre><code>root:~# echo $PYTHONPATH
  </code></pre>
  
  <p>in shell produces no output. </p>
  
  <p>What am I missing?</p>