Skip to content

Instantly share code, notes, and snippets.

@damienalexandre
Created October 20, 2011 10:13
Show Gist options
  • Save damienalexandre/1300820 to your computer and use it in GitHub Desktop.
Save damienalexandre/1300820 to your computer and use it in GitHub Desktop.
Run a shell command in backgroud using PHP
<?php
/**
* Run a command in background (Windows and linux \o/)
*
* @param wait wait for the command to terminate
* @author dalexandre
* @author jbenoist
* @since 24/02/2009
* @update 19/08/2009
* @param String $cmd
*/
public function runCmdInBackground($cmd, $outputfile = null, $wait = false)
{
if($outputfile === null)
$outputfile = sfConfig::get('sf_root_dir') . '/log/cmdBackground_'.date('Y_m_d_h_i_s_').rand(0, 10).'.log';
elseif ($outputfile === false)
$outputfile = '/dev/null';
if(substr(php_uname(), 0, 7) != 'Windows')
{
$wait = $wait ? '' : (' >> ' . $outputfile . ' &');
exec('nohup ' . $cmd . $wait);
}
else
{
$wait = $wait ? ' /WAIT ' : ' ';
pclose(popen('start /B' . $wait . $cmd . ' >> ' . $outputfile, 'r'));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment