I have SWI Prolog working in my vm where I'm going to be working on a PHP/HTML website. I've been following this guide: http://www.j-paine.org/dobbs/prolog_from_php.html#appendix to set up my php file to receive the output from the executed prolog file. The problem that I'm having is that it seems that the output from SWI's command line (which I'm executing from my bash terminal) isn't being captured as a standard output that I can get PHP to recognize. So, in my terminal, when I execute SWI and a test prolog file, it writes out like I need it to, but I can't figure out how to capture that output to then use in PHP.
Any ideas or suggestions would be really great. Thanks!
Related
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
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?
I have a python script, which I am calling from the php file.
My output is coming as expected, when run as python test.py
But when I am trying to run the php file through the browser, I am not getting the output.
However, on commenting the following lines, I am able to get some results. But I want these lines to run.
with open(filen,'w') as f:
f.write(str("write date"))
I am not sure, what's happening. Can anyone explain what difference its making when php reaches these lines.
Thanks
the question is not clear. I'm guessing you want to execute a python script from php. Your python script does not output anything but write to a file.
executing a python file from php
<?php
print shell_exec("python test.py");
?>
this question may help you if the code above doesn't work: php shell_exec() command is not working
please edit your question for a more accurate response.
I tried to execute a .jar using HTML or PHP.
In the first case I wrote the code below:
<applet code=Diagnostica.class
archive="Diagnostica/Diagnostica.jar"
width="120" height="120">
</applet>
This way doesn't work cause the file Diagnostica.jar need to contain an applet I suppose.
So I tried the second option in PHP:
exec("java -jar Diagnostica/Diagnostica.jar");
This way it works. Diagnostica.jar starts outside the browser, but I want to start it inside the browser.
How can I do?
If that file does not contain an Applet, I do not think you will be able to start it in the browser (have a look here).
Executing it via exec should work like executing it via the command line - but as you already noticed, it will be run on the server - not the client.
If it should run on the client, you'll either have to use a Java Applet or let the user download the jar-executable and execute it via command line etc.
I am trying to run a php script in the background on windows.
Basically my goal is to run a exe file in the background and receive a signal when its done.
I'm open to any suggestions.
Thanks.
make a .bat file and on that bat file reference php file then it will work
if your using linux environment, i suggest creating a bash file and put your commands.
and if your using windows environment, use .bat file and put your commands.
php filename.php <arguments>
php another_filename.php <arguments>
and then run in in the terminal.. for the signal, you can exit() your php script based on what the condition your script needs..
UPDATE
Regarding your comment, if you want the webpage to create an executable then you must use the backtick or exec() in your web php file
for example, in your web php file:
//other php codes
echo `php runthisfile.php`;
echo exec('php runthisfile.php');
//other php codes
then if this is encountered, in the background, it will run the runthisfile.php file.. the backtick is a special symbol in php and exec() is a function that enables you to execute command line instructions..
and regarding the signalling, there are many approach.. some are:
in the background file, you can store a value in the database that the web page is listening to
make a soap call that uses xml as its data medium
or if what I think is right, just use AJAX in your web.. it really is helpful
My aim is to create a log file reader using PHP and Shell scripting.
I plan to run the script at intervals in order to detect the last time a particular entry shows up in the logfile.
The way it should work is by calling the PHP script which in turn calls a shell command
tac logfile.log | grep "what i am looking for" | head -n 1
or
tac logfile.log | head -n 1
What this script will do is:
in the case of the first script: scan the log file from the bottom up until it finds what it is looking for and shows me the first line, which is actually the last occurrence of the line in the file.
in the case of the second script: scan only the last line and output it. I am NOT trying to tail the file.
So my intention is to scan the file from the end to the last occurence but I need php to do this because PHP can parse the lines it reads from the shell output and parse them easily.
The issue i am trying to resolve is that once I run the script from PHP using the functions shell_exec, exec, backtick operator, system etc. They all keep the PHP script running or output the result and keeps the first part of the shell running (tac logfile.log).
I dont understand why it works perfectly from CLI but runs indefinitely by trying to tac the whole log file when I run the same script from PHP. Ive noticed that tac and cat give this problem. Sometimes it shows the error "Broken pipe".
What should I be doing? How can I fix this?
You could use pure PHP. Open the the file with fopen, an example how to read a file backwards can be found at Read a file backwards line by line using fseek
than just break the loop after you found your string you search for.