CentOS6.5 start and stop under shell script example


#!/bin/bash
# The next lines are for chkconfig on RedHat systems.
# chkconfig: 35 98 02
# description: Starts and stops Server
# The next lines are for chkconfig on SuSE systems.
# /etc/init.d/xxx
#
### BEGIN INIT INFO
# Provides: Chinge_Yang
# Required-Start: $network $syslog
# Required-Stop:
# Default-Start: 2 3 5
# Default-Stop: 0 6
# Short-Description: Starts and stops Server
# Description: Starts and stops Server
### END INIT INFO
# Gets the scripts directory
cd ‘dirname $0’
bash_path=’pwd’
# Script name
me=$(basename $0)
# Define environment variables
export ORACLE_BASE=/opt/oracle/app
export ORACLE_HOME=$ORACLE_BASE/oracle/product/11.2.0/dbhome_1
export PATH=$PATH:$ORACLE_HOME/bin
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:/usr/lib
export TNS_ADMIN=$ORACLE_HOME/network/admin/
# Define output color functions
function red_echo () {
# Usage: red_echo content
local what=$*
echo -e “$(date +%F-%T) \e[1;31m ${what} \e[0m”
}
function green_echo () {
# Usage: green_echo content
local what=$*
echo -e “$(date +%F-%T) \e[1;32m ${what} \e[0m”
}
function yellow_echo () {
# Usage: yellow_echo content
local what=$*
echo -e “$(date +%F-%T) \e[1;33m ${what} \e[0m”
}
function twinkle_echo () {
# Usage: twinkle_echo $ (red_echo ), here are examples for the Red Flash output
local twinkle=’\e[05m’
local what=”${twinkle} $*”
echo -e “$(date +%F-%T) ${what}”
}
function return_echo () {
[$?-EQ 0] && green_echo $* success | | Red_echo $* failed
}
function return_error_exit () {
[ $? -eq 0 ] && local REVAL=”0″
local what=$*
if [ “$REVAL” = “0” ];then
[!-Z $what] && green_echo $what success
else
Red_echo $* fails, the script exits
exit 1
fi
}
# Define confirmation function
function user_verify_function () {
while true;do
echo “”
Are you sure? [Y/N]:” Y
case $Y in
[yY]| [yY] [eE] [sS])
Answer: \\033[20G [\e[1;32M is \e[0m] \033[0m
break
;;
[nN]| [nN] [oO])
Answer: \\033[20G [\e[1;32M any \e[0m] \033[0m
exit 1
;;
*) continue ;;
esac
done
}
# Define skip function
function user_pass_function () {
while true;do
echo “”
Are you sure? [Y/N]:” Y
case $Y in
[yY]| [yY] [eE] [sS])
Answer: \\033[20G [\e[1;32M is \e[0m] \033[0m
break
;;
[nN]| [nN] [oO])
Answer: \\033[20G [\e[1;32M any \e[0m] \033[0m
return 1
;;
*) continue ;;
esac
done
}
#CD to the program directory
program_path=$(dirname $bash_path)
program_name=”play”
function start () {
#cd $bash_path/.. /logfile/
# Determine whether the program runs
program_pid=$(pidof $program_path/$program_name)
if [[ -z “$program_pid” ]];then
cd $program_path
nohup ./$program_name &
else
yellow_echo “This program $program_path/$program_name is running,no need to start”
exit 0
fi
program_pid=$(pidof $program_path/$program_name)
[[ ! -z “$program_pid” ]] && green_echo “This program start successed” || return_error_exit “This program start failed.”
}
function stop(){
# Determine the PID
program_pid=$(pidof $program_path/$program_name)
[[ -z “$program_pid” ]] && yellow_echo “This program $program_path/$program_name is not running.” || { kill -9 $program_pid && red_echo “This program $progra
m_path/$program_name be end.”; }
}
function status(){
program_pid=$(pidof $program_path/$program_name)
[[ -z “$program_pid” ]] && yellow_echo “This program is not running” || green_echo “This program $program_path/$program_name is running”
}
case $1 in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
stop
start
;;
*)echo “$0 {start|stop|restart|status}”
exit 4;
;;
esac