php echo to elixir iex stdin - php

ok, I know I'm doing something weird here but I gotta do it.
So my elixir program needs to run one PHP script and take the information from that script, but it never comes in. I can manually run the php script and it works fine. At the bottom of the script it says:
echo $avar->getPUId();
So it's printing it out to echo I guess. I get the data I need when I run it from a command line:
C:\PHP>php.exe "-f" "C:\pap.php" "613b8859"
37a69912
But when I run it from iex I get nothing:
iex(1)> System.cmd("C:\\PHP\\php.exe", ["-f", "C:\\pap.php", "613b8859"])
Terminate batch job (Y/N)? y
Now, if I mess up on the filename or something php will give me an error in response saying like:
iex(1)> System.cmd("C:\\PHP\\php.exe", ["-f", "C:\\pap321.php", "613b8859"])
{"Could not open input file: c:/pap123.php\n", 1}
...
so I know I can talk to PHP, and it can talk back to me with errors, but echo isn't caught by my elixir application that calls it apparently. Is there any other way I can grab the data as it comes out of the PHP script?

I used
https://github.com/alco/porcelain
result = Porcelain.shell("C:\\PHP\\php.exe -f C:\\pap.php 613b8859")
IO.inspect result.out
it worked great.

Related

Better way to run python & display python errors from PHP

I am running a webserver for my laboratory that basically has a bunch of scripts I wrote in python for processing and analyzing tabular data.
I have a DigitalOcean droplet with a Laravel application deployed on it. When I want to run a script, I have the user upload some data file, and then from the PHP controller run:
shell_exec(python my_script.py arg1 arg2 etc);
The problem is, there are differences in dependencies and libraries between my development environment and 'production' environment. As such, when I try to run the script from the webserver and there is a python error, the object returned by shell_exec is just null. When the PHP blade template tries to parse/get data from this object, I get an error like so:
In this case, 'matchCount' is just a variable stored within a python list like this:
#Label peptides we found experimentally but do not have an in silico match for... as to predict contaminants
output = {
'sequence': protSeq,
'peptides': pepList,
'observablePeptideCount': str(len(pepList)),
'possibleObserved': possibleObserved,
'matchCount': matchCount,
'coverage': matchSumAA/protSeqAALength*100,
'massList': massList,
'tolerances': tolerances,
}
output = json.dumps(output)
The problem is, I understand the python script failed somewhere, but the error log does not give any indication of why or where. Is there some way I can have the webpage output the python error so I can correct it in the production environment?
Is there a better way to be doing all of this? Thank you for any help.
I won't recommend you print things out in production, but if it's a last resort you can try this:
<?php
$output = '';
$result = '';
exec('python my_script.py arg1 arg2 etc 2>&1', $output, $result);
var_dump($output); // all the output is here
var_dump($result); // gives an exit code, might be 1 if it's error

Running script and executing PHP for output

I'm looking to run a program, and for every output line it generates, execute a PHP script and pass the line content to it.
I know, pretty hard to understand. Here's an example:
Execute script -> script outputs 'Initializing script on 127.0.0.1'. Now it needs to execute a command like php5 input.php 'Initializing script on 127.0.0.1'.
Is this doable? If so, how would I go about doing this?
Edit: to clarify; I basically want command > log.txt but in stead of writing the output to that file, writing it to a PHP script as an argument
PHP is an interpreter much like Bash, Python, etc, so you can do "normal" scripting with it. For example:
#!/usr/bin/php5
<?php
echo "Hello, world!\n";
while($line = fgets(STDIN)) {
echo "> " . $line;
}
?>
Mark the file as executable, then run:
$ /program/that/generates/lines | /path/to/your/php/script
However, contrary to your original question, it sounds to me like you actually want to use JavaScript and possibly AJAX for web purposes. Sane web applications will have the said script run in the background and safely write the results to a file or stream, using AJAX to read it and update the information on the current page.

Read linux console PHP

I want to read the console logs of a process that is running, I currently have this
$output = shell_exec('pgrep tfs');
echo "<pre>$output</pre>";
And that returns me a random number like 34034
I've almost never worked with linux before so I dont really know how to do this.
You do realize that that's what pgrep does, right? It returns the process ID of the process you're asking about. So that's not a random number you're getting back. Your script is working perfectly.
Why don't you find the actual log file itself (usually in /var/logs) and use something like fopen to open and parse it?

Printing / Echoing To Console from PHP Script

I'm debugging a PHP script that runs a couple SQL queries and emails a set of users. I'm sure this is a very basic thing, but every time I try to echo, print, or print_r it doesn't appear while running the script.
So say I have this in the script:
print("This should print");
echo "on the command line";
When I run the script via command line php script.php it doesn't actually print anything to the command line while running the script.
Is there a way of having PHP print to console? I feel like I'm missing something extremely basic here.
Thats the method of doing it.
Things to check for are output buffering
http://php.net/manual/en/function.ob-flush.php
Is that code actually being run ? Make sure it doesnt branch before it gets there
A good approach could be:
function log($message)
{
$message = date("H:i:s") . " - $message - ".PHP_EOL;
print($message);
flush();
ob_flush();
}
It will output to your terminal each line you call. Just use :
log("Something");
log("Another line");
The following code working fine for me.
<?php
print("This should print");
echo "on the command line";
?>
with tags.
I know it is old, but I am going post my own solution to this just in case if someone would run into similar situation. I had a problem that a legacy command line PHP script wouldn't print or echo anything to the terminal when DSN is incorrectly configured and the script got stuck for very long time (not sure how long, never waited for it to terminate by itself). After putting ob_end_flush() in the entry line of the script, the output came back to the terminal. So it turned out that all output was buffered and since the script stuck at some point, the output stayed buffered and so never went to the terminal.

issuing things to shell and forgetting about it in php

how do you issue commands to the shell and then forget it's output and such? for example:
<?php
echo `sleep 2;echo hi`;
echo "foo";
?>
the result for this is hifoo. i would want a result that gives me foohi. why? i want the command issued to the shell simply issued and forgotten, i am confused about why PHP will wait for the result. is such a result possible?
(the idea behind this is setting up the correct number of selenium grid RC instances programatically. currently, it will stop after the first process is opened)
From php.net exec()
If a program is started with this
function, in order for it to continue
running in the background, the output
of the program must be redirected to a
file or another output stream. Failing
to do so will cause PHP to hang until
the execution of the program ends.
The same applies for all shell commands.
It's like anything you do at the command prompt, unless you take measures to put the new command in the background, the shell (and PHP ) will block until the command exits. Try this:
<?php
echo `(sleep 2; echo hi) &`;
echo 'foo';
?>
Note the brackets around your command, without that, the & woulud apply only to the echo , and you'd still have the 2 second pause.

Categories