#!/bin/bash script(){ case "$1" in start) pid=`ps -ef | grep -v "grep" | grep "/usr/local/filebeat/filebeat -e -c /usr/local/filebeat/filebeat.yml -d publish" | awk {'print $2'}` if [[ ${pid} ]]; then echo "filebeat is already running(pid: ${pid})" else /usr/local/filebeat/filebeat -e -c /usr/local/filebeat/filebeat.yml -d publish & fi ;; stop) pid=`ps -ef | grep -v "grep" | grep "/usr/local/filebeat/filebeat -e -c /usr/local/filebeat/filebeat.yml -d publish" | awk {'print $2'}` if [[ ${pid} ]]; then kill -9 ${pid} sleep 1 for((i=1;i<=30;i++)); do pid=`ps -ef | grep -v "grep" | grep "/usr/local/filebeat/filebeat -e -c /usr/local/filebeat/filebeat.yml -d publish" | awk {'print $2'}` if [[ ! ${pid} ]]; then echo "filebeat stop success" break else sleep 1 fi done else echo "filebeat is not running" fi ;; restart) /bin/bash ${0} stop /bin/bash ${0} start ;; *) echo "Please use start or stop or restart as first argument" ;; esac } script $1