WordPress has a built in maintenance mode. You’ve most likely seen it at some point while updating WordPress, themes or plugins.
While meant to protect WordPress from itself while doing something critical, it’s also useable for other situations where you want to change active PHP files. Using SSH you can easily enable and disable maintenance mode by creating and deleting the .maintenance
file. Here are some examples of what that looks like:
# Creates .maintenance file - Maintenance mode enabled
echo '<?php $upgrading = time(); ?>' | ssh remote-site-address "cat > .maintenance"
# Deletes .maintenance file - Maintenance mode disabled
ssh remote-site-address "rm .maintenance"
Maintenance mode over SSH is a perfect for migrations, rollbacks and mu-plugin deployments
Let’s assume you want to rollback your entire plugins folder to previous version. If you delete the plugins folder directly on your server the WordPress site will break and automatically deactivate all plugins. To prevent that from happening you can programmatically enable maintenance mode, delete plugins folder, extract plugins from a snapshot and then disable maintenance mode. The following is a section of code taking from my rollback script which does just that.
echo '<?php $upgrading = time(); ?>' | captaincore ssh $website --command="cat > .maintenance; rm -rf wp-content/themes/; unzip -o quicksave_${commit}_all.zip 'themes/*' -d wp-content/; rm -rf wp-content/plugins/; unzip -o quicksave_${commit}_all.zip 'plugins/*' -d wp-content/; rm -rf wp-content/mu-plugins/; unzip -o quicksave_${commit}_all.zip 'mu-plugins/*' -d wp-content/; rm .maintenance"