No, this cannot be done using exec() (and shell_exec() or system() )
If you have the pcntl extension installed , it will be:
function detached_exec($cmd) { $pid = pcntl_fork(); switch($pid) { // fork errror case -1 : return false // this code runs in child process case 0 : // obtain a new process group posix_setsid(); // exec the command exec($cmd); break; // return the child pid in father default: return $pid; } }
Name it as follows:
$pid = detached_exec($cmd); if($pid === FALSE) { echo 'exec failed'; } // do some work // kill child posix_kill($pid, SIGINT); waitpid($pid, $status); echo 'Child exited with ' . $status;
hek2mgl
source share