diff --git a/mbproxy/install/install-service.bat b/mbproxy/install/install-service.bat new file mode 100644 index 0000000..51add54 --- /dev/null +++ b/mbproxy/install/install-service.bat @@ -0,0 +1,50 @@ +@echo off +REM --------------------------------------------------------------------------- +REM Installs Mbproxy as a Windows service named "mbproxy", running the +REM Mbproxy.exe located in THIS folder. The service reads appsettings.json +REM from this same folder. Run this script as Administrator. +REM +REM For the fuller install (Windows Event Log source, ProgramData config / +REM log dirs, ACLs) use install\install.ps1 instead. +REM --------------------------------------------------------------------------- +setlocal +set "SVC=mbproxy" +set "BIN=%~dp0Mbproxy.exe" + +net session >nul 2>&1 +if errorlevel 1 ( + echo ERROR: this script must be run as Administrator. + pause + exit /b 1 +) + +if not exist "%BIN%" ( + echo ERROR: Mbproxy.exe was not found next to this script: + echo %BIN% + pause + exit /b 1 +) + +sc query %SVC% >nul 2>&1 +if not errorlevel 1 ( + echo Service "%SVC%" already exists. Run remove-service.bat first to reinstall. + pause + exit /b 1 +) + +echo Installing service "%SVC%" +echo binary: %BIN% +sc create %SVC% binPath= "%BIN%" start= auto DisplayName= "Mbproxy - Modbus TCP BCD proxy" +if errorlevel 1 ( + echo ERROR: sc create failed. + pause + exit /b 1 +) + +REM Auto-restart 60 s after each of the first two failures; nothing after that. +sc failure %SVC% reset= 86400 actions= restart/60000/restart/60000//0 >nul + +echo. +echo Installed. Edit appsettings.json in this folder if needed, then run +echo start-service.bat to start it. +pause diff --git a/mbproxy/install/publish.ps1 b/mbproxy/install/publish.ps1 index fdd70e3..30bc171 100644 --- a/mbproxy/install/publish.ps1 +++ b/mbproxy/install/publish.ps1 @@ -12,7 +12,8 @@ Each folder also receives a current appsettings.json — the platform-appropriate install template (Windows or Linux, selected by -Rid) — so every publish-out - flavour is a complete, deployable folder. + flavour is a complete, deployable folder. For a win-* RID the four service + batch files (install/remove/start/stop-service.bat) are copied in as well. The runtime is selected with -Rid (default win-x64). The binary is Mbproxy.exe on Windows RIDs and Mbproxy on Linux/macOS RIDs. @@ -93,6 +94,22 @@ foreach ($flavour in 'self-contained','framework-dependent') { Write-Host (" {0,-22} <- {1}" -f $flavour, $configTemplate) } +# ── Ship the Windows service-management batch files (win RIDs only) ───────────── +# install-service / remove-service / start-service / stop-service all act on the +# Mbproxy.exe in their own folder, so the published folder is self-managing. +if ($Rid -like 'win-*') { + $serviceScripts = 'install-service.bat','remove-service.bat','start-service.bat','stop-service.bat' + Write-Host "`n=== Service scripts ===" -ForegroundColor Cyan + foreach ($flavour in 'self-contained','framework-dependent') { + foreach ($script in $serviceScripts) { + $src = Join-Path $repoRoot "install\$script" + if (-not (Test-Path $src)) { throw "Cannot find service script: $src" } + Copy-Item -LiteralPath $src -Destination (Join-Path $OutputDir "$flavour\$script") -Force + } + Write-Host (" {0,-22} <- install\*-service.bat" -f $flavour) + } +} + function Format-Size { param([long]$Bytes) if ($Bytes -ge 1MB) { '{0:N1} MB' -f ($Bytes / 1MB) } diff --git a/mbproxy/install/publish.sh b/mbproxy/install/publish.sh index a9d4c4a..ec7b3bf 100644 --- a/mbproxy/install/publish.sh +++ b/mbproxy/install/publish.sh @@ -94,6 +94,24 @@ for flavour in self-contained framework-dependent; do printf ' %-22s <- %s\n' "$flavour" "$config_template" done +# Ship the Windows service-management batch files (win RIDs only). Each acts on the +# Mbproxy.exe in its own folder, so the published folder is self-managing. +if [[ "$rid" == win-* ]]; then + echo + echo "=== Service scripts ===" + for flavour in self-contained framework-dependent; do + for script in install-service.bat remove-service.bat start-service.bat stop-service.bat; do + src="$repo_root/install/$script" + if [[ ! -f "$src" ]]; then + echo "Cannot find service script: $src" >&2 + exit 1 + fi + cp -f "$src" "$output_dir/$flavour/$script" + done + printf ' %-22s <- install/*-service.bat\n' "$flavour" + done +fi + echo echo "=== Result ($rid) ===" for flavour in self-contained framework-dependent; do diff --git a/mbproxy/install/remove-service.bat b/mbproxy/install/remove-service.bat new file mode 100644 index 0000000..45cd95e --- /dev/null +++ b/mbproxy/install/remove-service.bat @@ -0,0 +1,39 @@ +@echo off +REM --------------------------------------------------------------------------- +REM Stops and removes the "mbproxy" Windows service. Does not delete any files +REM in this folder. Run this script as Administrator. +REM --------------------------------------------------------------------------- +setlocal +set "SVC=mbproxy" + +net session >nul 2>&1 +if errorlevel 1 ( + echo ERROR: this script must be run as Administrator. + pause + exit /b 1 +) + +sc query %SVC% >nul 2>&1 +if errorlevel 1 ( + echo Service "%SVC%" is not installed — nothing to do. + pause + exit /b 0 +) + +echo Stopping service "%SVC%" (if running)... +sc stop %SVC% >nul 2>&1 + +REM Give the service a few seconds to drain and stop before deleting it. +timeout /t 5 /nobreak >nul + +echo Removing service "%SVC%"... +sc delete %SVC% +if errorlevel 1 ( + echo ERROR: sc delete failed. If the service still shows up, close + echo services.msc and try again. + pause + exit /b 1 +) + +echo Removed. +pause diff --git a/mbproxy/install/start-service.bat b/mbproxy/install/start-service.bat new file mode 100644 index 0000000..f0fe450 --- /dev/null +++ b/mbproxy/install/start-service.bat @@ -0,0 +1,15 @@ +@echo off +REM --------------------------------------------------------------------------- +REM Starts the "mbproxy" Windows service. Run this script as Administrator. +REM --------------------------------------------------------------------------- +setlocal + +net session >nul 2>&1 +if errorlevel 1 ( + echo ERROR: this script must be run as Administrator. + pause + exit /b 1 +) + +net start mbproxy +pause diff --git a/mbproxy/install/stop-service.bat b/mbproxy/install/stop-service.bat new file mode 100644 index 0000000..bb02c94 --- /dev/null +++ b/mbproxy/install/stop-service.bat @@ -0,0 +1,15 @@ +@echo off +REM --------------------------------------------------------------------------- +REM Stops the "mbproxy" Windows service. Run this script as Administrator. +REM --------------------------------------------------------------------------- +setlocal + +net session >nul 2>&1 +if errorlevel 1 ( + echo ERROR: this script must be run as Administrator. + pause + exit /b 1 +) + +net stop mbproxy +pause