note 81317 added to function.proc-open
| From: | chrisATw3styleDOTco dot uk at osu1 dot php dot net | Date: | Fri, 22 Feb 2008 10:57:00 +0000 |
| Subject: | note 81317 added to function.proc-open | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-136394@lists.php.net to get a copy of this message | ||
It took me a long time (and three consecutive projects) to figure this out. Because popen() and
proc_open() return valid processes even when the command failed it's awkward to determine when
it really has failed if you're opening a non-interactive process like "sendmail -t".
I had previously guess that reading from STDERR immediately after starting the process would work,
and it does... but when the command is successful PHP just hangs because STDERR is empty and
it's waiting for data to be written to it.
The solution is a simple stream_set_blocking($pipes[2], 0) immediately after calling proc_open().
<?php
$this->_proc = proc_open($command, $descriptorSpec, $pipes);
stream_set_blocking($pipes[2], 0);
if ($err = stream_get_contents($pipes[2]))
{
throw new Swift_Transport_TransportException(
'Process could not be started [' . $err . ']'
);
}
?>
If the process is opened successfully $pipes[2] will be empty, but if it failed the bash/sh error
will be in it.
Finally I can drop all my "workaround" error checking.
I realise this solution is obvious and I'm not sure how it took me 18 months to figure it out,
but hopefully this will help someone else.
NOTE: Make sure your descriptorSpec has ( 2 => array('pipe', 'w')) for this
to work.
----
Server IP: 117.104.160.194
Probable Submitter: 58.160.93.53
----
Manual Page -- http://www.php.net/manual/en/function.proc-open.php
Edit -- https://master.php.net/note/edit/81317
Del: integrated -- https://master.php.net/note/delete/81317/integrated
Del: useless -- https://master.php.net/note/delete/81317/useless
Del: bad code -- https://master.php.net/note/delete/81317/bad+code
Del: spam -- https://master.php.net/note/delete/81317/spam
Del: non-english -- https://master.php.net/note/delete/81317/non-english
Del: in docs -- https://master.php.net/note/delete/81317/in+docs
Del: other reasons-- https://master.php.net/note/delete/81317
Reject -- https://master.php.net/note/reject/81317
Search -- https://master.php.net/manage/user-notes.php