feat: add service start/stop scripts for Windows and Linux

This commit is contained in:
Your Name
2026-04-28 21:19:36 +08:00
parent e94c730def
commit 2459122c0e
7 changed files with 380 additions and 0 deletions
+4
View File
@@ -18,3 +18,7 @@ Desktop.ini
# Build output # Build output
ftp_root/ ftp_root/
# Runtime
*.pid
ftp-server.log
+97
View File
@@ -0,0 +1,97 @@
@echo off
chcp 65001 >nul 2>&1
title FTP Server
:: ============================
:: FTP Server 启动/停止脚本 (Windows)
:: ============================
set "APP_NAME=ftp-server"
set "APP_EXE=%~dp0ftp-server.exe"
set "CONFIG=%~dp0config.json"
set "LOG_FILE=%~dp0ftp-server.log"
set "PID_FILE=%~dp0ftp-server.pid"
if "%1"=="" goto :usage
if "%1"=="start" goto :start
if "%1"=="stop" goto :stop
if "%1"=="restart" goto :restart
if "%1"=="status" goto :status
goto :usage
:: ---------- 启动 ----------
:start
if not exist "%APP_EXE%" (
echo [错误] 未找到 %APP_EXE%
echo 请先编译: go build -o ftp-server.exe ./cmd/
exit /b 1
)
:: 检查是否已在运行
tasklist /FI "IMAGENAME eq %APP_NAME%.exe" 2>nul | find /i "%APP_NAME%.exe" >nul
if %errorlevel%==0 (
echo [提示] %APP_NAME% 已经在运行中
goto :status
)
echo [启动] 正在启动 %APP_NAME% ...
start "%APP_NAME%" /MIN "%APP_EXE%" -config "%CONFIG%"
:: 等待启动
timeout /t 2 /nobreak >nul
:: 检查是否启动成功
tasklist /FI "IMAGENAME eq %APP_NAME%.exe" 2>nul | find /i "%APP_NAME%.exe" >nul
if %errorlevel%==0 (
echo [成功] %APP_NAME% 已启动
echo Web 管理面板: http://localhost:8080
echo FTP 端口: 2121
) else (
echo [失败] %APP_NAME% 启动失败,请检查配置
)
goto :eof
:: ---------- 停止 ----------
:stop
echo [停止] 正在停止 %APP_NAME% ...
tasklist /FI "IMAGENAME eq %APP_NAME%.exe" 2>nul | find /i "%APP_NAME%.exe" >nul
if %errorlevel%==0 (
taskkill /F /IM "%APP_NAME%.exe" >nul 2>&1
timeout /t 1 /nobreak >nul
echo [成功] %APP_NAME% 已停止
) else (
echo [提示] %APP_NAME% 未在运行
)
goto :eof
:: ---------- 重启 ----------
:restart
call %0 stop
timeout /t 2 /nobreak >nul
call %0 start
goto :eof
:: ---------- 状态 ----------
:status
tasklist /FI "IMAGENAME eq %APP_NAME%.exe" 2>nul | find /i "%APP_NAME%.exe" >nul
if %errorlevel%==0 (
echo [状态] %APP_NAME% 正在运行
for /f "tokens=2" %%a in ('tasklist /FI "IMAGENAME eq %APP_NAME%.exe" /NH 2^>nul') do (
echo PID: %%a
)
) else (
echo [状态] %APP_NAME% 未运行
)
goto :eof
:: ---------- 帮助 ----------
:usage
echo.
echo 用法: %~nx0 {start^|stop^|restart^|status}
echo.
echo 命令:
echo start 启动 FTP Server
echo stop 停止 FTP Server
echo restart 重启 FTP Server
echo status 查看运行状态
echo.
+130
View File
@@ -0,0 +1,130 @@
#!/bin/bash
# ============================
# FTP Server 启动/停止脚本 (Linux/macOS)
# ============================
APP_NAME="ftp-server"
APP_DIR="$(cd "$(dirname "$0")" && pwd)"
APP_BIN="$APP_DIR/ftp-server"
CONFIG="$APP_DIR/config.json"
LOG_FILE="$APP_DIR/ftp-server.log"
PID_FILE="$APP_DIR/ftp-server.pid"
# 颜色
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m'
info() { echo -e "${GREEN}[信息]${NC} $1"; }
warn() { echo -e "${YELLOW}[提示]${NC} $1"; }
error() { echo -e "${RED}[错误]${NC} $1"; }
# 获取 PID
get_pid() {
if [ -f "$PID_FILE" ]; then
local pid=$(cat "$PID_FILE" 2>/dev/null)
if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then
echo "$pid"
return 0
fi
# PID 文件过期,清理
rm -f "$PID_FILE"
fi
# 尝试通过进程名查找
pgrep -f "$APP_BIN" 2>/dev/null | head -1
}
# ---------- 启动 ----------
do_start() {
if [ ! -f "$APP_BIN" ]; then
error "未找到 $APP_BIN"
echo "请先编译: go build -o ftp-server ./cmd/"
exit 1
fi
local pid=$(get_pid)
if [ -n "$pid" ]; then
warn "$APP_NAME 已经在运行中 (PID: $pid)"
exit 0
fi
info "正在启动 $APP_NAME ..."
nohup "$APP_BIN" -config "$CONFIG" >> "$LOG_FILE" 2>&1 &
local new_pid=$!
echo "$new_pid" > "$PID_FILE"
sleep 2
if kill -0 "$new_pid" 2>/dev/null; then
info "$APP_NAME 已启动 (PID: $new_pid)"
echo " Web 管理面板: http://localhost:8080"
echo " FTP 端口: 2121"
echo " 日志文件: $LOG_FILE"
else
error "$APP_NAME 启动失败,请查看日志: $LOG_FILE"
rm -f "$PID_FILE"
exit 1
fi
}
# ---------- 停止 ----------
do_stop() {
local pid=$(get_pid)
if [ -z "$pid" ]; then
warn "$APP_NAME 未在运行"
exit 0
fi
info "正在停止 $APP_NAME (PID: $pid) ..."
kill "$pid" 2>/dev/null
# 等待进程退出
local count=0
while kill -0 "$pid" 2>/dev/null && [ $count -lt 10 ]; do
sleep 1
count=$((count + 1))
done
# 如果还没退出,强制杀掉
if kill -0 "$pid" 2>/dev/null; then
warn "正常停止超时,强制终止..."
kill -9 "$pid" 2>/dev/null
fi
rm -f "$PID_FILE"
info "$APP_NAME 已停止"
}
# ---------- 状态 ----------
do_status() {
local pid=$(get_pid)
if [ -n "$pid" ]; then
info "$APP_NAME 正在运行 (PID: $pid)"
else
warn "$APP_NAME 未运行"
fi
}
# ---------- 帮助 ----------
usage() {
echo ""
echo " 用法: $0 {start|stop|restart|status}"
echo ""
echo " 命令:"
echo " start 启动 FTP Server"
echo " stop 停止 FTP Server"
echo " restart 重启 FTP Server"
echo " status 查看运行状态"
echo ""
}
# ---------- 主入口 ----------
case "$1" in
start) do_start ;;
stop) do_stop ;;
restart) do_stop; sleep 2; do_start ;;
status) do_status ;;
*) usage; exit 1 ;;
esac
+20
View File
@@ -0,0 +1,20 @@
@echo off
chcp 65001 >nul 2>&1
title FTP Server - 停止
cd /d "%~dp0"
echo.
echo [停止] 正在停止 FTP Server ...
tasklist /FI "IMAGENAME eq ftp-server.exe" 2>nul | find /i "ftp-server.exe" >nul
if %errorlevel%==0 (
taskkill /F /IM "ftp-server.exe" >nul 2>&1
timeout /t 1 /nobreak >nul
echo [成功] FTP Server 已停止
) else (
echo [提示] FTP Server 未在运行
)
echo.
pause
+57
View File
@@ -0,0 +1,57 @@
@echo off
chcp 65001 >nul 2>&1
title FTP Server - 启动
:: 切换到脚本所在目录
cd /d "%~dp0"
if not exist "ftp-server.exe" (
echo.
echo [错误] 未找到 ftp-server.exe
echo 请先编译: go build -o ftp-server.exe ./cmd/
echo.
pause
exit /b 1
)
:: 检查是否已在运行
tasklist /FI "IMAGENAME eq ftp-server.exe" 2>nul | find /i "ftp-server.exe" >nul
if %errorlevel%==0 (
echo.
echo [提示] FTP Server 已经在运行中
echo 如需重启,请先运行 "停止FTP服务.bat"
echo.
pause
exit /b 0
)
echo.
echo [启动] 正在启动 FTP Server ...
echo.
:: 后台启动
start "" /MIN ftp-server.exe -config config.json
:: 等待启动
timeout /t 3 /nobreak >nul
:: 检查是否启动成功
tasklist /FI "IMAGENAME eq ftp-server.exe" 2>nul | find /i "ftp-server.exe" >nul
if %errorlevel%==0 (
echo [成功] FTP Server 已启动
echo.
echo Web 管理面板: http://localhost:8080
echo FTP 端口: 2121
echo 账号: admin / admin123
) else (
echo [失败] FTP Server 启动失败
echo.
echo 可能原因:
echo 1. 端口 2121 或 8080 已被其他程序占用
echo 2. config.json 配置文件有误
echo.
echo 请尝试在命令行运行 ftp-server.exe 查看详细错误信息
)
echo.
pause
+27
View File
@@ -0,0 +1,27 @@
@echo off
chcp 65001 >nul 2>&1
title FTP Server - 查看状态
cd /d "%~dp0"
echo.
echo ================================
echo FTP Server 运行状态
echo ================================
echo.
tasklist /FI "IMAGENAME eq ftp-server.exe" 2>nul | find /i "ftp-server.exe" >nul
if %errorlevel%==0 (
echo 状态: 运行中
for /f "tokens=2" %%a in ('tasklist /FI "IMAGENAME eq ftp-server.exe" /NH 2^>nul') do (
echo PID: %%a
)
) else (
echo 状态: 未运行
)
echo.
echo Web 管理面板: http://localhost:8080
echo FTP 端口: 2121
echo.
pause
+45
View File
@@ -0,0 +1,45 @@
@echo off
chcp 65001 >nul 2>&1
title FTP Server - 重启
cd /d "%~dp0"
echo.
echo [重启] 正在重启 FTP Server ...
:: 先停止
tasklist /FI "IMAGENAME eq ftp-server.exe" 2>nul | find /i "ftp-server.exe" >nul
if %errorlevel%==0 (
taskkill /F /IM "ftp-server.exe" >nul 2>&1
timeout /t 2 /nobreak >nul
echo [停止] FTP Server 已停止
) else (
echo [提示] FTP Server 未在运行
)
:: 再启动
if not exist "ftp-server.exe" (
echo [错误] 未找到 ftp-server.exe
echo.
pause
exit /b 1
)
echo [启动] 正在启动 FTP Server ...
start "" /MIN ftp-server.exe -config config.json
timeout /t 3 /nobreak >nul
tasklist /FI "IMAGENAME eq ftp-server.exe" 2>nul | find /i "ftp-server.exe" >nul
if %errorlevel%==0 (
echo [成功] FTP Server 已重启
echo.
echo Web 管理面板: http://localhost:8080
echo FTP 端口: 2121
echo 账号: admin / admin123
) else (
echo [失败] FTP Server 启动失败
)
echo.
pause