Shell Script to reboot multiple servers.

Here is simple bash script where you can add the server names in the command line and then it will go on rebooting every server which you have listed.

Script Usage:-

[[email protected] /]# ./server-reboot.sh appserver1 sqlserver1 dataserver1

Above command will reboot appserver1 sqlserver1 and dataserver1, if you have not setup ssh keys it will ask for password.

To use this script create a file called server-reboot.sh and following contents.

#!/bin/bash
for server in "${@}"
do
{
scriptName=$0
scriptPath=$(dirname $0)
script=$scriptPath/boot.txt
command=`base64 -w0 $script`
ssh -t $server "echo $command | base64 -d | sudo bash"
}
done

Create a another file named boot.txt in the same location where you have created server-reboot.sh.

for ((i=03; i>0; i--)); do sleep 1 ; printf "\rREBOOTING SERVER in $i Second. PRESS CTRL + C to cancel " ; done
shutdown -rf now

Save both the files, give execute permissions to server-reboot.sh and start rebooting.

It’s just not reboot script but you can add any commands which you want to run on multiple Linux servers in the file boot.txt

There are many other ways to do the same task but this works best for my environment. Add in comments what works best for you.

Leave a Reply

Your email address will not be published. Required fields are marked *