Last active
December 19, 2015 22:38
-
-
Save thers/6028479 to your computer and use it in GitHub Desktop.
Simple non-blocking command runner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Simple non-blocking command runner | |
* | |
* Class RunBackground | |
*/ | |
class RunBackground { | |
/** | |
* Running command without waiting for it to complete | |
* | |
* @param string $cmd | |
* @param string $inDir | |
*/ | |
public static function command ($cmd, $inDir = '.') { | |
switch(static::detectOS()) { | |
case 'nix': | |
shell_exec("cd $inDir && $cmd > /dev/null 2>/dev/null &"); | |
break; | |
case 'win': | |
pclose(popen("cd $inDir && start /B $cmd", "r")); | |
break; | |
} | |
} | |
/** | |
* Easiest OS detection ever | |
* | |
* @return string | |
*/ | |
protected static function detectOS () { | |
return is_writeable("/dev/null") ? "nix" : "win"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment