Autostart script for Ubuntu Nukket server

Sebi

New Member
Hi All,

i am new to nukkit and impressed how easy the installation and the configuration in ubuntu 20.04 LTS was. I connect to the server via SSH.
i managed to write the start.sh script and i am able to run it with:
Code:
./start.sh
But now i am struggling with starting the server after ubuntu reboots. What i have tried so far:
i have added the following line to the crontab of my user:

Code:
@reboot /home/ubuntu/nukkit/start.sh
#
does not work. cron.log does not show any errors just the execution of line above.
Tried the same line while logged in as user -> works.

so i thought its because of the non existing ssh "window".

so i changed the code of the start.sh to:
Code:
/usr/bin/screen -dmS NukkitServer /usr/bin/java -Xms1G -Xmx1G -jar /home/ubuntu/nukkit/nukkit.jar
I am able to resume to the screen of the runnig server after reboot. Instead of starting the server the nukkit installation routine appears and asks for selecting the language. After checking my home directory i can see he was tring to install in home/ubuntu/ instead of /home/ubuntu/nukkit/.

So i was trying to start the script manually
Code:
/usr/bin/screen -dmS NukkitServer /usr/bin/java -Xms1G -Xmx1G -jar /home/ubuntu/nukkit/nukkit.jar
with the same result. He was trying to install the server in the wrong location.
I tried several other things but to shorten this storry a bit i came to the result the script can only be executed correctly when changing to the
Code:
/home/ubuntu/nukkit/
folder first and executing the script with
Code:
./start.sh
I am not using linux on a regular base so i am not sure what to do else to get this solved.
I hope some one of you can help me by solving my issue.

Thanks
Sebastian
 

Zara

New Member
Yo there!
I think I know how to do that for you.
1)
Have you changed it into a exec?
Head into the DIR where your Sh script is and run:
chmod +x /home/ubuntu/nukkit/start.sh
See if you can then run:
./start.sh
Now, if you wanna run it on startup,
there is alot to explain, so here is the guide link: Script on startup

Kind regards,
Zara
 

Sebi

New Member
Hi Zara,

thank you for your answer.
The script is already executable. I have checked this with
Code:
ls -l /home/ubuntu/nukkit
.
and i am able to run the script with ./start.sh when changing the dir with cd /home/ubuntu/nukkit before.

as soon as i am trying to run the script from a diffrent dir e.g. the user home dir, the nukkit server installation routine starts as i described in my last post.

Is this a permission issue?
i have checked the permission and ownership and the user (ubuntu) has full permission and owns the folder /home/ubuntu/nukkit

I have checked your link regarding script on startup and i will give it a try but first i want to solve the execution issue.

best regards

Sebastian
 

erple2

New Member
I can tell you what I've done on my Raspberry Pi (running Raspbian, which is a Debian flavor, which Ubuntu is also based on), and it should work on any system that runs "systemd" as the startup manager. I don't know how familiar you are with Ubuntu (or Linux as a whole), though, so I'm going to make some assumptions. You have a script that you want to run (and that's about what mine looks like, more or less, too). I assume that you have a non-privileged user (ubuntu) that runs the server (that's good - don't run it as root).

Anyway, what you want to do is integrate that "script" into systemd by creating a "Unit File". Based on what you have above, your "Unit File" will look like:

Code:
[Unit]
Description=Minecraft Pocket Edition Server
After=network.target

[Service]
User=ubuntu
Group=ubuntu
Restart=on-abort
WorkingDirectory=/home/ubuntu/nukkit
ExecStart=/usr/bin/screen -dmS NukkitServer /usr/bin/java -Xms1G -Xmx1G -jar /home/ubuntu/nukkit/nukkit.jar

[Install]
WantedBy=multi-user.target
Call it whatever you want (I called it minecraftpeserver.service) then install it as normal:
copy it to /lib/systemd/system (as root)
Code:
$ sudo cp minecraftpeserver.service /lib/systemd/system
enable it with
Code:
sudo systemctl enable minecraftpeserver.service
then try to start it up with:
Code:
sudo systemctl start minecraftpeserver.service
If that works, then you should be good to go. By "enabling" the service in systemd, that means that it will try to start it up as the machine boots up.

Note that I got most of the above from https://pimylifeup.com/raspberry-pi-minecraft-pe-server/ and tweaked it a bit for my own purposes. I tried to modify it in such a way that it works with your system, too.
 

Sebi

New Member
Hi Both,

thank you for your help. I read the systemd tutorial Zara linked and created a service file (nukkit.service) with the content from erple2's post.

Now it works perfectly and i am happy ?

Sebastian
 

revpixel

New Member
To go a little farther I use a few things (SCREEN for example to leave the server headless but can attach to when needed. I also use SYSTEM.D Services as mentioned above...My change is that I additionally have the service options to reload (which sends the reload command to the server console) as well as full stop and restart scripts in my /etc/systemd/minecraft.service script.

Code:
#minecraft.service
[Unit]
Description=minecraft.revpixel.com
After=network.target

[Service]
WorkingDirectory=/mnt/data/nukkit
User=minecraft
Type=forking
ExecStart=/usr/bin/screen -dmS minecraftserver /usr/bin/java -Xms4G -Xmx4G -jar nukkit.jar
ExecStop=/usr/bin/screen -S minecraftserver -X quit
ExecStop=/usr/bin/screen -S minecraftserver -X reload
ExecReload=/usr/bin/screen -S minecraftserver -p 0 -X stuff "reload^M"
Restart=/usr/bin/screen -S minecraftserver -X quit & sleep 3 & /usr/bin/screen -dmS minecraftserver /usr/bin/java -Xms4G -Xmx4G -jar nukkit-1.0-SNAPSHOT.jar
TimeoutStartSec=0
Restart=on-failure
RestartSec=5s

[Install]
WantedBy=default.target
and' don't forget to CHMOD +X the service script. :)
 

erple2

New Member
To go a little farther I use a few things (SCREEN for example to leave the server headless but can attach to when needed. I also use SYSTEM.D Services as mentioned above...My change is that I additionally have the service options to reload (which sends the reload command to the server console) as well as full stop and restart scripts in my /etc/systemd/minecraft.service script.

Code:
#minecraft.service
...
ExecStop=/usr/bin/screen -S minecraftserver -X reload
ExecReload=/usr/bin/screen -S minecraftserver -p 0 -X stuff "reload^M"
Restart=/usr/bin/screen -S minecraftserver -X quit & sleep 3 & /usr/bin/screen -dmS minecraftserver /usr/bin/java -Xms4G -Xmx4G -jar nukkit-1.0-SNAPSHOT.jar
...
and' don't forget to CHMOD +X the service script. :)
I have a couple of comments on this:
  • On the Restart directive, you're using a single & when you probably want to be using && in your Restart command. Unless the screen program is interpreting the & directly (I don't see anything in the man page about it processing the &), otherwise, it's just backgrounding each of the processes, not executing them serially.
  • Also, why is ExecStop mapped to "screen -S minecraftserver -X reload" instead of something like "screen -S minecraftserver -X quit"?
  • in ExecReload, the "-X stuff "reload^M"" looks odd.
I suspect that your Unit file is incomplete?
 

revpixel

New Member
The ^M sends the ENTER KEY (so it types reload [ENTER] into the actual console..so it's like going into screen and typing that command. And & or && both work in this script (which is why I added the sleep command
back to the ^M (when you use the stuff option it doesn't send commands to the CMD it actually types into hence the -p 0 (position ZERO) but ya the && would work too...I may make that change....good point. (also stop is mapped to quit.... I had a copy paste error (notice it's there twice)
 

revpixel

New Member
Sorry, I was more referring to the -X stuff part. I understand what the reload^M was for.
stuff [string]
Stuff a string in the input buffer of a window.

Literally a parameter of the SCREEN term program. To ensure I'm not sending this to the Command Console but the actual Minecraft console.

 

leifis80

Member
Hello, and sorry for bringing up this old topic again but I can't find sollution anywhere else.

I have made a service script (link) following the posts up here, placed in /etc/systemd/system, enabled it and made it run. But it doesn't work.

Entering into screen says it dead so I don't know where it fails.
I'm sure it might have something to do with rights since I also need to use sudo to start the server "manually".
 
Top