#!/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 #獲取腳本所存放目錄
cd `dirname $0`
bash_path=`pwd`
#腳本名
me=$(basename $0)
#定義輸出顏色函數
function red_echo () {
#用法: red_echo "內容"
local what=$*
echo -e "$(date +%F-%T) \e[1;31m ${what} \e[0m"
}
function green_echo () {
#用法: green_echo "內容"
local what=$*
echo -e "$(date +%F-%T) \e[1;32m ${what} \e[0m"
}
function yellow_echo () {
#用法: yellow_echo "內容"
local what=$*
echo -e "$(date +%F-%T) \e[1;33m ${what} \e[0m"
}
function twinkle_echo () {
#用法: twinkle_echo $(red_echo "內容") ,此處例子爲紅色閃爍輸出
local twinkle='\e[05m'
local what="${twinkle} $*"
echo -e "$(date +%F-%T) ${what}"
}
function return_echo () {
[ $? -eq 0 ] && green_echo "$* 成功" || red_echo "$* 失敗"
}
function return_error_exit () {
[ $? -eq 0 ] && local REVAL="0"
local what=$*
if [ "$REVAL" = "0" ];then
[ ! -z "$what" ] && green_echo "$what 成功"
else
red_echo "$* 失敗,腳本退出"
exit 1
fi
}
#定義確認函數
function user_verify_function () {
while true;do
echo ""
read -p "是否確認?[Y/N]:" Y
case $Y in
[yY]|[yY][eE][sS])
echo -e "answer: \\033[20G [ \e[1;32m是\e[0m ] \033[0m"
break
;;
[nN]|[nN][oO])
echo -e "answer: \\033[20G [ \e[1;32m否\e[0m ] \033[0m"
exit 1
;;
*) continue ;;
esac
done
}
#定義跳過函數
function user_pass_function () {
while true;do
echo ""
read -p "是否確認?[Y/N]:" Y
case $Y in
[yY]|[yY][eE][sS])
echo -e "answer: \\033[20G [ \e[1;32m是\e[0m ] \033[0m"
break
;;
[nN]|[nN][oO])
echo -e "answer: \\033[20G [ \e[1;32m否\e[0m ] \033[0m"
return 1
;;
*) continue ;;
esac
done
}
#cd到程序目錄
program_path=$(dirname $bash_path)
program_name="play"
function start () {
#cd $bash_path/../logfile/
#判斷程序是否運行
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(){
#判斷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