I’m currently running an own Minecraft Server, and I wanted to build an option to restart the Minecraft server via PHP, so my friends can restart the MC server without needing access to my actual server.
Thus, first I wanted to create a script to start the server:
startup.php
<?php chdir('/home/minecraft/minecraft_server/'); exec("./start"); ?>
start
screen -mdS "minecraft_up" java -Xmx4G -Xms4G -jar server.jar nogui
After a bit of permission pain etc, by calling the webadress the server can be started.
Now, my problem is that I have no access to the screen process.
When running ps -ef
I get:
UID PID PPID C TTY TIME CMD www-data 29642 1 0 ? 00:00:00 SCREEN -mdS minecraft_up jav www-data 29643 29642 99 pts/5 00:02:07 java -Xmx4G -Xms4G -jar serv
When running sudo pwdx 29642
to get the process location I get:
29642: /home/minecraft/minecraft_server
But when I go into the /home/minecraft/minecraft_server
directory and run either screen -ls
or sudo screen -ls
, both times I get:
No Sockets found in /run/screen/S-minecraft.
In contrast: When executing the start file manually in the terminal, when running screen -ls
I can actually see the process (or open it with screen -r
):
There is a screen on: 29025.minecraft_up (01/24/2021 01:32:45 PM) (Detached) 1 Socket in /run/screen/S-minecraft.
I also get the same working directory, but I cannot run the start file manually while the process started py PHP is still running.
I’m really not an expert at all at Linux, I guess theres some user permission interference or something that PHP is treated as an own user, I really don’t know, which is why I’m asking.
How do I access this process started by PHP?
Advertisement
Answer
To create a service using systemd
:
Create a file in /etc/systemd/system/
called minecraft-server.service
with the following content:
[Unit] Description=Minecraft server After=network.target StartLimitIntervalSec=0 [Service] Type=simple Restart=always RestartSec=1 User=minecraft Group=appmgr ExecStart=java -Xmx4G -Xms4G -jar server.jar [Install] WantedBy=multi-user.target
Replace server.jar
with full path.
Now lets create the new group and user.
To create new group run the following command:
sudo groupadd appmgr
Now lets create the new user and it it to a group.
sudo useradd –G appmgr minecraft
Now your service is created, we need to enable it and run it for first time.
systemctl enable minecraft-server systemctl start minecraft-server
Last step to write php script to restart the services:
<?php exec("systemctl restart minecraft-server"); ?>