Skip to content

Instantly share code, notes, and snippets.

@thers
Last active December 19, 2015 22:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thers/6028479 to your computer and use it in GitHub Desktop.
Save thers/6028479 to your computer and use it in GitHub Desktop.
Simple non-blocking command runner
<?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