mbproxy/install: add Windows service-management batch files

Four simple .bat files — install / remove / start / stop-service — that
manage the 'mbproxy' Windows service against the Mbproxy.exe in their
own folder. publish.ps1 / publish.sh copy them into each win-* publish
flavour, so a published Windows folder is self-managing.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Joseph Doherty
2026-05-16 21:32:33 -04:00
parent 1d48b15ece
commit 1eeee1e292
6 changed files with 155 additions and 1 deletions
+50
View File
@@ -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
+18 -1
View File
@@ -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) }
+18
View File
@@ -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
+39
View File
@@ -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
+15
View File
@@ -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
+15
View File
@@ -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