简体   繁体   中英

Open and close process in Laravel

In web.php I start the process in this route

Route::get('/startProcess',function(){
   $descriptorspec=.......
   $process1 = proc_open('php C:\Process1.php',$descriptorspec,$pipes);

   // Here I need to save a reference to the process - $process1
});

I need to close this process in another route

Route::get('/stopProcess',function(){

   // Here I need to get a reference to the process - $process1

   proc_close( $process1 )
});

Where do I save a reference to the process and how do I get it?

proc_open returns a resource, and resources can't be serialised so I don't think you can retrieve the resource again after the request ends.

Here is a potential solution:

Route::get('/startProcess',function(){
   $descriptorspec=.......
   $process1 = proc_open('php C:\Process1.php',$descriptorspec,$pipes);
   $status = proc_get_status ($process1);
   return response()->json([
      'pid' => $status['pid'];
   ]);
});

Then you can use the PID retrieved to kill the process using a shell operation:


Route::get('/stopProcess/{pid}',function($pid){
     shell_exec("kill -9 $pid");
});

You can call the later as https://example.com/stopProcess/12345

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM