Stop script | Spigot | Reatach to a detached screen - linux

I am trying to make a minecraft server for a while and I have a little problem with a stop script. My startup script ( i want to mention in , just in case of something ) looks like this:
#!/bin/sh
screen -S server java -Xms2G -Xmx2G -XX:+UseConcMarkSweepGC -jar spigot.jar
And my stop script:
#!/bin/sh
screen -r server stop
Now , the problem is that this script is just accessing the screen without executing that command. I don't want to access the screen then type the "stop" command to stop the server (I mean I don't want to do that myself ) . I want a script which can reattach to my detached screen and execute that command for me. Can you find a solution, please? Oh yeah , and I know it's not important but I want to mention that I am using Spigot 1.12.2

Maybe you can run the command server stop in another script:
#!/bin/sh
screen -r ./Script.sh
------------------------------------
#./Sript.sh
#!/bin/sh
server stop

Related

Minecraft Linux Server | Start.sh problem

I have a Minecraft Server running on Linux.
I use to start the server, a start.sh file with following content:
(This content starts a screen session and the minecraft server)
screen -S {ScreenSession} java -Xmx2G -Xms2G -jar spigot-1.18.1.jar
If I use /restart ingame, the screen session will end and the server won't start. So I have to go into the Linux Server and start the Minecraft Server again.
My question:
How can I make it so, if I use /restart that the server will restart with a active screen session.
If have tried many things.
I hope someone can help me,
~Kitty Cat Craft
There is multiple way to achieve what you want.
If you have lot of servers, you can use a quick bash script with an auto restart like that:
#!/bin/sh
while true
do
java -Xmx2G -Xms2G -jar spigot-1.18.1.jar --nogui
sleep 5
done
When you will stop, it will wait 5 seconds then restart.
With this, you can use: screen -dmS <screenName> sh myScript.sh which will run the script into another screen. It's usefull when you run it from a script which run lot of server, like that:
screen -dmS srv1 sh srv1.sh
screen -dmS srv2 sh srv2.sh
screen -dmS srv3 sh srv3.sh
You can also, if you have only one server, just firstly use screen -S screenName. Then, when you are in the screen, run the script that restart automatically (the script that I gave at first).
Also, prefer use /stop than /restart, because spigot will try to find the script. And if it success, it will run a second time the same script, and so will have ghost process.

Send command to a nohup process?

I have a minecraft server that when I run it, takes the console and can receive commands/parameters.
I'm running it with nohup java -Xms.... -jar spigot.jar &. It will stay in the background with PID XXXX and port YYYY.
I want know if it is possible to send commands to it as /help.
Regards and thanks for the help.
Instead of running your server in the background with nohup java -Xms.... -jar spigot.jar & you could use a terminal multiplexer like screen.
See https://ss64.com/bash/screen.html
or https://www.gnu.org/software/screen/manual/screen.html
For interactive start, run screen first, then inside the screen session run java -Xms.... -jar spigot.jar (in the foreground, without nohup or &).
Then you can use screen's escape sequence CTRL+a d to detach from the session. Your server will continue to run.
If you later want to interact with the server, use screen -r. This will reattach your terminal to the session.
Type /help or whatever you need to do.
When you are done, detach from the session again.
You could also use screen -d -m java -Xms.... -jar spigot.jar to create a detached session with your command, e.g. in a startup script.
screen has a lot more capabilities. Read the documentation.
I can solved this by tripleee.
This works "nohup tail -f /usr/server/console.in | nohup java -Xms.... -jar spigot.jar >> /usr/server/console.out &"
Whit"echo command >> /usr/server/console.in"
Is as i run the command in the server.

Script runs fully when ran manually but misses commands when ran from another script

I have 2 scripts that I'm testing to automate starting services on my server however they behave weirdly.
The first script is
#!/bin/sh
screen -dmS Test_Screen
sleep 1
sudo sh cd.sh
echo "finished"
Which runs perfectly however the script it runs does not and is as follows
#!/bin/sh
screen -S Test_Screen -X stuff "cd /home/Test"
sleep 1
screen -S Test_Screen -X eval "stuff \015"
sleep 1
echo "Complete"
The second script will run perfect if I run it from command line and will CD into the directory within the screen. However, if it runs from the first script it Will Not CD into the correct directory within the screen, but it will still print "Complete".
I'm Using CENTOS 6.7 and the latest version of GNU screen
Any Ideas?
This seems to be a problem with session nesting.
In your first script you create a session named Test_Screen.
In your second script the -S parameter tells screen to create a session of the same name. This might cause screen to exit and not cd into the correct directory.
You could move the cd command in front of the sudo sh cd.sh and remove those screen calls from the second script leaving only
stuff \015
echo "Complete"
Using the correct screenflags should also work.
#!/bin/sh
screen -dr Test_Screen -X stuff "cd /home/Test"
sleep 1
screen -dr Test_Screen -X eval "stuff \015"
sleep 1
echo "Complete"
For a more modern alternative to screen, have a look at tmux.
Ok so this turned out really weird. After posting i tried a couple of things on a centos 6.7 hyper V test environment and got the exact same issue. However, later in the day we ended up changing service provider and upgrading to centos 7 in the process. I am not sure why but since the update the script now runs perfectly and i was able to actually merge the two scripts into one in order to make it more efficient. If anyone knows why the update fixed it feel free to let me know.

Using screen in bash script

I'm running a game server on a remote server where I use a detached screen instance to leave it running.
I'm now creating a script that can be used to shut down the server, back up all the vital files and start it up again, however I'm having a few difficulties with dealing with the screen.
I assumed that I could just switch into the detached screen in the script (after the server had already been shut down) by calling screen -r in the script.
But that doesn't seem to work because if I run the script from outside screen it just launches the server in that session.
screen -r
cd ~/servers/StarMade/
sh StarMade-dedicated-server-linux.sh
screen -d
This is what I thought would do the trick but it doesn't. Maybe somebody can help me out here. I'm not a bash expert. In fact this is propably my first bash script that doesn't include "Hello World". Thanks.
Your script, as in your example, will get executed by your sell, not the one in the screen. You need to tell the running screen to read a file and execute it - that's what the -X option is for.
Try
tempfile=$(mktemp)
cat > $tempfile <<EOF
cd ~/servers/StarMade/
sh StarMade-dedicated-server-linux.sh
EOF
screen -X readbuf $tempfile
screen -X paste .
rm -f $tempfile
You can leave screen running in a 2nd terminal session to see what happens.

Run a script after killing lxsession (xorg)

I am trying to run a program automatically within a bash script after killing the LXDE session. My script consists of:
#!/bin/sh
pkill lxsession;
sh /home/pi/RetroPie/EmulationStation/emulationstation
I tried this as well:
#!/bin/sh
nohup & pkill lxsession &
writevt /dev/tty1 'emulationstation'
My aim is to log out of the LXDE session and run EmulationStation on my Raspberry Pi with a bash script. I'm using pkill lxsession; to bypass lxsession's logout confirmation dialog.
As it stands, this script just gets me to the command line from a working LXDE desktop. Thanks for reading.
Dont EmulationStation need some sort of X server running in the background for it to work?
IF not, then try the following:
#!/bin/sh
pkill lxsession;
sleep 5
su -c sh /home/pi/RetroPie/EmulationStation/emulationstation
exit
It could also be that when you log out of your lxde session the emulationstation dosent have a usershell to open it, therefore "su -c"
I'm not sure if its going to work but I hope you solve it. :)

Resources