IMPORTANT NOTES:
►This is a guide on how to run scheduled restarts on a Minecraft Linux-based server. THIS IS NOT A MINECRAFT MOD.
►I use these scripts for a Fabric server. It should also work on Forge, but I would suggest just using THIS MOD, since there are already a couple of them for Forge to do this.
►There are a couple of Fabric mods that claim to do this, however, some of them simply do not work because are outdated or for other reasons. That's why I decided to share this guide. It might work for some but not for others.
►This should work for older versions than 1.19, however, I ONLY tested this on 1.19.2.
►This method should only work on dedicated servers, with a Linux distribution (in my case, Ubuntu).
►Before we begin, you should already know at least the basics for connecting to your server via SSH, FTP, moving/creating files, and also you should be able to have your Minecraft server already running. Preferably, I advise using "screen" to keep the server running. make sure you have that installed.
The way this works: The Minecraft-Fabric server is running using a screen session command on a system service file. There is a one-shot-type file that triggers on a schedule to restart/stop that Minecraft service. There is also a message/warning file that triggers a warning message 1 min before the server restarts. Each of these files have its own "timer" file, so they get triggered when you wish.
Let's begin:
1) First, Download all the files provided. Extract them and open each file using any text-code editor like sublime text. Connect to your server using SSH and FTP so you can transfer/create files. Stop the Minecraft server for now.
2) Open the "minecraft.service" file to change some settings:
-[User=root]: Change the "root" user to the one you are currently using.
-[WorkingDirectory=/root/mcserver/fabric]: Change this to the path where your minecraft-server files are located. Usually where the JAR file is located.
-[ExecStart=screen -S mc -d -m java -Xmx6G -jar fabric-server-mc.1.19.2-loader.0.14.19-launcher.0.11.2.jar nogui]: This command will start your server when triggered. "mc" will be the name of the screen windows that will have your server running. Only change the amount of RAM you would like to use [-Xmx6G] and also the correct name of your .jar file to start the server.
-Save the file.
3)Open the "minecraft.timer" file. This file will trigger the one-shot file to restart the minecraft.service. In the [Timer] section of the file, add the desire restart times you want. In the included file, I restart my server every 3 hours, every day. You can specify multiple days and times just by adding new lines for example:
[Timer]
Unit=oneshot.service
OnCalendar=Mon..Fri 19:00
OnCalendar=Sat..Sun 22:00
-Save the file after editing.
IMPORTANT: Each server/VPS has a different time zone and location. You should keep that in mind when you're assigning the restart times since all of these files are going to use your server timezone, not your local one. You can use the "date" command to show the current date and time zone of your server. From there, you can calculate the correct restart time for your timezone/location.
4)Open the "restartwarning.service" and go to the following line:
-[ExecStart=screen -p 0 -S mc -X eval 'stuff "say SERVER WILL RESTART IN 1 MIN, PLEASE LOG OFF NOW."\\015']: Change the message you want to show after the "say" part, ending before the quotes. do not change or remove anything else from this line. Leave the \\015' numbers as well.
-Save the file.
5)Open the "restart-warning.timer" file, and edit the times you want the warning message to be shown. You can set this to 1 min after the restart times, or 5 mins as well. just make sure to edit the time before the timer triggers a restart on the "minecraft.timer" file.
-Save the file
6) Once you have all the files ready, upload all of them where the server service files are located, usually at "/etc/systemd/system/".
7) You are going to run a few commands in this order, one by one:
►systemctl daemon-reload
►systemctl enable minecraft.service
►systemctl start minecraft.service
►systemctl enable oneshot.service
►systemctl enable minecraft.timer
►systemctl enable restartwarning.service
►systemctl enable restart-warning.timer
►systemctl start minecraft.timer
►systemctl start restart-warning.timer
7) Check the status of the Minecraft service by running: "systemctl status minecraft.service". you should see an "active (exited)" status.
8) Now check if your server is up and running by listing all the running screen windows, enter the command "screen -ls" and you should see a screen instance running named "mc". You can remote into it by using "screen -r mc", you can exit out of that screen using the combination CNTRL+A+D at the same time while being inside the screen you entered.
That should be it. Your server should restart on the desired schedule. You should also get all of those warning messages before the restart as well. The server console should also run the "stop" command to kick all players and save all worlds before restarting. I hope you find this useful, even though I'm not a super pro at programming or a linux pro user, but I will do my best to help if you have any issues.
if you have any questions, send me a DM on TWITTER so I can reply faster.