Skip to content

Instantly share code, notes, and snippets.

@dobrivoje
Created December 22, 2018 14:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dobrivoje/932056acc020876706d68150360df8e3 to your computer and use it in GitHub Desktop.
Save dobrivoje/932056acc020876706d68150360df8e3 to your computer and use it in GitHub Desktop.
Linux Servers Restart Script
#!/bin/bash
SERVER_DESCRIPTION="Nis Test"
LOG_RESTART="/root/scripts/logs/restart.log"
clear
printf "\n\n\n"
printf "Please, choose server to restart.\n
Available servers : \n
[1] Tomcat\n[2] TomcatDM\n[3] PostgresSQL
[4] ActiveMQ\n[5] Redis\n[6] MongoDB\n\n[0] Exit\n"
echo ______________________________________________________
echo
read -p "Please, choose a server : " op
if [ $op -eq 1 ]
then
### Process ID identification.
### Among possibly many instances of the same program, (eg. many apache tomcats)
### the exact instance PID id may be determined using top, htop or ps...
PROCES_IDENT="/usr/local/tomcat/conf/logging.properties"
server="Tomcat"
comm='/etc/init.d/tomcat'
elif [ $op -eq 2 ]
then
PROCES_IDENT="/usr/local/tomcatDM"
server="TomcatDM"
comm='/etc/init.d/tomcatDM'
elif [ $op -eq 3 ]
then
PROCES_IDENT="/usr/lib/postgresql/9.3"
server="TomcatDM"
comm='/etc/init.d/postgresql'
elif [ $op -eq 4 ]
then
PROCES_IDENT="/usr/local/activemq/apache-activemq"
server="ActiveMQ"
comm='service activemq'
elif [ $op -eq 5 ]
then
PROCES_IDENT="/usr/bin/redis-server"
server="Redis"
comm='service redis-server'
elif [ $op -eq 6 ]
then
PROCES_IDENT="/usr/bin/mongod"
server="MongoDB"
comm='service mongod'
elif [ $op -eq 0 ]
then
printf "Exit\n\n"
exit
else
errMsg="There is no server by the chosen number $op"
echo
echo $errMsg
printf "\n\n"
echo $errMsg >> $LOG_RESTART
echo "------------------------------------------------------------------" >> $LOG_RESTART
exit
fi
echo "$SERVER_DESCRIPTION : Server [$server] -> stop : `date '+[%F %X]'` " >> $LOG_RESTART
ps -ef | grep $PROCES_IDENT | awk '{print $2}' | xargs kill -9
echo "$SERVER_DESCRIPTION : Server [$server] -> Waiting for 5 sec to end process tree..." >> $LOG_RESTART
sleep 5
echo "$SERVER_DESCRIPTION : Server [$server] -> start : `date '+[%F %X]'` " >> $LOG_RESTART
echo "------------------------------------------------------------------" >> $LOG_RESTART
sh -c `$comm start`
# echo `$comm start`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment