1

I have two functions, one runs a thread in the background with exec() and returns output

function runThread($cmd, $priority = 0){
    if($priority){
        exec(sprintf('nohup nice -n %s %d > /dev/null 2>&1 & echo $! &',$priority, $cmd),$pid);
    }else{
        exec(sprintf('nohup %s > /dev/null 2>&1 & echo $! &',$cmd),$pid);
    }
    return $pid[0];
}

The other monitors the thread using shell_exec() and returns false when it completes

function isRunning($pid){
    try{
        $result = shell_exec(sprintf("ps %s", $pid));
        if( count(preg_split("/\n/", $result)) > 2){
            return true;
        }
    }catch(Exception $e){}

    return false;
}

Instead of returning true or false, I'd like to return values based off the time it takes to run the entire thread so I can incorporate an AJAX progress bar e.g. return 20%, return 40% etc.. Is this possible with shell_exec() or should I use something like proc_open() and how can the current state of the thread be returned in real time?

Thanks for your help

Dan
  • 425
  • 2
  • 11

0 Answers0