Add service install scripts

This commit is contained in:
Pat Hartl 2025-02-06 19:10:55 -06:00
parent adae9f9f6d
commit 2839b11893
3 changed files with 107 additions and 0 deletions

View file

@ -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."

View file

@ -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."

View file

@ -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 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
<plist version=\"1.0\">
<dict>
<key>Label</key>
<string>com.myapp.service</string>
<key>ProgramArguments</key>
<array>
<string>$EXEC_PATH</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>UserName</key>
<string>$SERVICE_USER</string>
<key>GroupName</key>
<string>$SERVICE_GROUP</string>
</dict>
</plist>" | 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."