1

I have a problem displaying the results of a Perl script that I am calling from my PHP webpage. The Perl script constantly monitors a socket and will display the output of this when run from the command line and also saves the output to a file. I know the Perl script is being called and running successfully as the text file is being updated but I do not get the output on the webpage as I was hoping for.

I have tried using the system(), exec(), passthru() and they all allow the Perl script to run but still with no output on the webpage so I am obviously missing something. Am I using the correct functions? Is there a parameter that I need to add to one of the above to push the output back to the webpage that calls the Perl script?

One example of what I have tried from the PHP manual pages:

<?php
exec('perl sql.pl', $output, $retval);
echo "Returned with status $retval and output:\n";
print_r($output);
?>

Edited to include output example as text instead of image as requested.

# perl sql.pl
Connecting to the PBX 192.168.99.200 on port 1752
 04/07 10:04:50             4788         4788 3256739                T912                                  200   2004788                                 A2003827 A
0

1 Answer 1

2

I'm no PHP expert, but I guess that exec waits for the external program to finish executing before populating the $output and $return variables and returning.

You say that your sql.pl program "constantly monitors a socket". That sounds like it doesn't actually exit until the user closes it (perhaps with a Ctrl-C or a Ctrl-Z). So, presumably, your PHP code sits there waiting for your Perl program to exit - but it never does.

So I think there are a few approaches I'd investigate.

  • Does sql.pl have a command-line option that tells it to run once and then quit?
  • Does PHP have a way to send a Ctrl-C or Ctrl-Z to sql.pl a second or so after you've started it?
  • Does PHP have a way to deal with external programs that never end? Can you open a pipe to the external process and read output from it a line at a time?
Sign up to request clarification or add additional context in comments.

2 Comments

Interesting, I think you are correct in saying the PHP is waiting for the script to exit which it is not doing as it is a constant connection to the host, I didn't think of that. I will look into the "open a pipe to the external process" as I need the Perl script to maintain its constant connection or I will lose data. Thanks for your suggestion Dave!
@GerryArmstrong: It looks to me like you want popen.

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.