-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart-server.sh
More file actions
150 lines (129 loc) · 5.14 KB
/
start-server.sh
File metadata and controls
150 lines (129 loc) · 5.14 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/bin/bash
cat << 'EOF'
_/_/_/ _/_/_/ _/ _/ _/ _/
_/ _/ _/ _/ _/_/ _/_/
_/_/ _/ _/ _/ _/ _/ _/
_/ _/ _/ _/ _/ _/
_/_/_/ _/_/_/ _/_/ _/ _/
DEDICATED SERVER
https://github.com/EvilOlaf/scum
EOF
# workaround to avoid breaking existing installations
# if PORT is still used in docker-compose.yml, move its value to GAMEPORT and warn user.
if [[ -n "${PORT}" ]]; then
echo 'ATTENTION!'
echo '"PORT" environment variable is deprecated'
echo 'and will be removed at some point.'
echo 'Replace with "GAMEPORT" in your docker-compose.yml.'
echo 'ATTENTION!'
GAMEPORT="${GAMEPORT:-$PORT}"
fi
# skip download of SteamCMD if present already
if [ ! -f /opt/steamcmd/steamcmd.sh ]; then
echo "SteamCMD not found. Installing..."
mkdir -p /opt/steamcmd && \
cd /opt/steamcmd && \
wget --timeout=30 --tries=3 https://steamcdn-a.akamaihd.net/client/installer/steamcmd_linux.tar.gz && \
tar -xvzf steamcmd_linux.tar.gz && \
rm /opt/steamcmd/steamcmd_linux.tar.gz && \
echo "SteamCMD successfully installed"
else
echo "SteamCMD found, skipping installation..."
fi
# update SteamCMD and SCUM dedicated server
echo "Update SteamCMD and SCUM dedicated server..."
/opt/steamcmd/steamcmd.sh +@sSteamCmdForcePlatformType windows \
+force_install_dir /opt/scumserver \
+login anonymous \
+app_update 3792580 validate \
+quit
echo "Starting SCUM dedicated server..."
# Handle shutdown signals gracefully, suppress false positive shellcheck warning
# shellcheck disable=SC2329
shutdown() {
echo "Received shutdown signal, stopping server..."
if [ -n "$SCUM_PID" ]; then
echo "Sending SIGINT to SCUMServer.exe (PID $SCUM_PID)..."
kill -INT "$SCUM_PID" 2>/dev/null || true
# Wait up to 60 seconds for graceful shutdown
for _ in {1..60}; do
if ! kill -0 "$SCUM_PID" 2>/dev/null; then
echo "Server stopped gracefully"
exit 0
fi
sleep 1
done
echo "Server did not stop in time, forcing shutdown..."
kill -KILL "$SCUM_PID" 2>/dev/null || true
fi
# Also stop the xvfb-run wrapper to clean up
if [ -n "$WRAPPER_PID" ]; then
kill -TERM "$WRAPPER_PID" 2>/dev/null || true
fi
exit 0
}
# Start server in background so we can handle signals
# Disable shellcheck warning, quoting does more harm than use in this particular case
# shellcheck disable=SC2086
xvfb-run --auto-servernum --server-args="-screen 0 1024x768x24" \
wine /opt/scumserver/SCUM/Binaries/Win64/SCUMServer.exe \
-log \
-port=${GAMEPORT:-7777} \
-QueryPort=${QUERYPORT:-27015} \
-MaxPlayers=${MAXPLAYERS:-32} \
${ADDITIONALFLAGS} &
WRAPPER_PID=$!
echo "Server wrapper started with PID $WRAPPER_PID"
# Wait for SCUMServer.exe to appear and get its PID
echo "Waiting for SCUMServer.exe process..."
SCUM_PID=""
for _ in {1..30}; do
SCUM_PID="$(pgrep -f "Z:.*SCUMServer.exe" | head -1)"
if [ -n "$SCUM_PID" ]; then
echo "SCUMServer.exe found with PID $SCUM_PID"
break
fi
sleep 1
done
if [ -z "$SCUM_PID" ]; then
echo "ERROR: SCUMServer.exe process not found after 30 seconds"
exit 1
fi
# Memory watchdog - monitor memory usage and trigger graceful shutdown if critical
MEMORY_THRESHOLD=${MEMORY_THRESHOLD_PERCENT:-95}
CHECK_INTERVAL=${MEMORY_CHECK_INTERVAL:-60}
MEMORY_WATCHDOG_DEBUG=${MEMORY_WATCHDOG_DEBUG:-false}
if ! [[ "$MEMORY_THRESHOLD" =~ ^[0-9]+$ ]]; then
echo "WARNING: Invalid MEMORY_THRESHOLD_PERCENT value '$MEMORY_THRESHOLD_PERCENT'. Forcing default: 95"
MEMORY_THRESHOLD=95
fi
if ! [[ "$CHECK_INTERVAL" =~ ^[0-9]+$ ]] || [ "$CHECK_INTERVAL" -le 0 ]; then
echo "WARNING: Invalid MEMORY_CHECK_INTERVAL value '$MEMORY_CHECK_INTERVAL'. Forcing default: 60"
CHECK_INTERVAL=60
fi
if [ "$MEMORY_THRESHOLD" -gt 0 ]; then
STARTUP_MEMORY_USAGE=$(LC_ALL=C free | awk '/Mem/{printf("%.0f"), ($2-$7)/$2*100}')
echo "Memory watchdog enabled: SCUM server will initiate graceful shutdown if system-wide memory usage exceeds ${MEMORY_THRESHOLD}% (currently ${STARTUP_MEMORY_USAGE}%, checking every ${CHECK_INTERVAL}s)"
(
while true; do
MEM_USAGE=$(LC_ALL=C free | awk '/Mem/{printf("%.0f"), ($2-$7)/$2*100}')
if [ "$MEM_USAGE" -ge "$MEMORY_THRESHOLD" ]; then
echo "Memory watchdog triggered: memory usage is ${MEM_USAGE}%! Initiating graceful shutdown to prevent data loss..."
shutdown
elif [ "$MEMORY_WATCHDOG_DEBUG" = "true" ]; then
echo "Memory watchdog debug: current memory usage is ${MEM_USAGE}%"
fi
sleep $CHECK_INTERVAL
done
) &
echo "Memory watchdog started."
else
echo "Memory watchdog disabled (MEMORY_THRESHOLD_PERCENT set to 0)"
fi
# Now that SCUM_PID is known, set up signal handlers
trap shutdown SIGTERM SIGINT
# Wait for server process
wait $WRAPPER_PID
exit_code=$?
echo "Server exited with code $exit_code"
exit $exit_code