Executing scripts/programs fro …

Executing scripts/programs from a web page can be troublesome and fraught with security issues. Executing long running scripts is troublesome as well as the webpage appears to never stop loading! Here’s how to get around it in PHP.
Usually I use exec() or system() to run external script but the script I wanted to run this time wasn’t supposed to exit! I had to find another way. I used popen():

$cmd = "command &"
$fp = popen ( $cmd, "r");
sleep( 1 );

That will run the command, wait 1 second and then close the pipe to the command. A message on the popen() manual page above warns that file descriptors may be left open and the Apache process may not respond to further queries but I haven’t tested that.

Author: Donncha

Donncha Ó Caoimh is a software developer at Automattic and WordPress plugin developer. He posts photos at In Photos and can also be found on Google+ and Twitter.

Leave a Reply