#!/bin/bash -e if [[ $# != 3 ]]; then echo "Usage: app.sh \${command} \${server} \${id}" echo " command: start/stop/restart" echo " server: the name of the server" echo " id: the number greater than zero of the server id" exit 0 fi CMD=$1 SVR=$2 SID=$3 cd ${SVR}_${SID} start_server() { pid=`ps -ef | grep ezs_${SVR} | grep ${SVR}_${SID}.json | awk '{print $2}'` if [ ! -z ${pid} ]; then echo "The server '${SVR}' with id:${SID} is running." exit 0 fi if [ ! -x ./bin/ezs_${SVR} ]; then chmod +x ./bin/ezs_${SVR} fi nohup ./bin/ezs_${SVR} -conf ./conf/${SVR}_${SID}.json > ./ezs_${SVR}_${SID}.out 2>&1 & sleep 2 pid=`ps -ef | grep ezs_${SVR} | grep ${SVR}_${SID}.json | awk '{print $2}'` if [ -z ${pid} ]; then echo "The server '${SVR}' with id:${SID} start failed." tail -n30 ./bin/ezs_${SVR}_${SID}.out exit 1 fi echo "The server '${SVR}' with id:${SID} was started, pid: ${pid}" } stop_server() { pid=`ps -ef | grep ezs_${SVR} | grep ${SVR}_${SID}.json | awk '{print $2}'` if [ ! -z ${pid} ]; then kill ${pid} sleep 1 echo "The server '${SVR}' with id:${SID} was stopped, pid: ${pid}" fi } case ${CMD} in start) start_server exit 0 ;; stop) stop_server exit 0 ;; restart) stop_server start_server exit 0 ;; *) echo "Unknown command: ${CMD}" exit 1 ;; esac