Asynchronous Shell Exec In PHP


Answer :

If it "doesn't care about the output", couldn't the exec to the script be called with the & to background the process?

EDIT - incorporating what @AdamTheHut commented to this post, you can add this to a call to exec:

" > /dev/null 2>/dev/null &" 

That will redirect both stdio (first >) and stderr (2>) to /dev/null and run in the background.

There are other ways to do the same thing, but this is the simplest to read.


An alternative to the above double-redirect:

" &> /dev/null &" 

I used at for this, as it is really starting an independent process.

<?php     `echo "the command"|at now`; ?> 

To all Windows users: I found a good way to run an asynchronous PHP script (actually it works with almost everything).

It's based on popen() and pclose() commands. And works well both on Windows and Unix.

function execInBackground($cmd) {     if (substr(php_uname(), 0, 7) == "Windows"){         pclose(popen("start /B ". $cmd, "r"));      }     else {         exec($cmd . " > /dev/null &");       } }  

Original code from: http://php.net/manual/en/function.exec.php#86329


Comments

Popular posts from this blog

Are Regular VACUUM ANALYZE Still Recommended Under 9.1?

Can Feynman Diagrams Be Used To Represent Any Perturbation Theory?