Often, these tasks impact the "production" website, or conversely, the "production" website often impacts these tasks. Either way, sometimes it's best to get the site offline for a minute or two, to let the maintenance task run quickly and to completion, without competition.
I thought the approach below was totally obvious, but I've learned that a lot of people are really happy to learn how to do this sort of thing.
Say your website document root is:
/var/www/websiteAnd say your "sorry" website is:
/var/www/sorryWe'll make a script called /root/switch.
#!/bin/shSay your existing cron job is:
site = /var/www/website
sorry = /var/www/sorry
hold = /var/www/hold
if [ -d /var/www/sorry ]; then
mv $site $hold
mv $sorry $site
else
mv $site $sorry
mb $hold $site
fi
0 0 * * * /do/my/maintenance >/dev/null 2<&1To put the sorry site in place while the maintenance is running, just change that to:
0 0 * * * /root/switch; /do/my/maintenance; /root/switch >/dev/null 2<&1
The above simply calls the "switch" script twice - once before, and once after, the maintenance script. It keeps all of the details of what "switch" actually does hidden away from the cron job, as a good programming practice.
The above approach lets you customize your "sorry site" - some of the pages can say "We're down for maintenance" (say, the main page) and other pages can still work (say... for example... the pages that let people check out :-)
If you just want to take ALL pages offline, there's a simpler way - setup a variant .htaccess file and swap that in place, instead of moving the directories around.