Minecraft restart bash script won't run init.d/service command

Solution 1:

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 with systemctl 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:

# Automatically generated by systemd-sysv-generator

[Unit]
Documentation=man:systemd-sysv-generator(8)
SourcePath=/etc/init.d/minecraft
Description=Minecraft server
Before=multi-user.target
Before=multi-user.target
Before=multi-user.target
Before=graphical.target
Before=shutdown.target
After=local-fs.target
After=remote-fs.target
After=network-online.target
Wants=network-online.target
Conflicts=shutdown.target

[Service]
Type=forking
Restart=always
TimeoutSec=1min
RestartSec=5
IgnoreSIGPIPE=no
KillMode=process
RemainAfterExit=no
PIDFile=/home/MyUser/minecraft/minecraft-server.pid
ExecStart=/etc/init.d/minecraft start
ExecStop=/etc/init.d/minecraft stop

[Install]
WantedBy=multi-user.target

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 as active 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 by systemd. The Minecraft init script will create this on startup.
  • ExecStart the comand to run to start the service
  • ExecStop the command to run to kill the service

Testing 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 use screen to connect to the the Minecraft console screen -r minecraft-server, now we can type a command for the server to stop either stop or restart, 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 the RestartSec time specified has elapsed, you should be able to verify the the Minecraft service is running again by either reconnecting to the screen again or using systemctl status minecraft (use q 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).