ecs/app.sh

84 lines
1.4 KiB
Bash
Raw Normal View History

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