@@ -9,6 +9,7 @@ TCPDUMP_TIMEOUT=30
9
9
10
10
running=false
11
11
skip_sleep=false
12
+ shutdown_scheduled_at=false
12
13
13
14
echo_line () {
14
15
echo -e " $1 "
@@ -41,7 +42,7 @@ echo_debug() {
41
42
}
42
43
43
44
is_running () {
44
- if [ " $( docker container inspect -f ' {{.State.Status}}' ${ CONTAINER_NAME} ) " = " running" ]; then
45
+ if [ " $( docker container inspect -f ' {{.State.Status}}' $CONTAINER_NAME ) " = " running" ]; then
45
46
return 0
46
47
else
47
48
return 1
@@ -57,11 +58,11 @@ start_discord_handler() {
57
58
}
58
59
59
60
start_tunnel () {
60
- echo_line " Started tunnel on game port ${ GAME_PORT} to ${ CONTAINER_NAME} ."
61
+ echo_line " Started tunnel on game port $GAME_PORT to $CONTAINER_NAME ."
61
62
socat TCP4-LISTEN:$GAME_PORT ,fork,reuseaddr TCP4:$CONTAINER_NAME :$GAME_PORT &
62
63
63
64
if [ -n " $QUERY_PORT " ]; then
64
- echo_line " Started tunnel on query port ${ QUERY_PORT} to ${ CONTAINER_NAME} ."
65
+ echo_line " Started tunnel on query port $QUERY_PORT to $CONTAINER_NAME ."
65
66
socat TCP4-LISTEN:$QUERY_PORT ,fork,reuseaddr TCP4:$CONTAINER_NAME :$QUERY_PORT &
66
67
fi
67
68
}
@@ -74,37 +75,37 @@ check_for_start() {
74
75
return
75
76
fi
76
77
77
- echo_debug " Listening for connection attempts on port ${ GAME_PORT} for ${ TCPDUMP_TIMEOUT} seconds..."
78
+ echo_debug " Listening for connection attempts on port $GAME_PORT for $TCPDUMP_TIMEOUT seconds..."
78
79
79
80
# Command to trigger locally: nc -vu localhost 8211
80
81
tcpdump_output=$( timeout $TCPDUMP_TIMEOUT tcpdump -n -c 1 -i any port $GAME_PORT 2>&1 )
81
82
82
83
if echo " $tcpdump_output " | grep -q " 0 packets captured" ; then
83
- echo_debug " No traffic logged for ${ TCPDUMP_TIMEOUT} seconds."
84
+ echo_debug " No traffic logged for $TCPDUMP_TIMEOUT seconds."
84
85
skip_sleep=true
85
86
return
86
87
fi
87
88
88
- echo_debug " Connection attempt detected on game port ${ GAME_PORT} ."
89
+ echo_debug " Connection attempt detected on game port $GAME_PORT ."
89
90
90
91
echo_info " ***STARTING SERVER***"
91
- docker start " ${ CONTAINER_NAME} " > /dev/null
92
+ docker start " $CONTAINER_NAME " > /dev/null
92
93
93
94
max_attempts=10
94
95
attempt=0
95
- until [ $attempt -ge $max_attempts ] || docker inspect --format=' {{.State.Health.Status}}' " ${ CONTAINER_NAME} " 2> /dev/null | grep -q " healthy" ; do
96
+ until [ $attempt -ge $max_attempts ] || docker inspect --format=' {{.State.Health.Status}}' " $CONTAINER_NAME " 2> /dev/null | grep -q " healthy" ; do
96
97
echo_line " Waiting for the server to be healthy..."
97
98
sleep 5
98
99
attempt=$(( attempt + 1 ))
99
100
done
100
101
101
102
if [ $attempt -ge $max_attempts ]; then
102
- echo_warning " Server did not become healthy after ${ max_attempts} attempts. Please check the server logs."
103
+ echo_warning " Server did not become healthy after $max_attempts attempts. Please check the server logs."
103
104
else
104
105
echo_success " Server is healthy."
105
106
106
- echo_line " Allowing users ${ CONNECT_GRACE_SECONDS} seconds to connect..."
107
- sleep " ${ CONNECT_GRACE_SECONDS} "
107
+ echo_line " Allowing users $CONNECT_GRACE_SECONDS seconds to connect..."
108
+ sleep " $CONNECT_GRACE_SECONDS "
108
109
109
110
running=true
110
111
skip_sleep=true
@@ -115,21 +116,47 @@ check_for_stop() {
115
116
echo_debug " Checking for players..."
116
117
117
118
if is_running; then
118
- players_output=$( docker exec -i " ${CONTAINER_NAME} " rcon-cli ShowPlayers)
119
+ shutdown_now=false
120
+ players_output=$( docker exec -i " $CONTAINER_NAME " rcon-cli ShowPlayers)
119
121
120
122
if [ " $players_output " = " name,playeruid,steamid" ]; then
121
- echo_line " No players found. Server will be shut down."
122
- echo_info " ***STOPPING SERVER***"
123
+ now=$( date +%s)
124
+
125
+ if [ " $shutdown_scheduled_at " != false ]; then
126
+ echo_debug " Scheduled: $( date -d " @$shutdown_scheduled_at " +" %Y-%m-%d %H:%M:%S" ) | Now: $( date -d " @$now " +" %Y-%m-%d %H:%M:%S" ) "
127
+
128
+ if [[ $shutdown_scheduled_at -le $now ]]; then
129
+ echo_debug " Scheduled shutdown is in the past."
130
+ shutdown_now=true
131
+ fi
132
+ else
133
+ if [ " $SHUTDOWN_DELAY_SECONDS " != " 0" ]; then
134
+ echo_line " No players found. Server will be shut down in $SHUTDOWN_DELAY_SECONDS seconds."
135
+ shutdown_scheduled_at=$(( $now + $SHUTDOWN_DELAY_SECONDS ))
136
+ echo_debug " Shutdown scheduled for $( date -d " @$shutdown_scheduled_at " +" %Y-%m-%d %H:%M:%S" ) (current time: $( date -d " @$now " +" %Y-%m-%d %H:%M:%S" ) )"
137
+ else
138
+ echo_line " No players found. Server will be shut down."
139
+ shutdown_now=true
140
+ fi
141
+ fi
142
+ elif [ " $shutdown_scheduled_at " != false ]; then
143
+ echo_debug " Scheduled shutdown interrupted because server is not empty."
144
+ shutdown_scheduled_at=false
145
+ fi
123
146
124
- docker stop " ${CONTAINER_NAME} " > /dev/null
147
+ if [ " $shutdown_now " = true ]; then
148
+ echo_info " ***STOPPING SERVER***"
149
+ docker stop " $CONTAINER_NAME " > /dev/null
125
150
126
151
running=false
127
152
skip_sleep=true
153
+ shutdown_scheduled_at=false
128
154
fi
129
155
else
130
156
echo_debug " Server has already been shut down."
131
157
running=false
132
158
skip_sleep=true
159
+ shutdown_scheduled_at=false
133
160
fi
134
161
}
135
162
@@ -145,8 +172,8 @@ run() {
145
172
146
173
if is_running; then
147
174
echo_line " Server is already running."
148
- echo_line " Allowing users ${ CONNECT_GRACE_SECONDS} seconds to connect..."
149
- sleep " ${ CONNECT_GRACE_SECONDS} "
175
+ echo_line " Allowing users $CONNECT_GRACE_SECONDS seconds to connect..."
176
+ sleep " $CONNECT_GRACE_SECONDS "
150
177
running=true
151
178
else
152
179
echo_line " Server is not running."
@@ -163,8 +190,8 @@ run() {
163
190
if [ " $skip_sleep " = true ]; then
164
191
skip_sleep=false
165
192
else
166
- echo_debug " Sleeping for ${ LOOP_SLEEP_SECONDS} seconds..."
167
- sleep " ${ LOOP_SLEEP_SECONDS} "
193
+ echo_debug " Sleeping for $LOOP_SLEEP_SECONDS seconds..."
194
+ sleep " $LOOP_SLEEP_SECONDS "
168
195
fi
169
196
done
170
197
}
0 commit comments