What I intend to do
I'm attempting to make a restart script for my Minecraft server so my friends can use the /restart
command in-game because they don't have SSH access to my server to restart Minecraft.
My script
Currently my restart.sh
script contains
exec >> zzz.log
exec 2>&1
set -x
sleep 5
exec /etc/init.d/minecraft start
Script break down
The first two lines will redirect standardout and standarderror (as Linux fluent users will already know) into the zzz.log
file, this is just so I can capture the output to help me figure what's going on.
set -x
is also another part to help me figure what's going on, this will output each line that is executed into zzz.log
because the above lines redirect the output to there.
sleep(5)
as it suggests will make the script pause for 5 seconds, this is just to ensure Minecraft has had enough time to shutdown.
exec
will make the following command /etc/init.d/minecraft start
run in a different process. Since I do not know how the script is invoked, I assumed this could be a child process of the Minecraft server and thus if the Minecraft server process dies, the script will too, having it this way should ensure the command is run.
My issue
Now, if the Minecraft server is offline and I execute ./restart.sh
the server starts up perfectly fine. When in-game if I type /restart
the Minecraft server shuts down like its supposed to, but never re-launches again. Looking at my zzz.log
file I can see the problem lays around the exec /etc/init.d/minecraft start
command, this line is never successfully executed when Minecraft executes the restart.sh
script. Why is this?
init script
I obtained the init script from here. This allows me to start
, stop
, restart
the Minecraft server along with various other commands to control the Minecraft server.
Best Answer
How I came to this solution
Thanks to @dly's comment, I now have a solution!
/etc/inittab
doesn't exist on Ubuntu 16.04, but/etc/systemd
does which means the solution is a bit different than what it would have been initially.With a quick read around I was able to find some nice information on systemd unit files. This is probably not the best resource to use since it doesn't tell you if the particular option should be placed under
[Unit]
,[Service]
or[Install]
, but nevertheless, it tells you about the key options needed.Follow along for users with a similar issue
The following will assume one has completed the install steps for Ahtenus' init script for Minecraft/Bukkit servers (this also works perfectly fine for my Spigot server).
To start, you'll need to run this
systemctl enable minecraft.service
. Then you'll probably want to edit the unit file that gets created for you, you can do this withsystemctl edit --full minecraft.service
, this will open a text editor (nano in my case, I didn't get to choose). Here you'll be able to edit the parameters. I configured mine like this:Most of this is auto created for you, but there may be a few options you'll want to change and add, so I'll go through a few of the options.
SourcePath
is the absolute path to the init script.Description
fairly self-explanatory.Restart
has several options, I choose always so no matter how the process dies, it will 'always' be restarted.TimeoutSec
is the timeout for a failed start before it tries to start again.RestartSec
is how long it should wait before starting the service again after shutdown.RemainAfterExit
whether the service should be considered asactive
even after it processes has exited.PIDFile
the path to a PID file for the main process, this should only contain the PID and will be removed when the process is shutdown, but will never be written to bysystemd
. The Minecraft init script will create this on startup.ExecStart
the comand to run to start the serviceExecStop
the command to run to kill the serviceTesting it works
Now we can test this works, in case the service has already been started, we can create a new process using
systemctl restart minecraft
even if there wasn't a process for this service, it will create a new one. Then we can usescreen
to connect to the the Minecraft consolescreen -r minecraft-server
, now we can type a command for the server to stop eitherstop
orrestart
, these can also be used in-game in the same way (remember to prepend the/
when in-game, the console always does this for you). The Minecraft server should now shutdown, and after theRestartSec
time specified has elapsed, you should be able to verify the the Minecraft service is running again by either reconnecting to thescreen
again or usingsystemctl status minecraft
(useq
to exit this).Further reading
I found this page on the Redhat documentation site mildly informative as well. (Link in comments, I don't have enough rep to post 3+ links).