I'm trying to start up a minecraft server as soon as my ec2 instance boots (it has a CloudWatch event to stop and start the instance everyday, so when it boots i need to start the server)
My server's folder sits in /home/ec2-user/minecraft/ and inside this folder i have a start-server.sh script that runs the following command:
java -Xmx1024M -Xms1024M -jar minecraft_server.1.15.2.jar noguiI can manually use screen to start the server in the background and it works just fine, i can exit my ssh session and the server is still running.
But when i try to use screen inside the start-up script the screen session doesn't start. Below are the 3 things that i've tried so far:
- Added a start-server-on-boot.sh script inside
/var/lib/cloud/scripts/per-bootwith the following command:
screen -dmS "minecraft" ~/minecraft/start-server.sh
What i was expecting to accomplish here was to start a detached screen session with the server
running. If i run this script manually from the per-boot folder it works as expected. But when it
runs during start-up it doesn't work (when i ssh into the machine there's no screen session created)
I checked the cloud-init.log for more details and the script ran without errors:
util.py[DEBUG]: Running command ['/var/lib/cloud/scripts/per-boot/start-server-on-boot.sh'] with allowed return codes [0] (shell=True, capture=False)
Added the script in the
/etc/rc.localfile. Also tested manually and it works, but when the instance boots it doesn't (no screen session created).Added a
User Datascript into my ec2 instance with the samescreencommand and it doesn't work as well. Tested running other commands and those worked (created afoo.txtfile)
I checked the cloud-init.log as well and the script ran without errors:
util.py[DEBUG]: Running command ['/var/lib/cloud/instance/scripts/userdata.txt'] with allowed return codes [0] (shell=True, capture=False)
I believe that the issue is that the screen session is terminating for some reason.
I really don't know what else can i try here and really hope i'm making a silly mistake that someone can see and i can get this thing to work (it's the last thing missing in order to get everything up and running)
1 Answer
It ended up being a permission issue. AWS EC2 start-up scripts runs as root, therefore it was not able to find the java command neither the minecraft folder, because those were accessible only from the ec2-user (which was the one that i ssh'ed with)
So the solution was to change the command from the start-server-on-boot.sh script that sits inside /var/lib/cloud/scripts/per-boot to the following:
runuser -l ec2-user -c 'screen -dmS "minecraft" ~/minecraft/start_server.sh'
That way it was able to find both the minecraft folder and the java command and the screen session got created with the server up