From 2839b1189333f21701bf1dd87e911696aea75324 Mon Sep 17 00:00:00 2001 From: Pat Hartl Date: Thu, 6 Feb 2025 19:10:55 -0600 Subject: [PATCH] Add service install scripts --- LANCommander.Server/InstallService-Linux.sh | 39 +++++++++++++++++ .../InstallService-Windows.ps1 | 25 +++++++++++ LANCommander.Server/InstallService-macOS.sh | 43 +++++++++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 LANCommander.Server/InstallService-Linux.sh create mode 100644 LANCommander.Server/InstallService-Windows.ps1 create mode 100644 LANCommander.Server/InstallService-macOS.sh diff --git a/LANCommander.Server/InstallService-Linux.sh b/LANCommander.Server/InstallService-Linux.sh new file mode 100644 index 00000000..638ba8bb --- /dev/null +++ b/LANCommander.Server/InstallService-Linux.sh @@ -0,0 +1,39 @@ +#!/bin/bash +SERVICE_NAME=lancommander.server +SERVICE_FILE=/etc/systemd/system/$SERVICE_NAME.service +EXEC_PATH=$(pwd) + +SERVICE_USER=$USER +read -e -i "$SERVICE_USER" -p "Run as user: " input +SERVICE_USER="${input:-$SERVICE_USER}" + +SERVICE_GROUP=$SERVICE_USER +read -e -i "$SERVICE_GROUP" -p "Run in group: " input +SERVICE_GROUP="${input:-$SERVICE_GROUP}" + +if [ -f "$SERVICE_FILE" ]; then + echo "Service already exists. Stopping and removing it." + sudo systemctl stop $SERVICE_NAME + sudo systemctl disable $SERVICE_NAME + sudo rm -f $SERVICE_FILE +fi + +echo "[Unit] +Description=LANCommander Server +After=network.target + +[Service] +ExecStart=$EXEC_PATH +Restart=always +User=$SERVICE_USER +Group=$SERVICE_GROUP +WorkingDirectory=$(dirname $EXEC_PATH) + +[Install] +WantedBy=multi-user.target" | sudo tee $SERVICE_FILE > /dev/null + +sudo systemctl daemon-reload +sudo systemctl enable $SERVICE_NAME +sudo systemctl start $SERVICE_NAME + +echo "Service '$SERVICE_NAME' installed and started successfully." \ No newline at end of file diff --git a/LANCommander.Server/InstallService-Windows.ps1 b/LANCommander.Server/InstallService-Windows.ps1 new file mode 100644 index 00000000..9cf3940f --- /dev/null +++ b/LANCommander.Server/InstallService-Windows.ps1 @@ -0,0 +1,25 @@ +# Elevate if not running as admin +if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { + Start-Process -FilePath "powershell" -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs + exit +} + +# Define service details +$serviceName = "LANCommander Server" +$serviceDisplayName = "Service for running the LANCommander Server" +$exePath = ".\LANCommander.Server.exe" + +# Remove existing service +if (Get-Service -Name $serviceName -ErrorAction SilentlyContinue) { + Stop-Service -Name $serviceName -Force -ErrorAction SilentlyContinue + Remove-Service -Name $serviceName + Start-Sleep -Seconds 2 +} + +# Install the service +New-Service -Name $serviceName -BinaryPathName "`"$exePath`"" -DisplayName $serviceDisplayName -StartupType Automatic + +# Start the service +Start-Service -Name $serviceName + +Write-Host "Service '$serviceName' installed and started successfully." \ No newline at end of file diff --git a/LANCommander.Server/InstallService-macOS.sh b/LANCommander.Server/InstallService-macOS.sh new file mode 100644 index 00000000..194ec939 --- /dev/null +++ b/LANCommander.Server/InstallService-macOS.sh @@ -0,0 +1,43 @@ +#!/bin/bash +PLIST_FILE=/Library/LaunchDaemons/app.lancommander.server.service.plist +EXEC_PATH=$(pwd) + +SERVICE_USER=$USER +read -e -i "$SERVICE_USER" -p "Run as user: " input +SERVICE_USER="${input:-$SERVICE_USER}" + +SERVICE_GROUP=$SERVICE_USER +read -e -i "$SERVICE_GROUP" -p "Run in group: " input +SERVICE_GROUP="${input:-$SERVICE_GROUP}" + +if [ -f "$PLIST_FILE" ]; then + echo "Service already exists. Stopping and removing it." + sudo launchctl unload $PLIST_FILE + sudo rm -f $PLIST_FILE +fi + +echo " + + + + Label + com.myapp.service + ProgramArguments + + $EXEC_PATH + + RunAtLoad + + KeepAlive + + UserName + $SERVICE_USER + GroupName + $SERVICE_GROUP + +" | sudo tee $PLIST_FILE > /dev/null + +sudo launchctl load $PLIST_FILE +sudo launchctl start app.lancommander.server.service + +echo "Service 'app.lancommander.server.service' installed and started successfully." \ No newline at end of file