当前位置: > shell编程 >

CentOS6.5下启停shell脚本例子

时间:2015-11-23 00:53来源:blog.51cto.com 作者:ygqygq2 的BLOG 举报 点击:
#!/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)

#定义环境变量
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/

#定义输出颜色函数
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
发表评论
为了和诣的生活,我关闭了评论页面,请大家到QQ群里交流吧:348944156,也欢迎关注本站微信公众号:centoscn
栏目分类