| New file |
| | |
| | | <# |
| | | Register / remove / inspect a Windows Scheduled Task that starts the TrafficAudit |
| | | backend at machine boot, with no user session required. |
| | | |
| | | Run from an ELEVATED PowerShell in the backend folder (jar + application.yml + |
| | | this script + start-backend-service.cmd): |
| | | |
| | | powershell -NoProfile -ExecutionPolicy Bypass -File install-autostart.ps1 |
| | | powershell -NoProfile -ExecutionPolicy Bypass -File install-autostart.ps1 -DelaySeconds 90 |
| | | powershell -NoProfile -ExecutionPolicy Bypass -File install-autostart.ps1 -StartNow |
| | | powershell -NoProfile -ExecutionPolicy Bypass -File install-autostart.ps1 -Status |
| | | powershell -NoProfile -ExecutionPolicy Bypass -File install-autostart.ps1 -Remove |
| | | |
| | | What it does: |
| | | - trigger : AtStartup, delayed by -DelaySeconds (default 60s, so MySQL is up first) |
| | | - principal: SYSTEM, highest privileges, "run whether user is logged on or not" |
| | | - settings : no execution time limit, start when available, restart 3x / 1 min on failure |
| | | - action : cmd.exe /c start-backend-service.cmd (logs to app.log next to the jar) |
| | | |
| | | Notes: |
| | | - Re-running without -Remove is idempotent (-Force overwrites the task). |
| | | - Start/stop manually with: Start-ScheduledTask / Stop-ScheduledTask -TaskName <name> |
| | | - Keep this file ASCII-only: Windows PowerShell 5.1 reads a BOM-less .ps1 as ANSI. |
| | | #> |
| | | param( |
| | | [string]$TaskName = 'TrafficAuditBackend', |
| | | [int]$DelaySeconds = 60, |
| | | [switch]$Remove, |
| | | [switch]$Status, |
| | | [switch]$StartNow |
| | | ) |
| | | |
| | | $ErrorActionPreference = 'Stop' |
| | | $root = Split-Path -Parent $MyInvocation.MyCommand.Path |
| | | $launcher = Join-Path $root 'start-backend-service.cmd' |
| | | $logFile = Join-Path $root 'app.log' |
| | | |
| | | function Assert-Admin { |
| | | $identity = [Security.Principal.WindowsIdentity]::GetCurrent() |
| | | $principal = New-Object Security.Principal.WindowsPrincipal($identity) |
| | | if (-not $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { |
| | | throw 'This script must run from an elevated (Run as Administrator) PowerShell.' |
| | | } |
| | | } |
| | | |
| | | if ($Status) { |
| | | $task = Get-ScheduledTask -TaskName $TaskName -ErrorAction SilentlyContinue |
| | | if (-not $task) { Write-Host "Scheduled task '$TaskName' is not registered."; exit 1 } |
| | | $task | Select-Object TaskName, TaskPath, State | Format-List |
| | | Get-ScheduledTaskInfo -TaskName $TaskName | |
| | | Select-Object LastRunTime, LastTaskResult, NextRunTime, NumberOfMissedRuns | Format-List |
| | | exit 0 |
| | | } |
| | | |
| | | if ($Remove) { |
| | | Assert-Admin |
| | | if (Get-ScheduledTask -TaskName $TaskName -ErrorAction SilentlyContinue) { |
| | | Unregister-ScheduledTask -TaskName $TaskName -Confirm:$false |
| | | Write-Host "Removed scheduled task '$TaskName'." |
| | | } else { |
| | | Write-Host "Scheduled task '$TaskName' was not registered; nothing to remove." |
| | | } |
| | | exit 0 |
| | | } |
| | | |
| | | Assert-Admin |
| | | if (-not (Test-Path $launcher)) { throw "Missing launcher: $launcher" } |
| | | if (-not (Test-Path (Join-Path $root 'traffic-audit-server.jar'))) { |
| | | Write-Host "WARN: jar not found in $root - the task will be registered, but startup will fail until the jar is there." |
| | | } |
| | | |
| | | $action = New-ScheduledTaskAction -Execute 'cmd.exe' -Argument ('/c "' + $launcher + '"') -WorkingDirectory $root |
| | | |
| | | $trigger = New-ScheduledTaskTrigger -AtStartup |
| | | if ($DelaySeconds -gt 0) { $trigger.Delay = 'PT' + $DelaySeconds + 'S' } |
| | | |
| | | $principal = New-ScheduledTaskPrincipal -UserId 'SYSTEM' -LogonType ServiceAccount -RunLevel Highest |
| | | |
| | | $settings = New-ScheduledTaskSettingsSet ` |
| | | -AllowStartIfOnBatteries ` |
| | | -DontStopIfGoingOnBatteries ` |
| | | -StartWhenAvailable ` |
| | | -MultipleInstances IgnoreNew ` |
| | | -RestartCount 3 ` |
| | | -RestartInterval (New-TimeSpan -Minutes 1) ` |
| | | -ExecutionTimeLimit ([TimeSpan]::Zero) |
| | | |
| | | Register-ScheduledTask ` |
| | | -TaskName $TaskName ` |
| | | -Action $action ` |
| | | -Trigger $trigger ` |
| | | -Principal $principal ` |
| | | -Settings $settings ` |
| | | -Description 'TrafficAudit backend auto-start at machine boot (script-managed).' ` |
| | | -Force | Out-Null |
| | | |
| | | Write-Host "Registered scheduled task '$TaskName'." |
| | | Write-Host " Trigger : At startup, +${DelaySeconds}s" |
| | | Write-Host " Principal: SYSTEM (highest privileges, no logon required)" |
| | | Write-Host " Launcher : $launcher" |
| | | Write-Host " Log : $logFile" |
| | | Write-Host '' |
| | | Write-Host "Service address after boot: http://<server-ip>:8090" |
| | | Write-Host "Manage it with: -Status | -StartNow | -Remove" |
| | | |
| | | if ($StartNow) { |
| | | Start-ScheduledTask -TaskName $TaskName |
| | | Write-Host "Started task '$TaskName' now." |
| | | } |