I am trying to create a C program compiler + executer using PHP.
My PHP code is
<?php
$cwd='/path/to/pwd';
$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("file", "/path/to/log/file", "a") );
$process = proc_open("./a.out", $descriptorspec, $pipes, $cwd);
fwrite($pipes[0], 123);
fclose($pipes[0]);
echo stream_get_contents($pipes[1]);
fclose($pipes[1]);
My C file is
#include <stdio.h>
int main() {
int a;
printf("Enter a number");
scanf("%d", &a);
printf("The value is: %d", a);
return 0;
}
Now when I run this, I am getting the output
Enter a numberThe value is: 123
Is there anyway to do it like a terminal execution? So it should wait for the input after showing Enter a number. Then once I enter the number it will execute further and show the output.
It will be like a web site with a terminal.
Will it be possible with proc_open and PHP? Is it possible to achieve by using Web socket in some way?
Please help. Thanks in advance.
Related
To my PHPeoples,
This is sort of a weird PHPuzzle, but I'm wondering if it's PHPossible.
The end goal is the equivalent functionality of
mysql mydb < file.sql
But with an API like this
./restore < file.sql
Where restore is a PHP script like this
#!/usr/bin/env php
$cmd = "msyql mydb";
passthru($cmd, $status);
However, I want to pass STDIN to the passthru command.
The clear benefit here is that I can put restore somewhere in a pipeline and everything works peachy. Here's an example
# would be pretty awesome!
ssh $remote "msyqldump $config mydb | gzip" | gzip -dc | ./restore
Anyway, I doubt it's possible using passthru, but perhaps with proc_open in some way?
As a last case resort, in the event of an unsolvable PHPredicament, I would do something like this
./restore file.sql
With script like this
#!/usr/bin/env php
$cmd = sprintf("mysql mydb < %s", $argv[1]);
passthru($cmd, $status);
PHPwnd
./restore
#!/usr/bin/env php
<?php
// set descriptors
$desc = array(
0 => array("file", "php://stdin", "r"),
1 => array("pipe", "w"),
2 => array("pipe", "w")
);
// open child proc
$proc = proc_open("mysql -uUSER -pPASS mydb", $desc, $pipes);
if (is_resource($proc)) {
// display child proc stdout
echo stream_get_contents($pipes[1]);
// close file streams
array_map('fclose', $pipes);
// get return value
$ret = proc_close($proc);
// display return value
printf("command returned %s\n", $ret);
}
Example use
cat file.sql.gz | gzip -dc | ./restore
What! PHProfound!
Turns out passthru already does this! PHenomenalP!
Check it out, PHPals:
gzip.php
#!/usr/bin/env php
<?php
passthru("gzip", $status);
if ($status !== 0) {
error_log("gzip exited with status %d", $status);
}
How PHProgressivist!
echo "hello" | php gzip.php | gzip -dc
Output
hello
I'm trying to spawn a process using following statement
$cmd = "Hello.scala";
$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("pipe", "w")
);
$process = proc_open($cmd, $descriptorspec, $pipes, $cwd, null);
through PHP script. I'm using spawned process to execute a simple Scala program and capturing program's output. But unfortunately I'm not getting success. Child process runs successfully but after few seconds its getting terminated due to sigterm signal.
$stat = proc_get_status($process);
if ($stat['signaled'] && $stat['termsig'] == 9){
echo "Process got sigterm";
}
finally its printing Process got sigterm. I tried all the way to increase CPU time using ulimit in httpd and I also tried to set max execution time but no success. I will really appreciate any help on this.
Ok before the flaming starts I did some research here and at some other links:
Run process with realtime output in PHP
This is what I am currently using:
<?php
$descriptorspec = array(
0 => array("pipe", "r"), // stdin
1 => array("pipe", "w"), // stdout
2 => array("pipe", "w"), // stderr
);
flush(); ?>
<?php $process = proc_open($_POST['field1'], $descriptorspec, $pipes, dirname(__FILE__), null);?>
<?php $stdout = stream_get_contents($pipes[1]); fclose($pipes[1]); $stderr = stream_get_contents($pipes[2]); fclose($pipes[2]); ?>
This works fantastic for outputting any stdout and stderr from a console application to my webpage, however, due to what I am assuming is buffering it does not update in realtime. I have tried the methods in the link I posted and a similar method in Ajax but neither would update in realtime, just wait for all of the input to finish then display on the page.
All I can come up with is that php is waiting for the proc_open() command to finish executing before it tries to do anything with the output.
Any insight would be much appreciated.
Thanks!
I would like the output of one process created with proc_open to be piped to another one created with proc_open (in php). For example. In bash I can do:
[herbert#thdev1 ~]$ cat foo
2
3
1
[herbert#thdev1 ~]$ cat foo | sort
1
2
3
[herbert#thdev1 ~]$
I would like to simulate this in php using proc_open (instead of shell_exec) in order to have control over return-codes, pipes, etc. So I want something like this:
$catPipes=array();
$sortPipes=array();
$cwd = '/tmp';
$env = array();
$catProcess = proc_open("cat foo", array(
0 => array("pipe", "r"),
1 => array("pipe", "w")
), $catPipes, $cwd, $env);
$sortProcess = proc_open("sort", array(
0 => array("pipe", "r", $catPipes[1]),
1 => array("pipe", "w"),
), $sortPipes, $cwd, $env);
echo stream_get_contents($sortPipes[1]);
fclose($sortPipes[1]);
//proc_close(this) ... proc_close(that) ... etc
Would someone know how I can simulate the "|" of bash in php, i.e. connect the second descriptor of the cat-process to the first descriptor of the sort-process? Any help would be appreciated! But please do not redirect me to shell_exec, as I want to be able to check exit-codes and log errors :).
EDIT:
My needs-to-work-business-solution btw is:
while(!feof($searchPipes[1])) fwrite($lookupPipes[0], stream_get_line($searchPipes[1], 40000));
Which is basically what the OS would do, but I do not want to my own pipe-management, as I have a kernel/posix for that, and let's be honest, it's not 1976 :)
Yes, you can -- but i think you have to define this the way round. That you can use the STDIN of "sort" as STDOUT pipe for "cat". Have a look at the following, which works for me:
<?php
$txt = "a\nc\ne\nb\nd\n";
$fh = fopen('data://text/plain;base64,' . base64_encode($txt), 'r');
$sort_pipes = array();
$sort_proc = proc_open(
'sort',
array(
array('pipe', 'r'),
STDOUT
),
$sort_pipes
);
$cat_pipes = array();
$cat_proc = proc_open(
'cat',
array(
$fh,
$sort_pipes[0]
),
$cat_pipes
);
In the first two rows i defined a data stream from a text string that i do not have to rely on a file somewhere in the filesystem. Note, that i have a list of unsorted characters stored in the data stream (a, c, e, b, d). Running the script above should return a sorted list to STDOUT.
Note, that you can specify resources as descriptors, too. In this case you must omit the array notation, so:
STDOUT
instead of
array(STDOUT)
etc.
Btw.: you can even write directly to a file specified by a file name. You can find further information on the descriptor specification in the manual entry for proc_open at http://en.php.net/manual/de/function.proc-open.php
EDIT
The other way works too, of course: you can also write "cat" to an STDOUT pipe array('pipe', 'w') and use $cat_pipes[1] as STDIN for "sort". :)
I'm trying to get
`mysql -uroot`;
to enter the MySQL interactive client just as executing
$ mysql -uroot
from the shell does.
It's okay if the PHP script exists after (or before), but I need it to invoke the MySQL client.
I've tried using proc_open() and of course system(), exec() and passthru(). Wondering if anyone has any tips.
New solution:
<?php
$descriptorspec = array(
0 => STDIN,
1 => STDOUT,
2 => STDERR
);
$process = proc_open('mysql -uroot', $descriptorspec, $pipes);
Old one:
Save for tab completion (you could probably get it in there if you read out bytes with fread instead of using fgets), this gets you on your way, lots left to tweak:
<?php
$descriptorspec = array(
0 => array("pty"),
1 => array("pty"),
2 => array("pty")
);
$process = proc_open('mysql -uroot', $descriptorspec, $pipes);
stream_set_blocking($pipes[1], 0);
stream_set_blocking($pipes[2], 0);
stream_set_blocking(STDIN,0);
do {
echo stream_get_contents($pipes[1]);
echo stream_get_contents($pipes[2]);
while($in = fgets(STDIN)) fwrite($pipes[0],$in);
} while (1);
I guess it does work, but it's waiting for some input. Try sending some sql commands to it's stdin. Of course, since the backtick operator doesn't support IO remapping, you'll need more complex process handling.