Exec Problem

Discussion in 'PHP' started by pjhenriqs, Jun 22, 2006.

  1. #1
    Hi!

    I am trying to execute a PERL program (using exec function on PHP). The purpose is for the user to click a button and then send a system call to execute this program on the server. I'm having a strange program behaviour that I can't understand.

    When I try to call the PERL program through the web browser on the client it works the first time, but then it doesn't return anything and the web browser on the server doesn't work as well. The error code given is -1073741790. I don't know what this is and I don't know what to do.

    I have also tried it with shell_exec,exec and system, and also using pipes but the behaviour is the same. I'm using Windows 2000. Can anyone help me?

    Paulo H.
     
    pjhenriqs, Jun 22, 2006 IP
  2. clancey

    clancey Peon

    Messages:
    1,099
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I have not seen such an error. When I call a perl program from php in Linux I use the following:

    
    $result = exec( "perl /home/user/somedir/script.pl " . $args);
    list( $a, $b, $c) = split(" " , $result);
    
    Code (markup):
    Is this the approach you used?
     
    clancey, Jun 23, 2006 IP
  3. pjhenriqs

    pjhenriqs Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    The code I'm using is

    
     $cmd = "cmd /c & cd\ & cd tool & perl tool.pl -u=pjhenriqs -p=mypass & exit";
    $output = exec($cmd);
    echo("<pre>" . $output . "</pre>";
    
    PHP:
    My $cmd string used to be smaller (just putting it in the right dir and running the perl program), but i've been trying to put this to work for so long that I've reached this point. I don't know where to find what the error code means and I really can't understand why when I'm on client A i only can run the php page on server B only one time before it returning error.
    I'm also using set_time_limit(60) because the perl program takes quite a while (it connects to another server). Could this be the problem?

    Thanks for your reply,
    Paulo H.
     
    pjhenriqs, Jun 23, 2006 IP
  4. clancey

    clancey Peon

    Messages:
    1,099
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I ran a test version of your code aginst an Apache server running on a WinXP pro machine. After fixing the error in line 3 and shrinking the call to the bare minimum, it ran just fine.

    If you are going to call the echo function in the format "echo(" you need to finish with ");"

    If the perl program is connecting to another machine, it will take time. If there are any line breaks in the result, only the last line with be contained in the return variable.

    You doing too much in the command. You should only need to invoke perl and the program, as in:

    $cmd = "perl c:\A_DIR\tool.pl -u=pjhenriqs -p=mypass";

    In my test, asking for a cmd window created huge problems. The exec timed out waiting for a response. Trying to run it a second time, basically hung the program. In fact, since you are asking Windows to run the program in another "cmd" instance, I do not see how it can return anything meaningful.

    Try this instead -- with the reversed slashes:

    
    $cmd = "perl c:/THE_DIRECTORY_PATH/tool.pl -u=pjhenriqs -p=mypass";
    $output = exec($cmd);
    echo("<pre>" . $output . "</pre>";
    
    Code (markup):
     
    clancey, Jun 23, 2006 IP
  5. balamm

    balamm Peon

    Messages:
    64
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I haven't tried the examples above but I find this works well for returning the output, if any, from shell commands.

    	exec("$cmd 2>&1", $output);
    	foreach($output as $outputline){
    	echo(nl2br("$outputline"));
    	}
    PHP:
     
    balamm, Jun 23, 2006 IP
  6. clancey

    clancey Peon

    Messages:
    1,099
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    0
    #6
    balamm! Great addition to the solution. I never thought of this! My approach works if you are expecting a single line of data in response to the call . . . but your's solves the problem of multi-line returns. This is good to know.
     
    clancey, Jun 24, 2006 IP