473,396 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

help with proc_open(): some apps work - some don't

Hello,

I'm attempting to use proc_open to launch, control and log CLI
applications from my scripts, but my first tests are disheartening:

<pre>
<?php

$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("file", "/var/tmp/errors", "a+")
);

$cwd = '/var/tmp';
$env = array('a' => 'abc'); // otherwise some processes exit with code
127

$cmd='cat -n';
// $cmd='tr a A';
$process = proc_open($cmd, $descriptorspec, $pipes, $cwd, $env);

if (is_resource($process))
_echo("OK\n");
else
die ("not opened\n");

_echo(print_r(proc_get_status($process),1));

stream_set_blocking($pipes[1],0);

fwrite($pipes[0],"blah blah\n");
fwrite($pipes[0],"blah blah\n");

$time_limit = microtime(true)+2;
while (true) {
_echo(stream_get_contents($pipes[1]));

$status=proc_get_status($process);

if (!$status['running']) {
_echo("DIED\n");
break;
}

if (microtime(true) > $time_limit) {
_echo("TIMEOUT\n");
posix_kill($status['pid'], 2); // SIGINT
break;
}
}

_echo(stream_get_contents($pipes[1]));
fclose($pipes[0]);
fclose($pipes[1]);

_echo(print_r(proc_get_status($process),1));

$return_value = proc_close($process);
_echo("command returned $return_value\n");
function _echo($str) {
echo htmlspecialchars($str);
if (php_sapi_name()!='cli') {
ob_flush(); flush();
}
}
?>
</pre>

With 'cat -n', every line gets processed as soon as it is sent; not so
with 'tr a A' (I have to fclose(pipes[0]) for tr to process the input).
What's the difference? My ultimate goal is to run pppd, ping, wget and
others, put only ping seems to work.

regards,

-- Ariel

Nov 24 '05 #1
1 2287
Update: a simple script like this works fine as the child process:

#!/usr/local/bin/php
<?php
while ( trim($line = fgets(STDIN)) != 'exit' )
fwrite(STDOUT,$line);
exit(0);
?>

But 'grep .' doesn't - it also requires me to fclose its stdin.
Is this about stream buffering? I've tried
stream_set_write_buffer($pipes[0],0), to no avail.

help!

- Ariel

Nov 25 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Alan Little | last post by:
I'm struggling with proc_open(). What I'm trying to do is conduct an sftp session. The process appears to open fine, but anything I send to it with fwrite() is ignored, and any output from sftp is...
0
by: Christian Hammers | last post by:
Hello I would like to call a unix shellscript from within a PHP script and - write data to its STDIN - read data from its STDOUT *and* STDERR - get its exit code afterwards proc_open seems...
4
by: FLEB | last post by:
I like PHP for its excellent inline integration into standard HTML files, but I like Perl for its quick-moving syntax and simpler data-processing. To resolve this deep-seated inner turmoil (oh, the...
0
by: FrenKy | last post by:
Hi *. I get this error when trying to run proc_open() function: "Fatal error: Call to undefined function: proc_open() in /home/frantic/public_html/a/phpshell.pup on line 140" Does anybody...
0
by: yawnmoth | last post by:
popen / proc_open seem to be broken (as per PHP Bug #30463). As such, does anyone know if there are any alternatives to them? I tried to use fsockopen to load a bunch of instances of a script to...
1
by: Spudster3 | last post by:
I'm trying to get this to work.. <?php echo passthru('wmic cpu get loadpercentage'); ?> and just output to the browser... It looks like cmd.exe hangs when it tries to execute the command. ...
9
by: Freebird | last post by:
Hello everyone ... I've a problem in here, I need to make a 70 000 rows insert, and it's taking a lot more than 30 seconds, my problem is that it will be a script that will work in many...
4
by: darrin.skinner | last post by:
Hi all, How can I open a file/pipe to something other than STDOUT/STDERR? I.e. I want to write to file descriptor # 3. fopen("php://std3"... does not work. fopen("/dev/ttyS3"... does not...
6
by: xhe | last post by:
I am using ffmpeg to convert video, this is a sample script: $str='/home/transla1/bin/ffmpeg -i /home/transla1/public_html/ cybertube/web/uploads/video/31_AK000005.AVI -s 240x180 -b 100k -ar...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.