SimonTech Development
  • Contact Us
  • Request Quote
  • Consulting
      • Back
      • CMS Strategy
      • Custom Development
      • Server Management
      • Integrated Automation
  • Development & Design
      • Back
      • Joomla CMS
      • WordPress CMS
      • Custom Application Development
      • Marketing Automation
  • Website Maintenance
      • Back
      • Managed Cloud Hosting
      • CMS Maintenance
      • Site Security Mitigation
      • Backup, Repair & Restore
  • Plans & Pricing
  • Support
Account
Please wait, authorizing ...
Not a member? Sign up now
×

Knowledge Base

PHP

PHP long running background process - Realtime output [1]

Details
Parent Category: Knowledge base
Category: PHP

 Trying to run a Background PHP process on a web page that will return its output in realtime?
Try something like this example of a basic shell execution from PHP using proc_open():

$cmd = "ping 127.0.0.1";

$descriptorspec = array(
   0 => array("pipe", "r"),   // stdin is a pipe that the child will read from
   1 => array("pipe", "w"),   // stdout is a pipe that the child will write to
   2 => array("pipe", "w")    // stderr is a pipe that the child will write to
);
flush();
$process = proc_open($cmd, $descriptorspec, $pipes, realpath('./'), array());
echo "<pre>";
if (is_resource($process)) {
    while ($s = fgets($pipes[1])) {
        print $s;
        flush();
    }
}
echo "</pre>";

 

if you want to use the PHP exec(), you can send output to a file and read the last line of output file using ajax calls at regular intervals or using SSE:

$result = exec('$cmd > /path/to/file &');

The ampersand at the end will make it run in the background and you can read the output in /path/to/file

  • Services
  • Support Tickets
  • Knowledge Base
  • Extensions and Releases
  • Contact Us
Facebook LinkedIn Twitter Github
  • Terms & Conditions
  • Privacy Policy
© 2008 - 2025 SimonTech Development