Skip to content

Instantly share code, notes, and snippets.

@avargas
Created April 24, 2012 21:19
Show Gist options
  • Save avargas/2483901 to your computer and use it in GitHub Desktop.
Save avargas/2483901 to your computer and use it in GitHub Desktop.
php script rerun on saved changes
<?php
function monitor ($file)
{
$last = filemtime($file);
$command = trim(`which php`);
$command .= " -f " . $file;
$command .= ' 2>&1';
$i = 1;
$out = 0;
$clear = implode('', array_map('chr', array(27, 91, 72, 27, 91, 50, 74)));
$line = str_repeat('-', exec('tput cols'));
echo $clear;
while (true) {
$cols = exec('tput cols');
echo date('r') . ' running for #' . $i++ . ': ' . $command ."\n";
echo $line;
passthru($command, $out);
echo "\n$line";
# success
echo "Exit status: $out\n";
# loop and wait til file changes so we can re-run it
while (true) {
clearstatcache();
$new = filemtime($file);
if ($new != $last) {
break;
}
# sleep for 1 secs if previous run died, or else .4 seconds
usleep($out != 0 ? 1000000 : 400000);
}
$last = $new;
echo $clear;
}
}
monitor($argv[1]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment