By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,376 Members | 1,599 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,376 IT Pros & Developers. It's quick & easy.

proc_open print stderr

P: n/a
please help
how can I get stderr from processes ($proc[$i]) in my screen
without waiting the other processes to end

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/php
  2. <?php
  3.  
  4. $con = mysql_connect("localhost", "user");
  5. if (!$con)
  6. {
  7. die('Could not connect: ' . mysql_error());
  8. }
  9. mysql_select_db("database");
  10.  
  11. $stores = mysql_num_rows(mysql_query ("select * from stores"));
  12.  
  13. $query1 = mysql_query ("select sid, sip, sport, sname from stores");
  14.  
  15. $reads = array();
  16. $storess = array();
  17. while ( $store = mysql_fetch_assoc($query1))
  18. {
  19. $storess[$store['sid']] = $store;
  20.  
  21. $ip = $store['sip'];
  22. $port = $store['sport '];
  23.  
  24. $descriptorspec = array(
  25. 0 =array("pipe", "r"),  // stdin is a pipe that the child will
  26. read from
  27. 1 =array("pipe", "w"),  // stdout is a pipe that the child will
  28. write to
  29. 2 =array("pipe", "w") // stderr
  30. );
  31.  
  32. $proc[$i] = proc_open('mysqldump -u user -pPASS -h '.$ip.' -P '.
  33. $port.' ... file.sql', $descriptorspec, $pipes);
  34.  
  35. //get stderr
  36. $reads[$store['sid']] = $pipes[2];
  37.  
  38. }//end while
  39.  
  40. echo "Dumping ...". "\n";
  41.  
  42.  
  43. /*
  44. echo "ALL STDERR"
  45. */
  46.  
  47.  
  48.  
  49.  
  50. echo "--OK--". "\n";
  51. ?>
  52.  

I tried this:

Expand|Select|Wrap|Line Numbers
  1. while ( stream_select($select_reads, $null, $null, 0) !== false )
  2. {
  3.  
  4.  
  5. echo "stream is \n";
  6. var_dump($select_reads);
  7. foreach ($select_reads as $read) {
  8. $sid = array_search($read, $reads);
  9.  
  10. echo $storess[$sid]['sname'] . " SAID:\n";
  11. var_dump(fread($read, 1024));
  12. }
  13.  
  14. // stream_select modifies the contents of $select_reads
  15. // in a loop we should replace it with the original
  16. $select_reads = $reads;
  17. sleep(1);
  18. }//end while
  19.  
Jun 2 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
sa******@gmail.com schreef:
please help
how can I get stderr from processes ($proc[$i]) in my screen
without waiting the other processes to end
Hi,

I cannot help you with your STDERR, but I spotted something that could
potentially slow down your script a lot.
[code]
#!/usr/bin/php
<?php

$con = mysql_connect("localhost", "user");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database");

$stores = mysql_num_rows(mysql_query ("select * from stores"));
Just a small suggestion:
The above line is a waste of CPU cycles (and bandwidth is mySQL is on
another fysical server).
You actually ask mysql to return the full content of the table stores,
only to find out how many there are.
This won't matter a lot when table stores is small, but it is a bad
approach, I think.
MySQL must:
1) Find out what * translates to (all your columnnames).
2) Send the full content of that table to PHP.

Much better is to use the SQL build-in command COUNT() for this:
$SQL = "SELECT COUNT(*) AS numrowsstore FROM stores;";
or
$SQL = "SELECT COUNT(storeid) AS numrowsstore FROM stores;";
(assuming storeid exists)

Just a suggestion. :-)

I hope somebody else can help you with the STDERR.

Regards,
Erwin Moller
Jun 2 '08 #2

P: n/a
On 8 May, 12:06, Erwin Moller
<Since_humans_read_this_I_am_spammed_too_m...@spam yourself.comwrote:
sakis...@gmail.com schreef:
please help
how can I get stderr from processes ($proc[$i]) in my screen
without waiting the other processes to end

Hi,

I cannot help you with your STDERR, but I spotted something that could
potentially slow down your script a lot.
[code]
#!/usr/bin/php
<?php
$con = mysql_connect("localhost", "user");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database");
$stores = mysql_num_rows(mysql_query ("select * from stores"));

Just a small suggestion:
The above line is a waste of CPU cycles (and bandwidth is mySQL is on
another fysical server).
You actually ask mysql to return the full content of the table stores,
only to find out how many there are.
This won't matter a lot when table stores is small, but it is a bad
approach, I think.
MySQL must:
1) Find out what * translates to (all your columnnames).
2) Send the full content of that table to PHP.

Much better is to use the SQL build-in command COUNT() for this:
$SQL = "SELECT COUNT(*) AS numrowsstore FROM stores;";
or
$SQL = "SELECT COUNT(storeid) AS numrowsstore FROM stores;";
(assuming storeid exists)

Just a suggestion. :-)

I hope somebody else can help you with the STDERR.

Regards,
Erwin Moller
What is more to the point, is that the value $stores is never used
again.
Jun 2 '08 #3

P: n/a
Captain Paralytic schreef:
On 8 May, 12:06, Erwin Moller
<Since_humans_read_this_I_am_spammed_too_m...@spam yourself.comwrote:
>sakis...@gmail.com schreef:
>>please help
how can I get stderr from processes ($proc[$i]) in my screen
without waiting the other processes to end
Hi,

I cannot help you with your STDERR, but I spotted something that could
potentially slow down your script a lot.
>>[code]
#!/usr/bin/php
<?php
$con = mysql_connect("localhost", "user");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database");
$stores = mysql_num_rows(mysql_query ("select * from stores"));
Just a small suggestion:
The above line is a waste of CPU cycles (and bandwidth is mySQL is on
another fysical server).
You actually ask mysql to return the full content of the table stores,
only to find out how many there are.
This won't matter a lot when table stores is small, but it is a bad
approach, I think.
MySQL must:
1) Find out what * translates to (all your columnnames).
2) Send the full content of that table to PHP.

Much better is to use the SQL build-in command COUNT() for this:
$SQL = "SELECT COUNT(*) AS numrowsstore FROM stores;";
or
$SQL = "SELECT COUNT(storeid) AS numrowsstore FROM stores;";
(assuming storeid exists)

Just a suggestion. :-)

I hope somebody else can help you with the STDERR.

Regards,
Erwin Moller

What is more to the point, is that the value $stores is never used
again.
LOL, yes. Good point.
That makes it an even better waste of CPU. ;-)

Regards,
Erwin Moller
Jun 2 '08 #4

P: n/a
sa******@gmail.com wrote:
please help
how can I get stderr from processes ($proc[$i]) in my screen
without waiting the other processes to end

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/php
  2. <?php
  3.     $con = mysql_connect("localhost", "user");
  4.     if (!$con)
  5.       {
  6.          die('Could not connect: ' . mysql_error());
  7.      }
  8.      mysql_select_db("database");
  9.      $stores = mysql_num_rows(mysql_query ("select * from stores"));
  10.     $query1 = mysql_query ("select sid, sip, sport, sname from stores");
  11.     $reads = array();
  12.     $storess = array();
  13.     while ( $store = mysql_fetch_assoc($query1))
  14.     {
  15.         $storess[$store['sid']] = $store;
  16.         $ip = $store['sip'];
  17.         $port = $store['sport '];
  18.         $descriptorspec = array(
  19.                0 =array("pipe", "r"),  // stdin is a pipe that the child will
  20. read from
  21.              1 =array("pipe", "w"),  // stdout is a pipe that the child will
  22. write to
  23.                2 =array("pipe", "w") // stderr
  24.         );
  25.         $proc[$i] = proc_open('mysqldump -u user -pPASS -h '.$ip.' -P '.
  26. $port.' ... file.sql', $descriptorspec, $pipes);
  27.                 //get stderr
  28.         $reads[$store['sid']] = $pipes[2];
  29.     }//end while
  30.     echo "Dumping ...". "\n";
  31.     /*
  32.         echo "ALL STDERR"
  33.         */
  34.     echo "--OK--". "\n";
  35. ?>
  36.  


I tried this:

Expand|Select|Wrap|Line Numbers
  1. while ( stream_select($select_reads, $null, $null, 0) !== false )
  2.     {
  3.         echo "stream is \n";
  4.         var_dump($select_reads);
  5.         foreach ($select_reads as $read) {
  6.             $sid = array_search($read, $reads);
  7.             echo $storess[$sid]['sname'] . " SAID:\n";
  8.             var_dump(fread($read, 1024));
  9.         }
  10.         // stream_select modifies the contents of $select_reads
  11.                 // in a loop we should replace it with the original
  12.            $select_reads = $reads;
  13.            sleep(1);
  14.     }//end while
  15.  
You read from your stderr pipe. In this case you could use:

echo stream_get_contents($descriptorspec[2]);

But you have an error in your proc_open call. You cannot use the
redirection symbols '<' or '>' (or the pipe symbol '|'). These are
handled by the command processor, which does not get executed when you
call proc_open.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Jun 2 '08 #5

P: n/a
On May 8, 2:07 pm, sakis...@gmail.com wrote:
please help
how can I get stderr from processes ($proc[$i]) in my screen
without waiting the other processes to end

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/php
  2. <?php
  3.         $con = mysql_connect("localhost", "user");
  4.         if (!$con)
  5.         {
  6.                 die('Could not connect: ' . mysql_error());
  7.         }
  8.         mysql_select_db("database");
  9.         $stores = mysql_num_rows(mysql_query ("select * from stores"));
  10.         $query1 = mysql_query ("select sid, sip, sport, sname from stores");
  11.         $reads = array();
  12.         $storess = array();
  13.         while ( $store = mysql_fetch_assoc($query1))
  14.         {
  15.                 $storess[$store['sid']] = $store;
  16.                 $ip = $store['sip'];
  17.                 $port = $store['sport '];
  18.                 $descriptorspec = array(
  19.                         0 =array("pipe", "r"),  // stdin is a pipe that the child will
  20. read from
  21.                         1 =array("pipe", "w"),  // stdout is a pipe that the child will
  22. write to
  23.                         2 =array("pipe", "w") // stderr
  24.                 );
  25.                 $proc[$i] = proc_open('mysqldump -u user -pPASS -h '.$ip.' -P '.
  26. $port.' ... file.sql', $descriptorspec, $pipes);
  27.                 //get stderr
  28.                 $reads[$store['sid']] = $pipes[2];
  29.         }//end while
  30.         echo "Dumping ...". "\n";
  31.         /*
  32.         echo "ALL STDERR"
  33.         */
  34.         echo "--OK--". "\n";
  35. ?>
  36.  

I tried this:

Expand|Select|Wrap|Line Numbers
  1. while ( stream_select($select_reads, $null, $null, 0) !== false )
  2.         {
  3.                 echo "stream is \n";
  4.                 var_dump($select_reads);
  5.                 foreach ($select_reads as $read) {
  6.                         $sid = array_search($read, $reads);
  7.                         echo $storess[$sid]['sname'] . " SAID:\n";
  8.                         var_dump(fread($read, 1024));
  9.                 }
  10.                 // stream_select modifies the contents of $select_reads
  11.                 // in a loop we should replace it with the original
  12.                 $select_reads = $reads;
  13.                 sleep(1);
  14.         }//end while
  15.  
there is an other solution:
sending stderr to file

Expand|Select|Wrap|Line Numbers
  1. $descriptorspec = array(
  2. 0 =array("pipe", "r"),  // stdin is a pipe
  3. that the child will
  4. read from
  5. 1 =array("pipe", "w"),  // stdout is a pipe
  6. that the child will
  7. write to
  8. 2 =array("file", "/tmp/error-output.txt",
  9. "a") // stderr is a file to write to
  10. );
  11.  
but the problem is: who to add to the file the "sname" to know from
which store came the stderr ?
thank you
Jun 2 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.