Score:2

How do I make a startup service for a Minecraft server that has a start.sh file?

eg flag

I installed a modded FTB server to my Ubuntu Server, created a user and group called "ftb", and gave it its own directory /opt/ftb/ that contains a folder with the server files. In the server folder, there is a start.sh file to start the server. How do I make a system service file so I can use commands such as systemctl status ftbacademy.service, systemctl start ftbacademy.service, and systemctl stop ftbacademy.service. Upon stopping the server I wish for it to save and then close the server. What would the .service file look like? I know it has to be placed in /etc/systemd/system/, but I don't know how to write the file that would make the ftb user open it's own virtual terminal or whatever to be able to save the world and then stop the server.

Ubuntu Server Version 20.04.3

FTB Academy 1.16 Server Files Download Site Used: https://feed-the-beast.com/modpacks/88/server/2077 I selected the Linux download option then ran the install file to automatically download the server files.

I am also using a different version of JDK to run the server file so I changed the start.sh file to this:

#!/bin/bash
if ! grep -q "eula=true" eula.txt; then
    echo "Do you agree to the Mojang EULA available at https://account.mojang.com/documents/minecraft_eula ?"
    read -N 1 -p "[y/n] " EULA
    if [ "$EULA" = "y" ]; then
        echo "eula=true" > eula.txt
        echo
    fi
fi
/usr/lib/jvm/java-11-openjdk-amd64/bin/java -javaagent:log4jfix/Log4jPatcher-1.0.0.jar -XX:+UseG1GC -XX:+UnlockExperimentalVMOptions -Xmx5000M -Xms4000M -jar forge-1.16.5-36.2.2.jar nogui
Nate T avatar
it flag
which modded ftb server? Did it come from the feedthebeast official site, or somewhere else. List endpoint and I will install it locally and let you know. Need to see the sh file before I tell you how to add it. It can probably be called in `.bashrc`, but I would need to see exactly what it does before giving install advice. Besides, I want a copy of the server. XD Btw you need to add your Ubuntu version to the question. Different versions contain different environments. Most answers will vary at least a tiny bit dependant on version. Especially since we just changed default win mgr in 21.04
lordoftimelords avatar
eg flag
@NateT I believe I have added all the information you have requested. I used the official FTB site and have added the link in the post.
lordoftimelords avatar
eg flag
@NateT `cd /usr/lib/jvm/java-11-openjdk-amd64/bin && ls -a | grep java` outputs `java java javac javadoc javap`. How will this help with the creation of the .service file? I have no issues running the server, I just want to make a .service file to run it on startup, stop the server, and check the status using the ftb user. Forgive me if I'm misunderstanding what's going on. I'm not too familiar with this. I mostly follow very specific guides and copy and paste stuff.
Terrance avatar
id flag
Does this answer your question? [How do I run a single command at startup using systemd?](https://askubuntu.com/questions/919054/how-do-i-run-a-single-command-at-startup-using-systemd)
Nate T avatar
it flag
Also, I see the word `log4j, which scares me a little. It's followed by 'fix', but I wouldn't bet my farm on it. If you're planning putting it in a port-fwded Docker or apache2 server, I wouldn't. See [here.](https://www.youtube.com/watch?v=qjA_vc9Ua5A&t=203s) Its literally a video of a Minecraft server on Ubuntu being injected with payloads containing cmds to be run on the victims system. I would suggest checking out the news and trying to guage the open-source community's confidence in the patch. Evidently, more vulnerabilities have cropped up since I last checked.
Nate T avatar
it flag
OK, IF YOU DONT PLAN TO PORT-FORWARD TO THE INTERNET, run this cmd in bash shell. `MC_CMD='alias start_srvr="/usr/lib/jvm/java-11-openjdk-amd64/bin/java -javaagent:log4jfix/Log4jPatcher-1.0.0.jar -XX:+UseG1GC -XX:+UnlockExperimentalVMOptions -Xmx5000M -Xms4000M -jar forge-1.16.5-36.2.2.jar nogui"' && echo "${MC_CMD}" >> ~/.bash_aliases || echo "${MC_CMD}" >> ~/.bashrc`
lordoftimelords avatar
eg flag
@NateT What if I do plan to port forward? I plan to use the server away from home with friends. I use have whitelist enforcement on and have only forwarded the required port. The user ftb also doesn't have sudo perms. Also, how would I stop the server if I ever had to? I don't quite understand what that command above does. I think I understand that it add an alias to shorten the command to start_srvr, but what does the `&& echo "${MC_CMD}" >> ~/.bash_aliases || echo "${MC_CMD}" >> ~/.bashrc` do?
Nate T avatar
it flag
`sudo chmod 774 <folder-path>` will give you permission needed, but run `ls -l <folder-path> ` and write down the old permission string first, in case you need to change back. Also why would you use systemd to run a start script? Are you wanting to run as a daemon? That wouldn't really make sense, unless you are using it as a dedicated webserver, in which case nevermind...
Nate T avatar
it flag
I would wait a couple weeks... **Watch the video** ^^^. It has a HUGE vulnerability in it. Big enough that it could very well be the final nail in Java's coffin. And that is coming from a professional Java developer. I don't say that lightly.
lordoftimelords avatar
eg flag
@NateT I added a comment to your answer below with a link to a guide that I followed for the vanilla servers. I don't really know the advantages and disadvantages of systemd, and I don't know what daemon is. I just like the functionality of being able to start the server, check the status, and stop the server with commands and not needing to keep an active window sshed into the server to do those things. Also, can I get a link to that video? And does that mean I should close my other servers? I use spigot for them and use TCPShield to prevent ddos and other things if I'm not mistaken.
Nate T avatar
it flag
Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/132813/discussion-between-nate-t-and-lordoftimelords).
Terrance avatar
id flag
Then you might want to see https://askubuntu.com/questions/676007/how-do-i-make-my-systemd-service-run-via-specific-user-and-start-on-boot too on adding `User=` and `Group=` for running as another user in systemd.
Score:2
eg flag

I created a file called [email protected] at the location /etc/systemd/system/. The file contains the following:

[Unit]
Description=FTB Server: %i
After=network.target

[Service]
WorkingDirectory=/opt/ftb/%i

User=ftb
Group=ftb

Restart=always

ExecStart=/usr/bin/screen -DmS mc-%i bash start.sh

ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval 'stuff "say SERVER SHUTTING DOWN IN 15 SECONDS..."\015'
ExecStop=/bin/sleep 5
ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval 'stuff "say SERVER SHUTTING DOWN IN 10 SECONDS..."\015'
ExecStop=/bin/sleep 5
ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval 'stuff "say SERVER SHUTTING DOWN IN 5 SECONDS..."\015'
ExecStop=/bin/sleep 5
ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval 'stuff "save-all"\015'
ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval 'stuff "stop"\015'

[Install]
WantedBy=multi-user.target

Then I used systemctl enable ftb@academy and systemctl start ftb@academy to enable and start the server. I also realized that when I initially created the ftb user and gave it ownership of the folder that I didn't give it permission of the subfolders, so it was denied permission to start the server.

Score:0
it flag

Take notice of the url for the EULA in the script. All it does is ask you have read, and if you accept, its terms, but only if you have not already been asked and answered yes.

So after the first time it is run, It will only ever execute the top and bottom lines.

The easiest way to set it up (which will save time in the long run) is to add an alias for the command on last line, which actually configures and starts the server file. This can be done with:

MC_CMD='alias start_srvr="/usr/lib/jvm/java-11-openjdk-amd64/bin/java -javaagent:log4jfix/Log4jPatcher-1.0.0.jar -XX:+UseG1GC -XX:+UnlockExperimentalVMOptions -Xmx5000M -Xms4000M -jar forge-1.16.5-36.2.2.jar nogui"' && echo "${MC_CMD}" >> ~/.bash_aliases || echo "${MC_CMD}" >> ~/.bashrc

Running the command I added above is just a slight convenience, so you don't need to worry about adding the script to PATH or else having to either calculate the relative path or type the full path every time you want to start your server.

If you ever decide to delete the server, you can remove the "command" by editing ~/.bash_aliases and removing the single line that contains it.

lordoftimelords avatar
eg flag
How would I ever stop the server using this method? I may not fully understand what is going on, but I believe this just starts the server on boot but nothing else really correct? For my other servers I followed this guide https://linuxconfig.org/ubuntu-20-04-minecraft-server-setup and like the ability to stop the server and have it automatically broadcast a server message that says the server is shutting down. I know it is picky, but I would prefer something like that. Is it possible? I don't have the knowledge to recreate this for the ftb server. I tried and was confused.
Nate T avatar
it flag
How does it say to do so in the readme? I can write a function to log it. In fact, with log4j, I can kill it from here! XD No but seriously, the file mentioned at the very end of the alias is the entrypoint. It should have builtin "in-game" admin command to do both of those (log and stop server)
lordoftimelords avatar
eg flag
Typically if you run the start.sh command as a user in the terminal while sshed, then it runs the server actively in the terminal. Then you can type commands into the server (almost like / commands ingame if you have owner or admin privileges) like stop. So it shows the loading of the server, you can type stop to save and stop, use commands like whitelist, blacklist, ipban, etc. I don't need the other commands since I can edit the files containing the whitelisted users, but stop would be nice so I don't have to go into Minecraft, join the server, then command it to stop. It takes time to load.
Nate T avatar
it flag
I see. Ill write something. gimme a second.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.