#!/usr/bin/php "/var/cache/nginx/cachedemo-prod", "cachedemo.example.com" => "/var/cache/nginx/cachedemo-prod", "cachedemo-stage" => "/var/cache/nginx/cachedemo-stage", "cachedemo-stage.example.com" => "/var/cache/nginx/cachedemo-stage", ); $siteArg = isset($argv[1])? $argv[1] : null; if (!isset($sites[$siteArg])) { echo "Valid site argument required: \n\n"; foreach ($sites as $key => $value) { echo " $key\n"; } echo "\nInvalid site argument: '$siteArg'"; exit(1); } $siteName = $argv[1]; $siteCachePath = $sites[$siteName]; $log = date("h:i:s A T") . " - $siteName - "; $descriptorspec = array( 0 => array("pipe", "r"), // stdin 1 => array("pipe", "w"), // stdout 2 => array("pipe", "w"), // stderr ); // execute Linux system process $process = proc_open("find \"$siteCachePath\" -type f -exec rm -v {} \; | wc -l", $descriptorspec, $pipes, dirname(__FILE__), null); $stdout = stream_get_contents($pipes[1]); $stderr = stream_get_contents($pipes[2]); fclose($pipes[1]); fclose($pipes[2]); if ($stderr) { $log .= "ERROR $stderr"; } else { $log .= $stdout; } // Log to file file_put_contents('/var/log/nginx/cache-clear.log', "$log", FILE_APPEND); // Output total cleared echo $stdout;