CentOS6.5启停脚本例子

简介:
#!/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



本文转自 ygqygq2 51CTO博客,原文链接:http://blog.51cto.com/ygqygq2/1715528,如需转载请自行联系原作者

相关文章
|
Linux
CentOS7.9服务器一键脚本部署FRP内网穿透服务端与客户端
CentOS7.9服务器一键脚本部署FRP内网穿透服务端与客户端
1753 1
|
Java Linux Docker
centos7 开机自启动自定义脚本
centos7 开机自启动自定义脚本
388 0
|
Java Linux
centos启动jar脚本
centos启动jar脚本
247 0
|
Linux Shell
在Linux、CentOS7中设置shell脚本开机自启动服务
以上就是在CentOS 7中设置shell脚本开机自启动服务的全部步骤。希望这个指南能帮助你更好地管理你的Linux系统。
2381 25
|
Linux Shell
Centos或Linux编写一键式Shell脚本删除用户、组指导手册
Centos或Linux编写一键式Shell脚本删除用户、组指导手册
452 4
|
Linux Shell 数据安全/隐私保护
Centos或Linux编写一键式Shell脚本创建用户、组、目录分配权限指导手册
Centos或Linux编写一键式Shell脚本创建用户、组、目录分配权限指导手册
723 3
|
网络协议 Linux Python
Python脚本配置Centos静态ip地址
这是一个Python脚本,用于自动化配置CentOS系统的静态IP地址。脚本创建或修改文件,填写接口名(如ens33)、IP地址、子网掩码、网关和DNS。运行时需替换变量值并使用`sudo`以管理员权限执行。完成配置后,它会重启网络服务。注意,用户应根据实际网络环境调整参数。
457 4
Python脚本配置Centos静态ip地址
|
网络协议 Shell Linux
Shell脚本配置Centos静态ip地址
这是一个用于在CentOS上设置静态IP的Shell脚本摘要: - 脚本交互式获取用户输入的IP地址、子网掩码、网关和DNS。 - 使用`sed`命令动态更新`/etc/sysconfig/network-scripts/ifcfg-ENS33`配置文件。 - 修改`BOOTPROTO`为`static`,并设置IP、NETMASK、GATEWAY和DNS1字段。 - 用`systemctl restart network`重启网络服务。 - 提示用户新配置的静态IP信息。
749 5
|
Ubuntu Java Linux
Linux centos7 ubuntu 一键安装Java JDK 脚本 shell 脚本
Linux centos7 ubuntu 一键安装Java JDK 脚本 shell 脚本
580 2
|
Ubuntu 应用服务中间件 Linux
Linux Centos7 ubuntu 安装nginx,脚本一键安装nginx
Linux Centos7 ubuntu 安装nginx,脚本一键安装nginx
562 2