#!/usr/bin/env bash cd $(dirname $0) DEPLOY_PATH=$(pwd) echo "DEPLOY_PATH : "${DEPLOY_PATH} BIN='logproxy' GPID=0 function is_running() { for pid in $(ps ux | grep ${BIN} | grep -v 'bash ' | grep -v grep | awk '{print $2}'); do _path=$(readlink -f /proc/${pid}/cwd) if [[ ${?} -eq 0 ]] && [[ ${_path} == ${DEPLOY_PATH} ]]; then GPID=${pid} echo "is_running : (${GPID})${DEPLOY_PATH} ${BIN} is running!" return 1 fi done return 0 } function kill_proc_9() { force=$1 if [[ ! -d ${DEPLOY_PATH} ]]; then echo "kill_proc : ${DEPLOY_PATH} invalid!" return 0 fi retry=0 is_running status=$? while [[ ${status} -eq 1 ]]; do if [ ! -z ${force} ]; then kill -9 ${GPID} echo "kill_proc force : (${GPID})${DEPLOY_PATH} succ!" else kill ${GPID} echo "kill_proc : (${GPID})${DEPLOY_PATH} succ!" fi sleep 1 retry=$(expr ${retry} + 1) if [ ${retry} -gt 15 ]; then force=1 fi is_running status=$? done return 0 } start() { stop if [ ! -d "./run" ]; then mkdir ./run fi log_path="./log" if [ ! -d ${log_path} ]; then mkdir ${log_path} fi log_path=$(readlink -f ${log_path}) chmod u+x ./bin/${BIN} && ./bin/${BIN} -f ./conf/conf.json &>${log_path}/out.log & if [ $? -ne 0 ]; then exit -1 fi is_running } stop() { kill_proc_9 } case C"$1" in Cstop) stop echo "${BIN} stopped!" ;; Cstart) start echo "${BIN} started!" ;; Cdebug) debug echo "${BIN} started!" ;; Cstatus) is_running status=$? echo "status : ${status}" exit ${status} ;; C*) echo "Usage: $0 {start|stop|status}" ;; esac