Asynchronous shell exec in PHP

前端 未结 13 2021

I\'ve got a PHP script that needs to invoke a shell script but doesn\'t care at all about the output. The shell script makes a number of SOAP calls and is slow to complete,

13条回答
  •  花落未央
    2020-11-22 01:15

    the right way(!) to do it is to

    1. fork()
    2. setsid()
    3. execve()

    fork forks, setsid tell the current process to become a master one (no parent), execve tell the calling process to be replaced by the called one. so that the parent can quit without affecting the child.

     $pid=pcntl_fork();
     if($pid==0)
     {
       posix_setsid();
       pcntl_exec($cmd,$args,$_ENV);
       // child becomes the standalone detached process
     }
    
     // parent's stuff
     exit();
    

提交回复
热议问题