mathielo / dst-dedicated-server

Don't Starve Together dedicated server guide for all platforms (Linux, Mac, Windows) with Docker. Extensive documentation covering mods installation, server config and performance, world generation and setting up admins.
MIT License
296 stars 56 forks source link

Help with clean shutdown command #13

Open Molorius opened 5 years ago

Molorius commented 5 years ago

Host OS: Arch Linux Local Computer

This isn't an issue so much as a help request. I am starting my server using systemd, but want to be able to cleanly close it without attaching to the docker session and sending 'c_shutdown()'. You can't do a simple docker exec because it's part of an interactive shell, not a direct command. I found this possible solution, so I sent:

echo "c_shutdown()" | docker attach dst_master

But I get the error:

the input device is not a TTY

I'm not familiar with docker, is there another way I can pipe the command into the session?

mathielo commented 5 years ago

Hey @Molorius I tried some ideas but unfortunately didn't manage to make it work. I remember this is something I wanted to implement long ago, i.e. create a stop.sh script that would do a clean shutdown saving the game, but never got it to work.

docker attach seems a bit limited as it works only in a very specific way (interactively). Tried with docker exec, which spawns another process in the container (e.g. another bash), but couldn't attach tty or send the c_shutdown() signal to the running proccess, dontstarve_dedicated_server_nullrenderer.

What I usually do when playing is to manually shut it down if I'm the last player to leave through the game console directly. It's still a manual process, but quicker.


Let's leave the issue open, if we ever come around a solution we can share it here as I believe more people would benefit from it :nerd_face:

Molorius commented 5 years ago

I got it to work using socat.

echo "c_shutdown()" | socat EXEC:"docker attach dst_master",pty STDIO

By putting that in a "my_shutdown.sh" script, I also got a working systemd file:

[Unit]
Description=Don't Starve Together Server
After=docker.service
After=network.service

[Service]
Type=oneshot
RemainAfterExit=yes
User={user you want starting this container}
WorkingDirectory={your working directory here}
ExecStart=docker-compose up -d
ExecStop=/bin/bash my_shutdown.sh

[Install]
WantedBy=multi-user.target