473,782 Members | 2,479 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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($p rocess))
_echo("OK\n");
else
die ("not opened\n");

_echo(print_r(p roc_get_status( $process),1));

stream_set_bloc king($pipes[1],0);

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

$time_limit = microtime(true) +2;
while (true) {
_echo(stream_ge t_contents($pip es[1]));

$status=proc_ge t_status($proce ss);

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

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

_echo(stream_ge t_contents($pip es[1]));
fclose($pipes[0]);
fclose($pipes[1]);

_echo(print_r(p roc_get_status( $process),1));

$return_value = proc_close($pro cess);
_echo("command returned $return_value\n ");
function _echo($str) {
echo htmlspecialchar s($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 2306
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_writ e_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
2528
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 simply dumped to my script's stdout, rather than being captured by fread(). Could someone give me a quick rundown of how to do something like this? The examples in the manual don't seem to work for me. This is on a FreeBSD system. -- Alan...
0
2294
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 to be the right thing to use but I have the problem that the called program gives >8kb data on both stdout/stderr back which causes my PHP script to simply hang in the fread call. To be precise the first 4096 "O" characters are read and displayed. ...
4
3156
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 drama) I've been trying to think of good ways to get Perl code to run inline in a PHP script. Here are a few of my ideas. If anyone has any further ideas, resources, or knows of someone else who's solved this already, please do tell... 1.) The...
0
2073
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 knows what setting I have to change to fix this? Configuration:
0
1953
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 no avail (it doesn't seem to work within a for loop).
1
3242
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. The IUSR_ account has access to both the cmd.exe and wmic files. I have also tried copying the wmic files to the C:\php dir, and to the directory the web app is running from. Any
9
1536
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 servers, I've no idea of how many, the set_time_limit does not work for many of the servers out there, so I'm in trouble, I've tryed to do a refresh every 100 inserts, but it doesn't work at all, does anyone have a class, function or piece of code that...
4
4563
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 work. Any ideas? For more detail on what I'm trying to do, its' something like this... -------------------------------------
6
5475
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 22050 -y /home/transla1/public_html/cybertube/web/uploads/video/ generated/31_70_AK000005.AVI.flv '; //exec($str); runExternal($str,$code); echo $code ;
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10146
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10080
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9944
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8968
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6735
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.