0
votes

I'm new in raspberry pi programming, and i want to be able to launch a minecraft server at the start of the pi.

For that, I've already loocked at Systemd files and screen command.

I manage to make them work separately, but not together, it's why I'm looking for help there.

Firstly, I'm using a Raspberry pi 4 4Go with raspbian v10, and forge 1.12.2 with java 8. I did a .sh file to launch easier the server:

#!/bin/bash
screen -S mcserver -dm java -Xms1024M -Xmx2048M -jar /home/pi/MinecraftServer/server/forge-1.12.2-14.23.5.2854.jar nogui

When I run the file, the server start perfectly in a socket as I want.

Secondly, I have a systemd file (auto-run-server.service):

[Unit]
Description=Auto run mc server
[Service]
ExecStart=/home/pi/MinecraftServer/server/minecraft.sh
[Install]
WantedBy=multi-user.target

But when I execute the service, nothing is happening, the status of the service shows a sucess, but there is nothing in screens (screen -list)

And when i replace the ExecStart value by

ExecStart=java -Xms1024M -Xmx2048M -jar /home/pi/MinecraftServer/server/forge-1.12.2-14.23.5.2854.jar nogui

The server starts, but the problem is that I want to access to a terminal to run commands in minecraft server, and i didn't find solution to access from there. ( It's why I want to create a "screen" )

I'm fully open to your answers, even if they don't use "screen", as long as I can access to a server terminal.

Thanks in advance.

1
Have you tried running screen from the same account that systemd uses ?Marged
Thanks for your comment @Marged, but I've only one account on the pi, the one by defaults.Fabien Denoyelle
You only use root ?Marged

1 Answers

2
votes

I'm using the follow systemd unit for testing:

[Service]
ExecStart=/tmp/screentest.sh

And this screentest.sh shell script:

#!/bin/sh

screen -S mcserver -dm sh -c 'while :; do date; sleep 5; done'

If I start the service (systemctl start screentest) and then run systemctl status screentest, I see:

● screentest.service
   Loaded: loaded (/etc/systemd/system/screentest.service; static; vendor preset: enabled)
   Active: inactive (dead)

The problem here is that the screen command exits immediately when running with -d, so systemd believes the command has completed and cleans everything up by removing any additional processes spawned by the service.

We can tell systemd that the service spawns a child and exits by setting the service type to forking:

[Service]
Type=forking
ExecStart=/tmp/screentest.sh

With this change in place, after starting the service we see:

● screentest.service
   Loaded: loaded (/etc/systemd/system/screentest.service; static; vendor preset: enabled)
   Active: active (running) since Sun 2021-01-10 09:58:11 EST; 4s ago
  Process: 14461 ExecStart=/tmp/screentest.sh (code=exited, status=0/SUCCESS)
 Main PID: 14463 (screen)
    Tasks: 3 (limit: 4915)
   CGroup: /system.slice/screentest.service
           ├─14463 SCREEN -S mcserver -dm sh -c while :; do date; sleep 5; done
           ├─14464 sh -c while :; do date; sleep 5; done
           └─14466 sleep 5

And screen -list shows:

root@raspberrypi:/etc/systemd/system# screen -list
There is a screen on:
        14612.mcserver  (01/10/2021 10:01:55 AM)        (Detached)
1 Socket in /run/screen/S-root.