본문 바로가기
ELK/ElasticSearch

ELK 서비스 Bash 실행 리눅스 CentOS 스크립트 만들기 (Service Bash Shell Script Start Stop,Restart,Status)

by by 앵과장 2020. 7. 28.
반응형

개발하면서 Instance 구성하거나 실행 스크립트 만들때 매번하는건데 정말 한번 간만에 만들어봤어요

좀더 잘만들었으면 좋을텐데 아직은 이정도라 좀더 분발하겠습니다.

 

경력을 한10년정도 뒤로먹어떠니 이거짜는것도 쉽지않네요 ㅜ.ㅜ

 

서비스 설치 이후 하나의 인스턴스에 여러개 프로세스를 구성하다보면 서비스 start|stop|restart|status 확인도 번거로운 부분이기때문에

이렇게 Bash Shell Script 를 만들어두면 편리하게 사용이 가능합니다.

 

요정도는 Backend 개발하시면 할수 있는거자나요 그죠!!

 

Elasticsearch.sh Bash Script  

시작|중지|상태|리스타트

#!/bin/bash
 
SERVICE="elasticsearch"
 
case "$1" in
    start)
        echo "elasticsearch starting..."
                /Users/renzo/espack/elasticsearch-6.5.4/bin/elasticsearch &
                exit
        ;;
     stop)
        echo "elasticsearch stoping..."
        PIDs=`ps -ef | grep "$SERVICE" | grep -v 'grep' | awk '{print $2}'`
        if pgrep -f "$SERVICE" >/dev/null
        then
                echo "PID : $PIDs"
                kill -9 $PIDs
        else
                echo "$SERVICE stopped"
        fi
        ;;
   status)
        echo "elasticsearch status..."
        if pgrep -f "$SERVICE" >/dev/null
        then
                echo "$SERVICE is running"
        else
                echo "$SERVICE stopped"
        fi
        ;;
  restart)
        echo "elasticsearch restart..."
        PIDs=`ps -ef | grep "$SERVICE" | grep -v 'grep' | awk '{print $2}'`
        if pgrep -f "$SERVICE" >/dev/null
        then
                echo "PID : $PIDs"
                kill -9 $PIDs
                sleep 15
                /Users/renzo/espack/elasticsearch-6.5.4/bin/elasticsearch &
                exit
        else
                echo "$SERVICE stopped"
                /Users/renzo/espack/elasticsearch-6.5.4/bin/elasticsearch &
                exit
        fi
 
        ;;
        *)
        echo "Usage: $0 {start|stop|status|restart}"
        ;;
esac

Kibana.sh Bash Script

SERVICE="kibana"
 
case "$1" in
    start)
        echo "kibana starting..."
                /Users/renzo/espack/kibana-6.5.4-darwin-x86_64/bin/kibana &
                exit
        ;;
     stop)
        echo "kibana stoping..."
        PIDs=`ps -ef | grep "$SERVICE" | grep -v 'grep' | awk '{print $2}'`
        if pgrep -f "$SERVICE" >/dev/null
        then
                echo "PID : $PIDs"
                kill -9 $PIDs
        else
                echo "$SERVICE stopped"
        fi
        ;;
   status)
        echo "kibana status..."
        if pgrep -f "$SERVICE" >/dev/null
        then
                echo "$SERVICE is running"
        else
                echo "$SERVICE stopped"
        fi
        ;;
  restart)
        echo "kibana restart..."
        PIDs=`ps -ef | grep "$SERVICE" | grep -v 'grep' | awk '{print $2}'`
        if pgrep -f "$SERVICE" >/dev/null
        then
                echo "PID : $PIDs"
                kill -9 $PIDs
                sleep 15
                /Users/renzo/espack/kibana-6.5.4-darwin-x86_64/bin/kibana &
                exit
        else
                echo "$SERVICE stopped"
                /Users/renzo/espack/kibana-6.5.4-darwin-x86_64/bin/kibana &
                exit
        fi
 
        ;;
        *)
        echo "Usage: $0 {start|stop|status|restart}"
        ;;
esac

Logstash.sh Bash Script

#!/bin/bash
 
SERVICE="logstash"
case "$1" in
    start)
        echo "Starting logstash: logstash"
            /Users/renzo/espack/logstash-6.5.4/bin/logstash &
            exit
        ;;
    stop)
        echo "Stoping logstash: logstash"
        PIDs=$(ps | grep logstash | grep if | cut -b 1-5)
        if pgrep -f "$SERVICE" >/dev/null
        then
                echo "PID : $PIDs"
                kill -9 $PIDs
        else
                echo "$SERVICE stopped"
        fi
        ;;
    status)
        echo "Status logstash: logstash"
        if pgrep -f "$SERVICE" >/dev/null
        then
                echo "$SERVICE is running"
        else
                echo "$SERVICE stopped"
        fi
        ;;
    restart)
        echo "Restarting logstash: logstash"
        PIDs=$(ps | grep logstash | grep if | cut -b 1-5)
        if pgrep -f "$SERVICE" >/dev/null
        then
                echo "PID : $PIDs"
                kill -9 $PIDs
                sleep 15
                /Users/renzo/espack/logstash-6.5.4/bin/logstash &
                exit
        else
                echo "$SERVICE stopped"
                /Users/renzo/espack/logstash-6.5.4/bin/logstash &
                exit
        fi
 
        ;;
        *)
        echo "Usage: $0 {start|stop|status|restart}"
        ;;
esac