Bit Byte Bit

Mike Stephens –
Web developer based in Manchester

www.bitbytebit.co.uk

mike [at] bitbytebit.co.uk

+44(0)7545 917336

UPS Remote Shutdown Bash Script

UPS Remote Shutdown Bash Script

This is a simple bash script that I wrote a few months back that remotely shuts down servers via SSH upon being executed. It is intended to be used as a shutdown script that should be called when a UPS sends a signal to a server to indicate that is should be shut down.

We have a pretty large APC UPS running 24×7 in work which powers a bunch of servers running Mac and Linux. By default, the UPS only has the option for one signal cable which is currently attached to our main server. You can purchase additional expansion cards and connect more signal cables to other servers however we didn’t really want to pay hundreds of pounds just for this privileged. Especially when it can be achieved so easily via bash scripting.

This may be a little specific to our setup however it can be easily adaptable. As you can see, we are also shutting down a FileMaker Pro Server and closing the databases correctly to stop any data corruption.

This script can be merged/replace the default script located at: /usr/libexec/upsshutdown (on Mac OS X)

Relatively new to bash scripting so if there are bugs / better methods please let me know!

You can also find this script on my github account.

#!/bin/bash
clear

#************************************************#
# ups_shutdown.sh #
# written by Mike Stephens #
# bitbytebit.co.uk #
# Jan 05, 2011 #
# #
# UPS Remote Shutdown Script #
# #
# This script is to be executed on a server #
# with a UPS attached. It will connect to #
# remote servers via SSH and send appropiate #
# commands. Finally, it will shutdown the #
# server exeuting the script. #
# #
# NOTE: Using SSH key auth so don't have to #
# enter credentials. #
#************************************************#

MSG="Shutting down due to power loss!"

echo "Running UPS shutdown script..."

# -------------------------------------- #
# svr01 commands #
# -------------------------------------- #

echo "Connecting to server 'svr01' via SSH..."

#Connect to 'svr01'
ssh root@192.168.100.10 -T <<\EOF

echo "Attempting to close FileMaker Pro Server and databases"

#Force close all open databases
fmsadmin close -m 'Power disruption. FileMaker Pro closing immediately.' -y -u admin -p password

#Stop FMP Server
fmsadmin stop -m 'Power disruption. FileMaker Pro shutting down immediately.' -y -u admin -p password

echo "FileMaker Pro Server databases closed and server stopped"

#Send shutdown command
shutdown -h now "${MSG}"

#Logout
logout

EOF

echo "Server 'svr01' has been sent the shutdown command"

# -------------------------------------- #
# svr02 commands #
# -------------------------------------- #

sleep 2

echo "Connecting to server 'svr02' via SSH..."

#Connect to 'svr02'
ssh root@192.168.100.11 -T <<\EOF

#Send shutdown command
shutdown -h now "${MSG}"

#Logout
logout

EOF

echo "Server 'svr02' has been sent the shutdown command"

# -------------------------------------- #
# svr03 commands #
# -------------------------------------- #

sleep 2

#Send echo before we shut down this server...
echo "Server 'svr03' has been sent the shutdown command"

#Send shutdown command to THIS server
shutdown -h now "${MSG}"

Post a comment.