Bash shell 服务通用控制脚本

脚本1

#!/bin/sh
ELF_PATH="/onlineapp/cut_server/server.py"
EXEC_CMD="/usr/bin/python $ELF_PATH"
#function
start(){
	#test have started
	PRO_PID=`ps -ef | grep -v grep | grep $ELF_PATH | awk '{print $2}'`
	if [ "$PRO_PID" != "" ]; then
		echo "$ELF_PATH have started!"
		exit 1
	fi
	#start
	nohup $EXEC_CMD &
}

stop(){
	echo $ELF_PATH
	PRO_PID=`ps -ef | grep -v grep | grep $ELF_PATH | awk '{print $2}'`
	echo $PRO_PID
	if [ "$PRO_PID" != "" ]; then
		for curpid in $PRO_PID
		do
			echo $curpid
			kill -9 $curpid
		done
		echo "$ELF_PATH stoped!"
	else
		echo "PID Not Found !"
	fi
}

case $1 in
	"start")
		start
	;;
	"stop")
		stop
	;;
	"restart")
            stop
            echo "3"
            sleep 1
            echo "2"
            sleep 1
            echo "1"
            sleep 1
            start
	;; 
	*)
		echo "./shell param"
		echo "    start         start Server"
		echo "    stop          stop Server"
	;;
esac

ps -A -opid,stime,etime,args | grep $ELF_PATH | grep -v grep

exit 0

脚本2

#!/bin/bash
ES_HOME="/developenv/elk/elasticsearch-7.11.1"
APP_NAME="elasticsearch"
echo "开始执行 $APP_NAME 控制脚本"
# 使用方式
usage() {
	echo "case:sh run.sh [start | stop | restart | status]"
	echo "请类似这样执行 ./*.sh start or ./*sh restart"
	exit 1
}

# 判断当前服务是否已经启动的函数
is_exist() {
	echo "执行 is_exist方法"
	pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}'`
	if [ -z "${pid}" ]; then
		echo "pid is null."
		return 1
	else
		echo "pid <> null"
		return 0
	fi
}
# 启动
start() {
	is_exist
	if [ $? -eq 0 ]; then
		echo "${APP_NAME} running. pid=${pid}"
	else
	    su elk -c "sh ${ES_HOME}/bin/elasticsearch -d -p ${ES_HOME}/pid"
		echo "${APP_NAME} started"
	fi
}
# 停止
stop() {
	echo "执行stop方法"
	is_exist
	if [ $? -eq 0 ]; then
		kill `cat ${ES_HOME}/pid`
		echo "${pid} stop"
	else
		echo "${APP_NAME} not running"
	fi
}
#重启
restart() {
	stop
	start
}
case "$1" in 
	"start")
		start
		;;
	"stop")
		stop
		;;
	"restart")
		restart
		;;
	*)
		usage
		;;
esac

脚本3

#!/bin/bash
BEATHOME="/developenv/elk/filebeat-7.11.1"
APP_NAME="filebeat"
AGENT="$BEATHOME/filebeat"
LOGFILE="$BEATHOME/filebeat.log"
ARGS="-e -c $BEATHOME/filebeat.yml"

start() {
    pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}'`
    if [ -z "${pid}" ];then
        echo "Starting filebeat: "
        nohup $AGENT $ARGS > $LOGFILE &
        if [ $? == '0' ];then
            echo "start filebeat ok"
        else
            echo "start filebeat failed"
        fi
    else
        echo "filebeat is still running!"
        exit
    fi
}

stop() {
    echo -n $"Stopping filebeat: "
    pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}'`
    if [ -z "${pid}" ];then
	echo "filebeat is not running"
    else
        kill $pid
	echo "stop filebeat ok"
    fi
}

restart() {
    stop
    start
}

status(){
    pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}'`
    if [ -z "${pid}" ];then
        echo "filebeat is not running"
    else
        echo "filebeat is running"
    fi
}
case "$1" in
    start)
        start
    ;;
    stop)
        stop
    ;;
    restart)
        restart
    ;;
    status)
        status
    ;;
    *)
        echo $"Usage: $0 {start|stop|restart|status}"
        exit 1
esac

脚本4

#!/bin/bash
SERVER_HOME="/developenv/elk/kibana-7.11.1"
LOG_HOME=$SERVER_HOME/logs
BIN_HOME=$SERVER_HOME/bin

START_SHELL=$BIN_HOME/kibana
PIDFILE=$BIN_HOME/server.pid
CONSOLE=$LOG_HOME/kibana-console_`date '+%Y-%m-%d'`.log
test -d $LOG_HOME || mkdir -p $LOG_HOME

start()
{
        echo "staring ..."
	nohup $START_SHELL >$CONSOLE 2>&1  &
	echo $! > $PIDFILE
}

stop()
{
if [ -f "$PIDFILE" ] ; then
    echo "kibana stoping.."    
    kill `cat $PIDFILE`
    sleep 1
    rm -rf $PIDFILE
    echo "stop kibana success"
  else
    echo "kibana is not running"
    exit 0;
  fi;
}

status()
{
    if [ -f "$PIDFILE" ] ; then
       pid=`cat $PIDFILE`
       if [[ pid -gt 0 ]]
       then
           echo "[$(date '+%Y-%m-%d %T')] kibana is running.( pid:$pid )"
       else
            echo "[$(date '+%Y-%m-%d %T')] kibana is not running"
       fi
    else 
         echo "kibana is not running"
    fi
}

restart()
{
   stop
   start
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
         status
        ;;
    restart)
        restart
        ;;
    *)
     echo "Usage: bash ./server.sh {start|status|stop|restart}"
        exit 1
        ;;
esac
exit 0

You May Also Like

About the Author: 一块自由的砖

码农一个,一块自由勤恳的砖,哪里需要哪里搬!( ̄▽ ̄)"