Project Zomboid

Project Zomboid

科米利托 Aug 10, 2024 @ 8:13pm
[LINUX] Dedicated Server management
Hi,
I've set up a dedicated server on a Linux machine that's configured to automatically restart at a specific time. However, I'm facing difficulties in automating the execution of save and quit commands before the server shuts down. I attempted to use RCON to issue these commands, but it seems it's not executing them as expected—there might be a misconfiguration on my part.

I've also come across a suggestion to use tmux for managing server commands, but since the server is already running with systemd, I'm hesitant to integrate tmux without ensuring I have the proper support and troubleshooting procedures in place.

Is there a way to set up a series of commands that can be executed automatically at a specified time, ideally without using tmux and RCON?

Thank you for your help.
< >
Showing 1-7 of 7 comments
Beard Aug 11, 2024 @ 3:44am 
Not familiar with system.d config, since the dedicated server wiki page recommends specifically against it. But are you able to access the server console through system.d commands (Such as Tmux can)?

If you can, then you can just use cron to periodically send the commands to the server console.
科米利托 Aug 11, 2024 @ 3:51am 
Originally posted by Beard:
Not familiar with system.d config, since the dedicated server wiki page recommends specifically against it. But are you able to access the server console through system.d commands (Such as Tmux can)?

If you can, then you can just use cron to periodically send the commands to the server console.
That's the worst part - I can't.

Basically, I created a scheduled task to start the server whenever the machine boots up. Similarly, I did the same for device shutdown. Yesterday, after the restart, our cars vanished, but when we left the chunk, they reappeared. So maybe the quit and save commands aren't that necessary after all?

Currently, the system startup is handled by a .service file in /etc/systemd/system/, which basically just starts the .sh file in the game folder. I assume if I delete this task and instead run it with tmux, I'll be able to access the console and run the commands, correct? But how do I schedule tmux to start automatically once the machine boots up? Do I make a different .service file in /etc/systemd/system/ for tmux startup? That's the part I don't really understand.
Beard Aug 11, 2024 @ 6:02am 
I have not personally tried to automate starting on startup, but you can just make a script run at startup which launches tmux with the necessary commands to start the game, the link below is an example of it:

https://www.geeksforgeeks.org/how-to-set-tmux-for-opening-specified-windows-at-startup/

You would basically just script starting tmux, then send-keys to Tmux to launch the startserver.sh file, and you should be done at that point..

Then you can create a cron job to tmux send-keys for any commands you want into the session name.

There may be a way to do something similar with system.d as well, but I am not well versed in it at all, and if it doesnt allow sending commands to the running console then it would be impossible I believe.
Last edited by Beard; Aug 11, 2024 @ 6:04am
科米利托 Aug 11, 2024 @ 7:01am 
So basically, I tried like this:

#!/bin/bash tmux send-keys -t pzserver 'say Server will restart in 10 minutes' C-m sleep 600 tmux send-keys -t pzserver 'save' C-m sleep 10 tmux send-keys -t pzserver 'quit' C-m

I created a timer to run this command at a specified time and then another one for device reboot. I'll test it to check if it works.

Like I said before, yesterday we had an issue where the cars disappeared, and we were afraid something didn't save correctly and the cars despawned. However, they reappeared once we left the chunk. Now, when I logged in, the cars were still here after the restart. Note that I still didn't run the commands save and quit yesterday. So my question is: is it really that important? I mean, I guess so, but it looks like the server saves automatically, or even when the device reboots, it keeps the progress up to the very end.
Beard Aug 11, 2024 @ 11:09am 
I consider it fairly important, cars specifically may not be affected, but stuff like time tracking, zombies and safehouse / faction info is definitely affected. People have reported loading into their bases filled with zombies, since if you do not quit correctly, the game wont track the zombies properly.
科米利托 Aug 12, 2024 @ 8:24am 
For some reason tmux doesn't want to work properly. When I target start-server.sh via tmux it loads the configuration partially - the server name is fine, the description is fine but no mods get loaded, the amount of players is incorrect etc. Therefore I decided to give a RCON method another try and like I said before I was doing something wrong - first time using RCON, I first used the IP of the machine on which the server is operating, then I tried server but today I decided to go with 127.0.0.1.

Let me just summarize everything so that anyone with similar issue can try the following steps to resolve the issue. Note that this is for people that want to automate the server and are NOT using tmux.

Prerequisites
Download mcrcon and configure the .ini file containing server's config to set the password, port and enable RCON. Unlock port which will be used for RCON (by default 27015).

Test the RCON connection with the server via:
mcrcon -H HOST -P PORT -p PASSWORD

Automation

Create new .sh file for server restart in server folder, for example rcon_restart.sh:
#!/bin/bash RCON_HOST="127.0.0.1" RCON_PORT="27015" RCON_PASSWORD="YOUR_PASSWORD" echo "save" | mcrcon -H $RCON_HOST -P $RCON_PORT -p $RCON_PASSWORD echo "quit" | mcrcon -H $RCON_HOST -P $RCON_PORT -p $RCON_PASSWORD

Make sure the script is executable:
chmod +x rcon_restart.sh

Create new .service file in /etc/systemd/system/
[Unit] Description=Project Zomboid RCON Command Service After=network.target [Service] Type=oneshot User=PZServer ExecStart=/PATH_TO_YOUR_SERVER_FOLDER/rcon_restart.sh [Install] WantedBy=multi-user.target

Create new .timer file in /etc/systemd/system/ note that the OnCalendar uses 24h format.
[Unit] Description=Timer for server restart [Timer] OnCalendar=--* 00:00:00 Persistent=true [Install] WantedBy=timers.target

Make sure you adjust all the timers to work together - if your service for start-server.sh starts when system boots up you can skip this but if you restart the server multiple times or don't restart the machine on which the server is running then you'll have to make sure the timers are not intertwining.
Last edited by 科米利托; Aug 12, 2024 @ 8:26am
Sokoloft Nov 30, 2024 @ 3:58pm 
I seen this googling stuff. I made something similar to this a few years ago but just re-did it. I prefer piping commands into the `zomboid.control` file. Here is my bash script. Just having issues sometimes that the server doesn't close cleanly. So the zomboid.control file exists with the word `quit` in it causing the server to shutdown as soon as it boots up. The `rm -f $zctrl` SHOULD hopefully fix that.

#!/usr/bin/sh # 11/30/24 # Add the following to your crontab # Ensure to uncomment the crontab string #pz #50 23,5,11,17 * * * sh /home/steam/Games/pzserv/restart.sh # Put this file alongside the startup script # Sets working dir cd "$(dirname "$0")" # Alias zomboid.control file zctrl=zomboid.control # Check if the server is running. If it is, run below code. Else exit. if (systemctl -q is-active pzserv.service); then echo "Starting pzserv-restart script." # Writes server commands to the zomboid.control file. echo "servermsg \"Server Restart in 10 Minutes!\"" > $zctrl sleep 300 echo "servermsg \"Server Restart in 5 Minutes!\"" > $zctrl sleep 240 echo "servermsg \"Server Restart in 1 Minute!\"" > $zctrl sleep 60 echo "servermsg \"Server is Restarting! Rejoin in a few minutes.\"" > $zctrl sleep 3 # Stop the service systemctl stop pzserv.service # Loop to check when the service has actually stopped. while true; do sleep 3 if (systemctl -q is-active pzserv.service); then echo "Service is active, check again in 3 seconds." else echo "Service is dead, starting service now." # Ensures clean startup rm -f $zctrl # Start the service systemctl start pzserv.service break fi done else echo "Application is not running.." # Ensures clean startup rm -f $zctrl fi echo "pzserv-restart exiting..." exit
< >
Showing 1-7 of 7 comments
Per page: 1530 50

Date Posted: Aug 10, 2024 @ 8:13pm
Posts: 7