acolchagoff
acolchagoff

Reputation: 1978

php runs terminal app with system()/shell_exec()/exec() but only half of the output shows up

so I have a php script:

<?php
    error_reporting(E_ALL);
    //print_r($_GET);
    $command = '../../build/program_name ' . $_GET['arg_id'];
    $command .= ' -test '.$_GET['arg1'].' '.$_GET['arg2'];


    //echo $command . "\n";
    //system('pwd');
    //system('ls -la ../../build');

    //system('../../build/program_name 17 -test 125 1500 2>&1');
    //passthru('../../build/program_name 17 -test 125 1500');
    system('../../build/program_name 17 -test 125 1500');
    //system($command);
    //$data = exec($command);
    //var_dump($data);

    //echo $data;
    //echo "\n";
?>

simple version:

<?php
    error_reporting(E_ALL);

    system('../../build/program_name 17 -test 125 1500');
?>

The output from this is:

argv[0] = ../../build/program_name
argv[1] = 17
argv[2] = -test
argv[3] = 125
argv[4] = 1500
argc = 5 so appears to be test request.
TEST(125, 1500) test requested.

If I run the command in the terminal however the output is:

argv[0] = ../../build/program_name
argv[1] = 17
argv[2] = -test
argv[3] = 125
argv[4] = 1500
argc = 5 so appears to be test request.
TEST(125, 1500) test requested.
{ "function": "test" , "inputs": [125.000000, 1500.000000], "output": 999.000000}

the last bit of output (the important part) isn't showing up when php runs the exact same command... I've printed the working directory and the command just to verify that I'm in fact running the command correctly, the results are consistently the same across exec, shell_exec, and system.... I'm just at a loss for what is happening here...

Edit: additional info about 'program_name' heres an extremely simplified version. its c++:

    float arg1;
    float arg2;
    if(argc == 5){
        arg1 = atof(argv[3]);
        arg2 = atof(argv[4]);
    } else {
        cout << "please supply appropriate args" << endl;
        return -1;
    }
    #ifdef DEBUG
        cout << "TEST(" << arg1 << ", " << arg2 << ") test requested." << endl;
    #endif

    float output = test(arg1, arg2);

    string jsonOut = "{ ";
    jsonOut.append(
            "\"function\": \"test\" , ").append(
            "\"inputs\": [").append(to_string(arg1)).append(", ").append(to_string(arg2)).append("], ").append(
            "\"output\": ").append(to_string(output)).append("}");
    cout << jsonOut << endl;
    return 0;

Upvotes: 0

Views: 87

Answers (2)

acolchagoff
acolchagoff

Reputation: 1978

Okay this is insane, but fixed it. the program 'program_name' was saving a little log file using fprintf(). php would ignore all output from the program after the first call to fprintf()... commenting out fprintf() and recompiling program_name fixed the problem and now php doesn't bail in the middle, apparently php is incompatible to calls to fprintf() on OS X???

Upvotes: 0

Barmar
Barmar

Reputation: 780713

Use passthru() instead of system(). The difference is that passthru() passes the command's output through to PHP's output, while system() captures the output and returns the last line.

The other output you're seeing is presumably written to stderr, which system() doesn't capture.

Upvotes: 1

Related Questions