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.
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:
<?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>&1';
echo "Running ". $system ."\n";
// I also tried passthru( )
system($system);
}
Next I do
php batch.php
All I get is the first line from PHP:
Running /usr/bin/python /mnt/data/scripts/the/python/script.py /path/to/data/file/one.log
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.
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.
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.
I even tried to
root:~# ps aux | grep php
and then
root:~# strace -p <process_id>
of the PHP script, but all I get is
root:~# strace -p 14232
Process 14232 attached - interrupt to quit
read(4,
Note: I added the 2>&1 bit from this question but it didn't help; that references Apache however I'm running PHP on the CLI.
Note:
root:~# echo $PYTHONPATH
in shell produces no output.
What am I missing?