Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7559707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:42:10+00:00 2026-05-30T12:42:10+00:00

In php there are several methods to execute a shell command: system() passthru() shell_exec()

  • 0

In php there are several methods to execute a shell command:

  • system()
  • passthru()
  • shell_exec()
  • exec()

First two displays output but doesn’t return it.
Last two returns output but doesn’t display it.

I want to run shell command which require a lot of time but it displays some output so I know it doesn’t hang. However at the end I want to process this output in php. If I choose one of first two I won’t get output so I will be unable to process it in php. If I run one of the last two I will be able to process output however my program will hang very long time without outputting anything.

Is there a way to run a shell command which will display output immediately and return it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T12:42:12+00:00Added an answer on May 30, 2026 at 12:42 pm

    Maybe this one will interest you? proc_open() – http://www.php.net/manual/en/function.proc-open.php

    And here is a handy snippet which might work for you (it’s copied from the comments on the site I gave you the link to):

    <?php
    /*
     * Execute and display the output in real time (stdout + stderr).
     *
     * Please note this snippet is prepended with an appropriate shebang for the 
     * CLI. You can re-use only the function.
     * 
     * Usage example:
     * chmod u+x proc_open.php
     * ./proc_open.php "ping -c 5 google.fr"; echo RetVal=$?
     */
    define(BUF_SIZ, 1024);        # max buffer size
    define(FD_WRITE, 0);        # stdin
    define(FD_READ, 1);        # stdout
    define(FD_ERR, 2);        # stderr
    
    /*
     * Wrapper for proc_*() functions.
     * The first parameter $cmd is the command line to execute.
     * Return the exit code of the process.
     */
    function proc_exec($cmd)
    {
        $descriptorspec = array(
            0 => array("pipe", "r"),
            1 => array("pipe", "w"),
            2 => array("pipe", "w")
        );
    
        $ptr = proc_open($cmd, $descriptorspec, $pipes, NULL, $_ENV);
        if (!is_resource($ptr))
            return false;
    
        while (($buffer = fgets($pipes[FD_READ], BUF_SIZ)) != NULL 
                || ($errbuf = fgets($pipes[FD_ERR], BUF_SIZ)) != NULL) {
            if (!isset($flag)) {
                $pstatus = proc_get_status($ptr);
                $first_exitcode = $pstatus["exitcode"];
                $flag = true;
            }
            if (strlen($buffer))
                echo $buffer;
            if (strlen($errbuf))
                echo "ERR: " . $errbuf;
        }
    
        foreach ($pipes as $pipe)
            fclose($pipe);
    
        /* Get the expected *exit* code to return the value */
        $pstatus = proc_get_status($ptr);
        if (!strlen($pstatus["exitcode"]) || $pstatus["running"]) {
            /* we can trust the retval of proc_close() */
            if ($pstatus["running"])
                proc_terminate($ptr);
            $ret = proc_close($ptr);
        } else {
            if ((($first_exitcode + 256) % 256) == 255 
                    && (($pstatus["exitcode"] + 256) % 256) != 255)
                $ret = $pstatus["exitcode"];
            elseif (!strlen($first_exitcode))
                $ret = $pstatus["exitcode"];
            elseif ((($first_exitcode + 256) % 256) != 255)
                $ret = $first_exitcode;
            else
                $ret = 0; /* we "deduce" an EXIT_SUCCESS ;) */
            proc_close($ptr);
        }
    
        return ($ret + 256) % 256;
    }
    
    /* __init__ */
    if (isset($argv) && count($argv) > 1 && !empty($argv[1])) {
        if (($ret = proc_exec($argv[1])) === false)
            die("Error: not enough FD or out of memory.\n");
        elseif ($ret == 127)
            die("Command not found (returned by sh).\n");
        else
            exit($ret);
    }
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There are several PHP or js code formatting libs out there -- does anyone
There are several methods on country codes. I have a list of codes with
Writing a PHP app and have several classes that only have static methods (no
I am currently researching the best methods to integrate i18n into projects. There's several
I heard that in PHP there are some alternatives for the functions substr() and
I'm a rookie designer having a few troubles with this page: http://www.resolvegroup.co.nz/javasurvey.php There are
Is there php function to remove the space inside the string? for example: $abcd=this
IN PHP: Is there a way for the user to fake a session variable?
I had made some questions regarding PHP-GTK (there are only 4 php-gtk tagged questions
As per the title, is there PHP equivalent of __name__ == __main__ ? Is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.