When I run proc_open with a file.txt path for the stderr output, that file is constantly updated every couple seconds with output of the proc_open program I am running which is ffmpeg. I just want to redirect that output to a phpfile where it can be read and parsed but while the process is running (so send info every couple seconds) so I can update a database. IS this possible? I have been googling for hours so if there are any experts out there who actually know how to use this function of have experience in this I would greatly appreciate any help.
this puts output in a text doc. If I change to 2 => array("pipe","w") and the echo the output only comes on my screen at the end of the process
$descriptorspec = array(
0 => array("pipe","r"),
1 => array("pipe","w"),
2 => array("file","/home/g/Desktop/test.txt","a")
) ;
$cwd = './' ;
// open process /bin/sh
$process = proc_open("/usr/local/bin/ffmpeg -i /home/g/Desktop/vid.wmv /home/g/Desktop/vid.flv", $descriptorspec, $pipes, $cwd) ;
you could run ffmpeg like this:
ffmpeg ..optinons... 2>&1 | php script.php
But you could have in input STDERR and STDOUT of ffmpeg
And second normal decision:
<?
$cmd = 'ffmpeg -i /home/azat/Desktop/src.avi -y /home/azat/Desktop/dst.avi';
$pipes = array();
$descriptors = array(2 => array('file', '/tmp/atest.log', 'a'));
$p = proc_open($cmd, $descriptors, $pipes);
while (true) {
sleep(1);
$status = proc_get_status($p);
if (!$status['running']) break;
echo "STEEL RUN\n";
// some manupulations with "/tmp/atest.log"
}
Also you can see this class - it is a wrapper for exec processes
Related
Hello I'am trying to execute a shell command in my php script but it is not working.
My php script :
//I save the Order
$holdedOrder->save();
$id = $holdedOrder->id;
$old_path = getcwd();
chdir(__DIR__.'/../');
$scriptFile = 'anacron_job_unhold_order.sh';
$bool = file_exists($scriptFile);
//$bool is true !
//this command works in shell but not in here do not know why
$s = shell_exec("echo \"/usr/bin/bash $scriptFile $id\" | /usr/bin/at now +$when");
chdir($old_path);
return [$s,$bool];
$when has a valid value 4 hours or 4 days ...
The command will be :
echo bash anacron_job_unhold_order.sh 29 | at now +1 minutes
the output is null. Trying it with exec() is returning 127 code
Edit :
I removed www-data from /etc/at.deny and still the same problem
The command shell_exec("echo \"/usr/bin/bash $scriptFile $id\" | /usr/bin/at now +$when"); is probably causing the issue, you should take a closer look on how at works.
The command should be something like
at [options] [job...]
To give a job to at from the command instead of STDIN, use heredoc syntax
at now + 1 minute <<< 'touch /tmp/test'
So the PHP code should be something like;
$s = shell_exec("/usr/bin/at now + {$when} <<< '/usr/bin/bash {$scriptFile} {$id}'");
exec($s, $output, $return_var);
Looking forward to integrate stockfish chess engine into a php-cli script.
There is an unexpected behavior, the stockfish program is quitting directly without "thinking", it returns only the position at depth 1, when called from php.
For better understanding, while running the stockfish program from the command line, here is the expected behavior (gif):
From php, the following is working (starting position, whites to play, asking for 50 depth), it returns a move a2a3, the position at depth 1, which is a pretty bad move!
The answer is instantaneous, where going trough all the depth levels should take at least many seconds.
It is identical with any FEN positions, always returning the depth 1.
$descr = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("pipe", "w")
);
$pipes = array();
$process = proc_open("stockfish", $descr, $pipes);
if (is_resource($process)) {
fwrite($pipes[0], "uci\n");
fwrite($pipes[0], "ucinewgame\n");
fwrite($pipes[0], "position fen rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - - 0 1\n");
fwrite($pipes[0], "go depth 50\n");
fclose($pipes[0]);
// Read all output from the pipe
while (!feof($pipes[1])) {
echo fgets($pipes[1]);
}
fclose($pipes[1]);
proc_close($process);
}
// RETURN last line: bestmove a2a3
All versions of stockfish had been tested, 8, 9, 10, with the same result.
I tried many options and different ways to run shell commands from php, including posix_mkfifo() piping, but none are working as expected, always returning a move at depth 1.
Another example, same behavior, return always "a2a3".
file_put_contents(".COMFISH","uci\nucinewgame\nsetoption name Threads value 1\nposition fen rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - - 0 1\ngo depth 50");
$COM = explode(" ",system("stockfish < .COMFISH"))[1];
var_dump($COM);
// RETURN a2a3
This might be directly linked on how that stockfish binary is written, (multi threadings), and not a php behavior, but I am looking for an explanation here?
From wiki:
Stockfish can use up to 512 CPU threads in multiprocessor systems.
Well, this was fairly simple, the pipe was closed too early.
Leaving here the base of a working code for future readers.
$descr = [0 => ["pipe", "r"], 1 => ["pipe", "w"]];
$pipes = [];
$process = proc_open("stockfish", $descr, $pipes);
if (is_resource($process)) {
fwrite($pipes[0], "uci\n");
fwrite($pipes[0], "ucinewgame\n");
fwrite($pipes[0], "position fen rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - - 0 1\n");
fwrite($pipes[0], "setoption name Skill Level value 17\n"); // Set level between 1 and 20
fwrite($pipes[0], "go movetime 5000\n"); // Return bestmove after 5 seconds
while (!feof($pipes[1])) {
echo fgets($pipes[1]);
}
fclose($pipes[0]);
fclose($pipes[1]);
proc_close($process);
}
// RETURN last line: bestmove e2e4 ponder e7e6
First let's run the command via terminal:
$ echo 1; /etc/init.d/apache3 restart; echo 2;
result.. ( apache3 on purpose to see an error )
1
bash: /etc/init.d/apache3: No such file or directory
2
awsome.
now let's run this via php..
<?php
$command = "echo 1; /etc/init.d/apache3 restart; echo 2; 2>&1";
$response = shell_exec("$command");
echo $response;
?>
all I see on the browser is: 1 2
I tried all sorts of things. replaced the semi colons with "&&"..
tried all the php stuff such as..
passthru()
exec()
system()
popen()
i tried it all pretty much. been hours.. can not get it to show me the same stuff i see via terminal.
You have to use 2>&1 after the restart command
Your command :
$command = "echo 1; /etc/init.d/apache3 restart; echo 2; 2>&1";
yours has "2>&1" at the end which has no use.
Also you will add to 2>&1 after each command in case others uses STDERR
$command = "echo 1; /etc/init.d/apache3 restart 2>&1; echo 2 ";
Consider using exec. The basis function only returns the first line of the output:
$response = exec("$command"); // just the first line
But use the additional parameters to capture both the output (as an array) and the return value
$retval = NULL;
$output = NULL;
$response = shell_exec("$command", $output, $retval); // last two params are passed by reference and modified by the command
Also, as user993553 posted, the output captured by these cli functions in PHP will usually just return stdout and not stderr. You can append " 2>&1" to any given command (note the space before the 2) in order to route stderr into the output.
That said, your function becomes:
$command = "echo 1; /etc/init.d/apache3 restart 2>&1; echo 2;";
$retval = NULL;
$output = NULL;
$response = exec($command, $output, $retval);
var_dump($output);
and the output:
array(3) {
[0] =>
string(1) "1"
[1] =>
string(37) "sh: 1: /etc/init.d/apache3: not found"
[2] =>
string(1) "2"
}
EDIT: you can also check $retval for an error condition. If it's not empty or zero, then signifies an error.
From the manual of shell_exec:
ALSO, note that shell_exec() does not grab STDERR, so use "2>&1" to
redirect it to STDOUT and catch it.
I have a php page that creates a shell script that the same php page starts after creating it, inside I have one of many commands that I want to send the process to a log that does not work while others actually work....
<php
$scriptfile = script.sh;
$logfile = process.log;
$imgfile = image.ppm;
//this one works, it sends the output to the log file
$cmd ="Scripts/convert.sh file.doc > $logfile \\\n";
file_put_contents($scriptfile, $cmd, FILE_APPEND | LOCK_EX);
//this one does not work, it does not send the output to the log file and stops the process
$cmd = "&& for i in $(seq --format=%003.f 0 $(( $(ls -1 | wc -l) -1 )) ); do echo doing OCR on page \$i; tesseract $imgfile-\$i.ppm $imgfile-\$i -l eng; done >> $logfile";
file_put_contents($scriptfile, $cmd, FILE_APPEND | LOCK_EX);
$cmd = "/bin/sh $scriptfile > /dev/null 2>&1 &";
shell_exec($cmd);
?>
I have tried the not working command from the shell and it does send the output to the log file, either this way:
for i in $(seq --format=%003.f 0 $(( $(ls -1 | wc -l) -1 )) ); do echo doing OCR on page $i; tesseract image-$i.ppm image-\$i -l eng; done >> process.log
or this way:
for i in $(seq --format=%003.f 0 $(( $(ls -1 | wc -l) -1 )) ); do echo doing OCR on page $i >> process.log; tesseract image-$i.ppm image-\$i -l eng; done
And here you have the way the shell script looks like after being created by php:
#! /bin/sh
Scripts/convert.sh file.doc >> process.log \
&& for i in $(seq --format=%003.f 0 $(( $(ls -1 | wc -l) -1 )) ); do echo doing OCR on page $i; tesseract image-000.ppm image-$i -l eng; done >> process.log
So my question is, what can be wrong, I've tried many different things already but no success unfortunately, any help or advice will be very welcomed!! thanks from now!!
I have an exec command I am using to make ffmpeg work.
$process = exec("/usr/local/bin/ffmpeg -i /home/g/Desktop/cave.wmv -deinterlace
-acodec libfaac -ab 96k -ar 44100 -vcodec libx264 -s 480x320 -f flv /home/g/Desktop
/file.flv 2>&1 | php testing123.php") ;
In testing123.php I use
$h = fopen('php://stdin', 'r'); $str = fgets($h); fclose($h);
//topic removed
$sql = 'INSERT INTO table
(id,status) VALUES(?,?)';
$stmt3 = $conn->prepare($sql);
$result=$stmt3->execute(array(rand(),$str));
However, as you may have guessed I get one database entry, only when the file initially executes from the exec command in the other file. Is there a way to keep executing the file or something else so that the output fed to the file can be inserted into my database every couple seconds?
You need to use a while loop. There are some examples in the fgets documentation.
$h = fopen('php://stdin', 'r');
while ( ($str = fgets($h)) !== false )
{
$sql = 'INSERT INTO ...';
mysql_query($sql);
}
fclose($h);
One of the comments in the documentation suggests using stream_get_line instead of fgets. In case you're interested, here's the direct link: http://www.php.net/manual/en/function.fgets.php#86319.