-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstop.ps1
56 lines (48 loc) · 1.64 KB
/
stop.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# Path to config file
$configFilePath = ".\config.json"
# Check if config.json exists
if (!(Test-Path $configFilePath)) {
Write-Host "Error: Configuration file not found at $configFilePath"
exit 1
}
# Read and parse the JSON file
$config = Get-Content -Raw -Path $configFilePath | ConvertFrom-Json
# Variables
$processName = $config.processName
# Output
Write-Host ""
Write-Host "## STOPPING ENSHROUDED SERVER"
Write-Host "------------------------------------"
Write-Host "Process name: $processName"
Write-Host ""
# Start the process in the background
try {
Write-Host "Searching for process '$processName'"
$process = Get-Process | Where-Object { $_.ProcessName -like "*$processName*" }
# Check if the process is still running
if ($process) {
Write-Host "The process '$($process.name):$($process.id)' is running"
# Step 1: Graceful shutdown
Write-Host "Initiating graceful shutdown of the Enshrouded server..."
try {
$process | Stop-Process
} catch {
Write-Host "Failed to send graceful shutdown command: $_"
}
# Step 2: Verify the server has stopped
Write-Output "Verifying if the server process has stopped..."
Start-Sleep -Seconds 5
if (Get-Process -ErrorAction SilentlyContinue | Where-Object { $_.ProcessName -like "*$processName*" }) {
Write-Host "Server process is still running. Stopping the process forcefully..."
$process | Stop-Process -Force
Start-Sleep -Seconds 5
} else {
Write-Host "Server process has stopped successfully!"
}
} else {
Write-Host "The process is not running"
}
} catch {
Write-Host "Failed to start the server: $_"
exit 1
}