ecs/app.sh
2025-07-18 10:45:29 +08:00

84 lines
1.4 KiB
Bash

#!/bin/bash -e
usage() {
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"
}
if [[ $# != 3 ]]; then
usage
exit 0
fi
CMD=$1
SVR=$2
SID=$3
start_server() {
cd ${SVR}
pid=`ps -ef | grep ecs_${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/ecs_${SVR} ]; then
chmod +x ./bin/ecs_${SVR}
fi
nohup ./bin/ecs_${SVR} -conf ./conf/${SVR}_${SID}.json > ./ecs_${SVR}_${SID}.out 2>&1 &
sleep 2
pid=`ps -ef | grep ecs_${SVR} | grep ${SVR}_${SID}.json | awk '{print $2}'`
if [ -z ${pid} ]; then
echo "The server '${SVR}' with id:${SID} start failed."
tail -n30 ./bin/ecs_${SVR}_${SID}.out
exit 1
fi
echo "The server '${SVR}' with id:${SID} was started, pid: ${pid}"
cd ..
}
stop_server() {
pid=`ps -ef | grep ecs_${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
;;
help)
usage
exit 0
;;
*)
echo "Unknown command: ${CMD}"
exit 1
;;
esac