Bit rough but,
What you need to do is to write a script that basically does what index.php does at its top end, so take this as your base, get rid of the micro time cruft and set IN_PARSER_MODE to false and IN_API_MODE to true, get rid of the ob_start, you should now have an initialized modx object, get rid of the handle request at the bottom and put your code here. To clear the cache use the code from \core\model\modx\processors\system\clearcache.php i.e do this :-
$paths = array(
'config.cache.php',
'sitePublishing.idx.php',
'registry/mgr/workspace/',
'lexicon/',
);
$contexts = $modx->getCollection('modContext');
foreach ($contexts as $context) {
$paths[] = $context->get('key') . '/';
}
$options = array(
'publishing' => 1,
'extensions' => array('.cache.php', '.msg.php', '.tpl.php'),
);
if ($modx->getOption('cache_db')) $options['objects'] = '*';
$results= $modx->cacheManager->clearCache($paths, $options);
then to set the site unavailable you need to set the system setting site_status to 0, do this by updating this setting something like :-
$setting = $modx->getObject('modSystemSetting',
array ('key' => 'site_status',
'namespace' => 'core'));
$setting->set('value', 0);
$setting->save();
}
and see how it goes.