自动更新管控端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

163 lines
3.0 KiB

#!/bin/bash
# scagent 服务管理脚本
# 用于启动、停止、重启和检查 scagent 服务状态
# 服务名称
APP_NAME="scagent"
# 可执行文件路径
APP_PATH="./scagent"
# PID文件路径
PID_FILE="./${APP_NAME}.pid"
# 日志文件路径
LOG_FILE="./${APP_NAME}.log"
# 检查程序是否存在
check_app_exists() {
if [ ! -f "$APP_PATH" ]; then
echo "错误: 程序 $APP_PATH 不存在,请先编译!"
exit 1
fi
}
# 检查程序是否在运行
is_running() {
if [ -f "$PID_FILE" ]; then
PID=$(cat $PID_FILE)
if ps -p $PID > /dev/null 2>&1; then
return 0 # 运行中
else
# PID文件存在但进程不存在,删除PID文件
rm -f $PID_FILE
fi
fi
return 1 # 未运行
}
# 启动服务
start() {
check_app_exists
if is_running; then
echo "$APP_NAME 服务已经在运行中!"
exit 0
fi
echo "正在启动 $APP_NAME 服务..."
# 以守护进程方式启动应用
nohup $APP_PATH -d > $LOG_FILE 2>&1 &
# 记录PID
echo $! > $PID_FILE
sleep 2
if is_running; then
echo "$APP_NAME 服务启动成功!"
echo "PID: $(cat $PID_FILE)"
else
echo "$APP_NAME 服务启动失败!"
exit 1
fi
}
# 停止服务
stop() {
if ! is_running; then
echo "$APP_NAME 服务未运行!"
exit 0
fi
echo "正在停止 $APP_NAME 服务..."
PID=$(cat $PID_FILE)
kill -15 $PID # 发送 SIGTERM 信号
# 等待进程终止
wait_time=0
max_wait=10
while is_running && [ $wait_time -lt $max_wait ]; do
sleep 1
wait_time=$((wait_time + 1))
echo -n "."
done
echo
if is_running; then
echo "强制终止 $APP_NAME 服务..."
kill -9 $PID
rm -f $PID_FILE
else
rm -f $PID_FILE
echo "$APP_NAME 服务已停止!"
fi
}
# 重启服务
restart() {
echo "正在重启 $APP_NAME 服务..."
stop
sleep 2
start
}
# 查看状态
status() {
if is_running; then
echo "$APP_NAME 服务正在运行中"
echo "PID: $(cat $PID_FILE)"
else
echo "$APP_NAME 服务未运行"
fi
}
# 查看日志
tail_log() {
if [ -f "$LOG_FILE" ]; then
tail -f $LOG_FILE
else
echo "日志文件不存在: $LOG_FILE"
fi
}
# 显示帮助信息
usage() {
echo "使用方法: $0 {start|stop|restart|status|log}"
echo " start 启动服务"
echo " stop 停止服务"
echo " restart 重启服务"
echo " status 查看服务状态"
echo " log 查看日志输出"
}
# 检查参数
if [ $# -eq 0 ]; then
usage
exit 1
fi
# 根据参数执行相应的命令
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
status)
status
;;
log)
tail_log
;;
*)
usage
exit 1
;;
esac
exit 0