Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #19252

Re: proc_open, proc_get_status, proc_close

From Arno Welzel <usenet@arnowelzel.de>
Newsgroups comp.lang.php
Subject Re: proc_open, proc_get_status, proc_close
Date 2023-01-07 16:05 +0100
Message-ID <k1tg26F6v07U1@mid.individual.net> (permalink)
References <20230106112959.738fadea@gargantua.manuelito.lan>

Show all headers | View raw


Badarbo, 2023-01-06 11:29:

> Hello,
> with the code below, $rv is 0 :-)
> 
> --- begin code ---
> $pdesc=[0=>['pipe','r'],1=>['file','stdout.log','w'],2=>['file','stderr.log','w']];
> $proc=proc_open('sleep 3',$pdesc,$pipes);
> fclose($pipes[0]);
> $rv=proc_close($proc);
> echo('rv: '.$rv.PHP_EOL);
> exit(0);
> --- end code ---
[...]

Do not trust the proc-Functions in PHP to get any process results. The
did not work reliable for this in PHP 7.4 and in 8.0/8.1 I never got any
useful result from proc_close().

If you really need to be sure to get the return code of the command you
called, wrap the call in a shell command and use the result from the
wrapper, like this:

<?php
$command = 'some-command-to-execute';

$descriptors = [
	0 => ['pipe', 'r'],   // stdin
	1 => ['pipe', 'w'],   // stdout
	2 => ['pipe', 'w'],   // stderr
	3 => ['pipe', 'w'],   // file descriptor for exit code
];
$process = proc_open($command.'; echo $? >&3', $descriptors, $pipes);
if (is_resource($process)) {
	while ($output = fgets($pipes[1])) {
		$shellOutput .= $output;
	}
}
if (!feof($pipes[3])) {
	$shellResult = (int) rtrim(fgets($pipes[3], 5), "\n");
}
fclose($pipes[3]);
proc_close($process);

// $shellOutput will contain the output of stdout
// $shellResult will not contain the return code




-- 
Arno Welzel
https://arnowelzel.de

Back to comp.lang.php | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

proc_open, proc_get_status, proc_close Badarbo <badarbo@TOGLITUTTELEMAIUSCOLEhotmail.it> - 2023-01-06 11:29 +0100
  Re: proc_open, proc_get_status, proc_close Arne Vajhøj <arne@vajhoej.dk> - 2023-01-06 09:07 -0500
  Re: proc_open, proc_get_status, proc_close "J.O. Aho" <user@example.net> - 2023-01-06 17:35 +0100
  Re: proc_open, proc_get_status, proc_close Arno Welzel <usenet@arnowelzel.de> - 2023-01-07 16:05 +0100
  Re: proc_open, proc_get_status, proc_close Ezimene nimi Teine nimi <techfan55555@hotmail.com> - 2023-03-06 03:25 -0800
    Re: proc_open, proc_get_status, proc_close The Doctor <doctor@doctor.nl2k.ab.ca> - 2023-03-06 16:22 +0000
  Re: proc_open, proc_get_status, proc_close V õ l u r <yefap33237@cutefier.com> - 2023-05-25 05:40 -0700

csiph-web