How to execute shell commands from php and do not wait for output on WINDOWS

April 09, 2021, at 2:10 PM

I have a mask detection python program. Whenever we run that, it keeps running continuously and keeps detecting (endless program). I have made a web portal to start off this python program on a button click event using php. Now the issue is that when i start off this program by shell_exec() , it starts. But php waits for the program to finish. Till the time program is running (from shell_exec() ), php just freezes, doesn't even load any other page.

So how to run cmd command from shell_exec() and not wait for it to finish executing?


$command = escapeshellcmd("start cmd /c python " );
$output = shell_exec($command);
Answer 1

This can be achieved in multiple ways, one relatively easy one I could think of is using PIPE and continuously reading in the results. If you

for example:

from subprocess import PIPE, run
cmd = [python3, "" ...]
result = run(cmd, stdout=PIPE, stderr=PIPE, universal_newlines=True)

now you could redirect the standard output to a file as shown here (specially this post) depending on OS and Python version.

Now, you can read this file (or a copy of it) every x seconds and send it to PHP.

Answer 2

I found and easy solution for my question on this blog

Thanks to this genius

Solution given here is this function

function <span style="color: red;">bgExec</span>($cmd) {
 if(substr(php_uname(), 0, 7) == "Windows"){
  pclose(popen("start /B ". $cmd, "r")); 
 }else {
  exec($cmd . " > /dev/null &"); 

I tried redirecting my stdout to null, but it didn't worked for me in Windows platform. Above function uses popen() and pclose() functions. That gets my job done.

pclose(popen("start cmd /c python", "r")); 
Add Dropdown filter for a table that is generated using CSV data

Add Dropdown filter for a table that is generated using CSV data

I have only recently dived into the world of web development, so I am not well versed in all the code etc

Image Zoom with JS using a button

Image Zoom with JS using a button

First of all, I apologize if my title is not understandable, English is not my first languageMy main goal is to have an image modal and with a zoom button and if you click the zoom button the image modal will appear

How to handle static in java

How to handle static in java

I got into programming a bit obliquely with Bukkit and thus didn't learn some things properlyBut since I've been doing real stuff for a while now I wanted to ask how to deal with static