Jump to content

Linux dedicated server management with systemd


n8atnite

Recommended Posts

hey FOR ALL LINUX SERVER OWNERS i made a systemd service that works with 64-bit PZ. If you aren't familiar with systemd, it's the process manager that most modern linux distros use. I have tested this on a server with an already-active save folder (I ran the start-server.sh script first, then started using this method).

 

The service file should look like this:

 

Quote

[Unit]
Description=Project Zomboid 41.6x server

 

[Service]
Type=simple
User=[USER]
WorkingDirectory=[DIR]
Environment=PATH=[DIR]/jre64/bin:$$PATH
Environment=LD_LIBRARY_PATH=[DIR]/linux64:[DIR]/natives:[DIR]:[DIR]/jre64/lib/amd64:$$LD_LIBRARY_PATH
ExecStart=/usr/bin/screen -DmS pzserver [DIR]/ProjectZomboid64
ExecStop=/usr/bin/rcon -H localhost -p 27015 -P [PASSWD] save
ExecStop=/usr/bin/rcon -H localhost -p 27015 -P [PASSWD] quit
ExecStop=/usr/bin/screen -S pzserver -X quit

 

[Install]
WantedBy=multi-user.target
Alias=pzserver

 

Replace [USER] with the user that first executed the server (it might be steam), otherwise the server will prompt for a nonexistent admin account, then quit without loading the world. Replace [DIR] with the *absolute* path to your "Project Zomboid Dedicated Server" game directory (i.e. /home/user/.steam/steamapps/common/Project Zomboid Dedicated Server).

 

Save the file as pzserver.service, then copy it to /etc/systemd/system.
Reboot systemd: systemctl daemon-reload
Start the server: systemctl start pzserver
Check its status: systemctl status pzserver. You should see a green active status with terminal output similar to that of start-server.sh.
To stop the server: systemctl stop pzserver. This will kill it immediately, so make sure to save it first. I recommend using RCON for sending commands like "save".
To restart the server: systemctl restart pzserver. This is the same as stop then start.
To make the server run at bootup: systemctl enable pzserver.

 

Here is an RCON client that will work with PZ: https://github.com/n0la/rcon.

If you have not installed this, the "ExecStop" lines with rcon should be removed. The service should still work, but the server will stop ungracefully. With autosave this shouldn't be an issue but keep in mind some things may break if you shut down with players online.

 

You can easily integrate these commands with any scripts or admin tools you have written. Happy admin-ing :)

Edited by n8atnite
updated to use screen, works much better now
Link to comment
Share on other sites

Pro tips:

 

Use symbolic links to make the directory path to your server files shorter/easier. Spaces in paths suck, so don't use them! (see the ln -s command in bash)

 

Make a bash alias for your rcon command. Put something like "alias pzcmd=/usr/bin/rcon -H localhost -p 27015 -P [PASSWD]" in ~/.bash_aliases (if on Ubuntu) and do source ~/.bashrc to reload your bash settings.

Link to comment
Share on other sites

And to connect to the server's shell, use screen -ls to find active sessions, then do screen -x [session id.name] to connect. You should see output log of the server and will be able to type commands, although using RCON is a better option for that.

Link to comment
Share on other sites

  • 1 month later...

Big thanks for this.

 

Super useful and I wouldn't even know where to start with making a service.

 

Now users on my small server can restart PZ with a discord bot if they have a mod mismatch and I'm not about.

Link to comment
Share on other sites

  • 11 months later...

Thanks for this head start.  I wasn't a fan of adding all those environment variables and working around start-server.sh.  I deviated a little from yours which I feel is probably more "noob friendly".  Here is how I have it working:

 

[Unit]
Description=Zomboid Dedicated Server
After=network.target

[Service]
Type=simple
User=[USER]
Group=[GROUP]
ExecStart=/bin/bash --login -c 'source [HOME]/.bashrc; cd [GAME DIR] && /usr/bin/screen -DmS [NAME] ./start-server.sh -servername [NAME]'
ExecStop=/usr/bin/rcon -a 127.0.0.1:27015 -p [RCON PW] save
ExecStop=/usr/bin/rcon -a 127.0.0.1:27015 -p [RCON PW] quit
ExecStop=/bin/bash --login -c 'source [HOME]/.bashrc; /usr/bin/screen -S [NAME] -X quit'
Restart=on-failure

[Install]
WantedBy=multi-user.target
Alias=[NAME]

 

I used a different variant of rcon so my commands are slightly different but they still perform the same tasks.  The build I used is found here: https://github.com/gorcon/rcon-cli

 

[USER] and [GROUP] are your login user and group

[HOME] is your user's home directory.  Usually defined as ~ but I used the full path just to be safe.

[GAME DIR] is the directory where the server files are located

[NAME] can be whatever you want.  I just put the server name for those.  The [NAME] for both screen commands needs to match though.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...