1 Star 0 Fork 0

__xiao__/AModule_bin

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
AModule.sh 5.85 KB
一键复制 编辑 原始数据 按行查看 历史
__xiao__ 提交于 2018-07-10 12:30 . fix service install
#!/bin/bash
### BEGIN INIT INFO
# Provides: AModule
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: AModule
# Description: http://git.oschina.net/xiao0613cn/AModule_bin
### END INIT INFO
# the config of ROOT, user must modify it when start srs from other directory,
# it's ok to use the script by command ./etc/init.d/AModule.sh
ROOT="/usr/local/AModule"
APP_EXEC="./GXAgent deamon"
APP_NAME=GXAgent
#CONFIG="./conf/srs.conf"
DEFAULT_PID_FILE='./AModule.pid'
DEFAULT_LOG_FILE='./AModule.log'
########################################################################
# utility functions
########################################################################
RED="\\033[31m"
GREEN="\\033[32m"
YELLOW="\\033[33m"
BLACK="\\033[0m"
POS="\\033[60G"
ok_msg() {
echo -e "${1}${POS}${BLACK}[${GREEN} OK ${BLACK}]"
}
failed_msg() {
echo -e "${1}${POS}${BLACK}[${RED}FAILED${BLACK}]"
}
# load process info of srs
# @set variable $APP_PID to the process id in srs.pid file.
# @return 0, if process exists; otherwise:
# 1, for pid file not exists.
# 2, for get proecess info by pid failed.
# @set variable $error_msg if error.
# @set variable $pid_file to pid file.
load_process_info() {
# get pid file
#pid_file=`cd ${ROOT} && cat ${CONFIG} |grep ^pid|awk '{print $2}'|awk -F ';' '{print $1}'`
#if [[ -z $pid_file ]]; then pid_file=${DEFAULT_PID_FILE}; fi
pid_file=${DEFAULT_PID_FILE}
# get abs path
#pid_dir=`dirname $pid_file`
#pid_file=`(cd ${ROOT}; cd $pid_dir; pwd)`/`basename $pid_file`
if [[ 0 -ne $# ]]; then
app_name=$1
else
app_name=$APP_NAME;
fi
#APP_PID=`cat $pid_file 2>/dev/null`
APP_PID=`ps -A ax | grep -i "$app_name" | sed -s 's/\([0-9]\{1,\}\).*/\1/g' | head -n 1`
#ret=$?; if [[ 0 -ne $ret ]]; then error_msg="file $pid_file does not exists"; return 1; fi
ps -p ${APP_PID} >/dev/null 2>/dev/null
ret=$?; if [[ 0 -ne $ret ]]; then error_msg="process [$APP_PID] does not exists"; return 2; fi
return 0;
}
start() {
# if exists, exit.
load_process_info
if [[ 0 -eq $? ]]; then failed_msg "$APP_NAME started(pid ${APP_PID}), should not start it again."; return 0; fi
# not exists, start server
ok_msg "Starting $APP_NAME..."
# get log file
#log_file=`cd ${ROOT} && cat ${CONFIG} |grep '^log_file'| awk '{print $2}'| awk -F ';' '{print $1}'`
#if [[ -z $log_file ]]; then log_file=${DEFAULT_LOG_FILE}; fi
log_file=${DEFAULT_LOG_FILE}
# get abs path
#log_dir=`dirname $log_file`
#log_file=`(cd ${ROOT} && cd $log_dir && pwd)`/`basename $log_file`
# TODO: FIXME: set limit by, for instance, "ulimit -HSn 10000"
cd ${ROOT}
${APP_EXEC}
#if [[ -z $log_file ]]; then
# (ulimit -c unlimited && cd ${ROOT}; ${APP} >/dev/null 2>&1)
#else
# (ulimit -c unlimited && cd ${ROOT}; ${APP} >> $log_file 2>&1)
#fi
# check again after start server
for ((i=0; i<3; ++i)); do
# sleep a little while, for srs may start then crash.
sleep 1
load_process_info
ret=$?; if [[ 0 -ne $ret ]]; then
failed_msg "$APP_NAME start failed"
failed_msg "see $log_file"
#return $ret;
fi
done
# check whether started.
load_process_info
ret=$?; if [[ 0 -eq $? ]]; then ok_msg "$APP_NAME started(pid ${APP_PID})"; return 0; fi
failed_msg "$APP_NAME not started"
return $ret
}
stop() {
# not start, exit
load_process_info GXAgent_deamon
if [[ 0 -eq $? ]]; then
kill -s SIGKILL ${APP_PID} 2>/dev/null
fi
# process exists, try to kill to stop normally
for ((i=0;i<100;i++)); do
load_process_info
if [[ 0 -eq $? ]]; then
ok_msg "Stopping $APP_NAME(pid ${APP_PID})..."
kill -s SIGTERM ${APP_PID} 2>/dev/null
ret=$?; if [[ 0 -ne $ret ]]; then failed_msg "send signal SIGTERM failed ret=$ret"; return $ret; fi
sleep 0.3
else
ok_msg "$APP_NAME stopped by SIGTERM"
# delete the pid file when stop success.
rm -f ${pid_file}
break;
fi
done
# process exists, use kill -9 to force to exit
load_process_info
if [[ 0 -eq $? ]]; then
kill -s SIGKILL ${APP_PID} 2>/dev/null
ret=$?; if [[ 0 -ne $ret ]]; then failed_msg "send signal SIGKILL failed ret=$ret"; return $ret; fi
ok_msg "$APP_NAME stopped by SIGKILL"
else
# delete the pid file when stop success.
rm -f ${pid_file}
fi
sleep 0.1
return 0
}
# get the status of srs process
# @return 0 if srs is running; otherwise, 1 for stopped.
status() {
load_process_info
ret=$?; if [[ 0 -eq $ret ]]; then echo "$APP_NAME(pid ${APP_PID}) is running."; return 0; fi
echo "$APP_NAME is stopped, $error_msg"
return 1
}
reload() {
# not start, exit
load_process_info
if [[ 0 -ne $? ]]; then failed_msg "$APP_NAME not start."; return 0; fi
ok_msg "Reload $APP_NAME(pid ${APP_PID})..."
# process exists, reload it
kill -s SIGHUP ${APP_PID} 2>/dev/null
ret=$?; if [[ 0 -ne $ret ]]; then failed_msg "Reload $APP_NAME failed ret=$ret"; return $ret; fi
load_process_info
if [[ 0 -ne $? ]]; then failed_msg "$APP_NAME reload failed."; return $ret; fi
ok_msg "$APP_NAME reloaded"
return 0
}
menu() {
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
status
;;
reload)
reload
;;
*)
echo "Usage: $0 {start|stop|status|restart|reload}"
return 1
;;
esac
}
menu $1
code=$?
exit ${code}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/xiao0613cn/AModule_bin.git
git@gitee.com:xiao0613cn/AModule_bin.git
xiao0613cn
AModule_bin
AModule_bin
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385