Comments (20)
This is a Python script that dynamically pauses and restarts the server when it detects no players and when a player joins.
First, install tcpdump
, then run the script.
Replace TARGET_PORT
, CONTAINER_NAME
, and sudo_password
with your values.
import subprocess
import time
TARGET_PORT = 8211
CONTAINER_NAME = "palworld-server"
sudo_password = "your_sudo_password_here"
def check_players():
players_output = run_command(
f"docker exec -it {CONTAINER_NAME} rcon-cli ShowPlayers"
)
print(f"player: {players_output}")
if players_output == "name,playeruid,steamid" or not players_output:
return False
else:
return True
def run_command(command):
"""Helper function to run a shell command and return its output"""
result = subprocess.run(
command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, text=True
)
return result.stdout.strip()
def main():
if not check_players():
print("No players found. Restart the server.")
run_command(f"docker restart {CONTAINER_NAME}")
for sec in range(30, 0, -1):
print(f"Waiting for restart to complete... {sec} seconds left")
time.sleep(1)
# if not players, pause server
if not check_players():
print("Restart complete. Pause the server.")
run_command(f"docker pause {CONTAINER_NAME}")
player = False
print("Waiting for players to connect")
else:
player = True
while not player:
# Use tcpdump to capture incoming traffic on the target port
capture_command = f"echo {sudo_password} | sudo -S tcpdump -n -c 1 -i any port {TARGET_PORT} 2>/dev/null"
capture_result = run_command(capture_command)
# Check if any packets were captured
if capture_result:
player = True
print(f"Connection attempt detected on port {TARGET_PORT}")
print("Starting server")
run_command(f"docker unpause {CONTAINER_NAME}")
for sec in range(30, 0, -1):
print(f"Waiting for players to connect... {sec} seconds left")
time.sleep(1)
else:
time.sleep(1)
else:
print("Players found. Server will continue running.")
time.sleep(60)
if __name__ == "__main__":
while True:
main()
-
detect players on the server every minute, and if there are no players, restart and pause the server.
Restarting the server is for resource optimization, and pausing the server is for quick server restarts. -
When the server is paused, it detects connections. When a connection is detected, unpause the server.
This works well for me. Feel free to modify it to suit your needs.
from palworld-server-docker.
#!/bin/bash
# Function to check players and take action based on the response
check_players() {
players_output=$(docker exec -it palworld-server rcon-cli ShowPlayers)
# Check if the response contains the header followed by an empty line
if echo "$players_output" | grep -q "name,playeruid,steamid"$'\n'$'\n'; then
echo "No players found. Shutting down the server."
docker exec -it palworld-server rcon-cli Shutdown
else
echo "Players found. Server will continue running."
# You can add additional actions or commands here if needed
fi
}
check_players
this in a nice little loop with a delay et voilà
to start the server i have something similar, unfortunately, the server has to be reached twice, as the game times out during first connection and server boot.
#!/bin/bash
TARGET_PORT=8211
while true; do
# Use tcpdump to capture incoming traffic on the target port
capture_result=$(sudo tcpdump -n -c 1 -i any port $TARGET_PORT 2>/dev/null)
# Check if any packets were captured
if [ -n "$capture_result" ]; then
# Trigger your event here (replace with your desired action)
echo "Connection attempt detected on port $TARGET_PORT"
echo "starting server"
docker start palworld-server
# End the loop
break
fi
# Adjust the sleep duration based on your monitoring requirements
sleep 1
done
# Optionally, you can perform cleanup or additional actions after the loop
echo "Loop ended"
from palworld-server-docker.
That's indeed an excellent workaround in the meantime, I might try and have a stab at it (and open a PR!) if I find the free cycles :)
Cheers !
from palworld-server-docker.
For users who do not ask for passwords when using the sudo
command, the echo {sudo_password} |
part must be deleted for normal operation. Otherwise, the tcpdump
command will not wait for a request.
this
capture_command = f"echo {sudo_password} | sudo -S tcpdump -n -c 1 -i any port {TARGET_PORT} 2>/dev/null"
to
capture_command = f"sudo -S tcpdump -n -c 1 -i any port {TARGET_PORT} 2>/dev/null"
Therefore, I don't need part sudo_password = "your_sudo_password_here"
too, delete it.
Sometimes Weird. This response is for another request.
error is included in the result when using the rcon-cli
command. If so, check_players
is not returned properly.
If you modify the function as follows, it works fine.
def check_players():
players_output = run_command(
f"docker exec -it {CONTAINER_NAME} rcon-cli ShowPlayers"
)
print(f"player: {players_output}")
return "," in players_output.replace("name,playeruid,steamid", "")
P.S. I'm using a modified Dockerfile for use in ARM64, and I have also changed the rcon-cli to https://github.com/itzg/rcon-cli. That may be why this is happening.
+ After writing this, I checked that the ARM version of Dockerfile was created in this repository. With this, this error doesn't seem to occur.
from palworld-server-docker.
This is a Python script that dynamically pauses and restarts the server when it detects no players and when a player joins.
First, install
tcpdump
, then run the script. ReplaceTARGET_PORT
,CONTAINER_NAME
, andsudo_password
with your values.import subprocess import time TARGET_PORT = 8211 CONTAINER_NAME = "palworld-server" sudo_password = "your_sudo_password_here" def check_players(): players_output = run_command( f"docker exec -it {CONTAINER_NAME} rcon-cli ShowPlayers" ) print(f"player: {players_output}") if players_output == "name,playeruid,steamid" or not players_output: return False else: return True def run_command(command): """Helper function to run a shell command and return its output""" result = subprocess.run( command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, text=True ) return result.stdout.strip() def main(): if not check_players(): print("No players found. Restart the server.") run_command(f"docker restart {CONTAINER_NAME}") for sec in range(30, 0, -1): print(f"Waiting for restart to complete... {sec} seconds left") time.sleep(1) # if not players, pause server if not check_players(): print("Restart complete. Pause the server.") run_command(f"docker pause {CONTAINER_NAME}") player = False print("Waiting for players to connect") else: player = True while not player: # Use tcpdump to capture incoming traffic on the target port capture_command = f"echo {sudo_password} | sudo -S tcpdump -n -c 1 -i any port {TARGET_PORT} 2>/dev/null" capture_result = run_command(capture_command) # Check if any packets were captured if capture_result: player = True print(f"Connection attempt detected on port {TARGET_PORT}") print("Starting server") run_command(f"docker unpause {CONTAINER_NAME}") for sec in range(30, 0, -1): print(f"Waiting for players to connect... {sec} seconds left") time.sleep(1) else: time.sleep(1) else: print("Players found. Server will continue running.") time.sleep(60) if __name__ == "__main__": while True: main()
1. detect players on the server every minute, and if there are no players, restart and pause the server. Restarting the server is for resource optimization, and pausing the server is for quick server restarts. 2. When the server is paused, it detects connections. When a connection is detected, unpause the server.
This works well for me. Feel free to modify it to suit your needs.
Hi, I was able to test the script on my server.
This seems to be a good solution to get around the problem with depressed pals.
This solution is fast enough that there is no connection abort when the server is paused and a player connects.
Thank you.
from palworld-server-docker.
I've played around with different ways to get this to work with a simple bash script. Also got inspired by @fremus89's example. I now have a script running locally on the server where I host the dedicated server for me and my friends. If anyone's interested, I put together a little Docker container that's ready to use in conjunction with this project's Docker container: https://github.com/dnwjn/palworld-server-watcher.
from palworld-server-docker.
I've played around with different ways to get this to work with a simple bash script. Also got inspired by @fremus89's example. I now have a script running locally on the server where I host the dedicated server for me and my friends. If anyone's interested, I put together a little Docker container that's ready to use in conjunction with this project's Docker container: https://github.com/dnwjn/palworld-server-watcher.
Just wanted to give you a shoutout here as this is exactly what I was looking for. I really didn't want to have to install Python and set up jobs to monitor my server. Having another Docker container that can handle it all is perfect, and it's been working great
from palworld-server-docker.
@dnwjn Would you mind if I add some documentation from your repo to my docs website?
Not at all! 😄
from palworld-server-docker.
Would be a great feature!
from palworld-server-docker.
I support this, it would also prevent the server to reach huge number in days xD
I do think this might as well be implemented at the game level to have a feature to "freeze time" once the last player disconnects.
from palworld-server-docker.
You could use something via RCON to watch the status of /ShowPlayers
maybe? At least, it could shutdown when 0 players. The user would have to start it back up.
from palworld-server-docker.
You could use something via RCON to watch the status of
/ShowPlayers
maybe? At least, it could shutdown when 0 players. The user would have to start it back up.
I have no idea on how to implement this. Can you describe this a bot more?
from palworld-server-docker.
script.txt
Something like this do I have in mind, but is untested
from palworld-server-docker.
I'd use rcon
to list the players too.
from palworld-server-docker.
Question is would this mean I need to manually start the server each time a new player wants to connect ?
from palworld-server-docker.
Any way to do this in the background and at boot of my home server? Works great if it's actively running in my terminal window, but when I try to make it a cron job, it doesn't seem to recognize that there's anyone on the server and reboots then pauses after 60s. It does correctly detect when someone joins and unpauses the container though (but then repeats the cycle after 60s). I've also just tried starting it in the background with &
but it immediately stops the job.
My cron was nohup python3 server.py &
I suspect the fact that it reads outputs from print is the issue.
from palworld-server-docker.
i use it with tmux, so i can just detach the session and reattach, nohup caused issues for me as well
https://tmuxcheatsheet.com/
i usually reboot by hand, so i start the script again
from palworld-server-docker.
That's perfect, thank you!
from palworld-server-docker.
I've played around with different ways to get this to work with a simple bash script. Also got inspired by @fremus89's example. I now have a script running locally on the server where I host the dedicated server for me and my friends. If anyone's interested, I put together a little Docker container that's ready to use in conjunction with this project's Docker container: https://github.com/dnwjn/palworld-server-watcher.
Just wanted to give you a shoutout here as this is exactly what I was looking for. I really didn't want to have to install Python and set up jobs to monitor my server. Having another Docker container that can handle it all is perfect, and it's been working great
Happy to hear it's useful to someone else as well!
from palworld-server-docker.
@dnwjn Would you mind if I add some documentation from your repo to my docs website?
from palworld-server-docker.
Related Issues (20)
- many errors `cli: execute: rcon: read tcp 127.0.0.1:PORT->127.0.0.1:25575: i/o timeout` and disconnected HOT 6
- Docker release version checker in Image HOT 3
- Discord Backup Message not always sending HOT 12
- Error in documentation for starting docker HOT 1
- Deletion messages for old backup files are not posted to Discord HOT 3
- SIGSEGV: invalid attempt to write memory at address HOT 1
- Today the server was upgraded to 0.2.0.6 and it doesn't work anymore HOT 7
- Update backup for 0.2.0.6 HOT 1
- Update restore for 0.2.0.6
- Server suddenly assigns new Steam/Player IDs HOT 4
- Discord Server Update Complete Message Not Sending HOT 1
- Could you add a env to disable .ini config file auto generation? HOT 1
- Strange things with the ENABLE_PLAYER_LOGGING function and discord messages after the last update HOT 3
- steamclient.so - No such file or directory HOT 3
- "SERVER_SETTINGS_MODE is set to 'manual', NOT using environment variables to configure the server!" HOT 1
- Can't change ENV parameters in docker-compose.yml (PalWorldSettings.ini doesn't work) HOT 2
- Palworld Server is not updating HOT 6
- steamcmd missing file permissions HOT 12
- MacOS Docker Desktop cant run the image HOT 25
- Old server LGSM to new server docker Plaword HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from palworld-server-docker.