Writing a Startscript

I hope I have found the perfect place for my problem ...

I have a dedicated server with some minecraftservers (java) which are connected via a Bungeecord on it.
These all have a start.sh which looks in its folder like this:

screen -A -m -d -S SERVERNAME java -jar -Xmx2048M -Xms2048M spigot.jar

The stop.sh looks like this:

screen -S SERVERNAME -X quit

I would like to combine the scripts, that you must perform only one file to start/stop the servernetwork...
It would be perfect if there was skript, which allows to start/stop the servers seperately AND together.... But I have no idea if such a programm is even possible :smiley: In another Forum they said i wouldn't be that hard to wirte something like that. They said I'd have to write a Start/Stop/Status script for the servers seperately and combine them in another script. Would be really nice if someone would help :slight_smile: The gameservers are running a screen when they are online. It would be brilliant if I could check the Status of the servers. (The script has to check if there is a screen online with the name "XY")

I look forward to your answers :slight_smile:

Yours sincerely
Spongebob

PS: Sorry for my bad english :slight_smile:

I tried really hard with this one, but the status isn't working that fine and i can't connect the scripts:

#!/bin/bash
INSTANZ="Bungeecord"
BPDIR=/home/bungeecord
Pidfile=/home/Steuerung/bungee.pid

if [ -f $Pidfile ]
then
	Pid=`cat $Pidfile`
fi

case "$1" in
'start')
		if [ -f $Pidfile ] ; then
				if test `ps -e | grep -c $Pid` = 1; then
						echo "Not starting $INSTANZ - instance already running with PID: $Pid"
				else
						echo "Starting $INSTANZ"
						cd $BPDIR
						nohup ./start.sh &> /dev/null &
						echo $! > $Pidfile
				fi
		else
				echo "Starting $INSTANZ"
				cd $BPDIR
				nohup ./bsp.sh &> /dev/null &
				echo $! > $Pidfile
		fi
		;;

'stop')
		if [ -f $Pidfile ] ; then
				echo "stopping $INSTANZ"
					cd $BPDIR
						nohup ./stop.sh &> /dev/null &
						echo $! > $Pidfile
		else
				echo "Cannot stop $INSTANZ - no Pidfile found!"
		fi
		;;

'restart')
		$0 stop
		sleep 30
		$0 start
		;;

'status')
		if [ -f $Pidfile ] ; then
				if test `ps -e | grep -c $Pid` = 0; then
						echo "$INSTANZ not running"
				else
						echo "$INSTANZ running with PID: [$Pid]"
				fi
		else
				echo "$Pidfile does not exist! Cannot process $INSTANZ status!"
				exit 1
		fi
		;;

*)
		echo "usage: $0 { start | stop | restart | status }"
		;;

esac
exit 0

RE: status
If you are going to test Pidfile for zero lines,

if test `ps -e | grep -c $Pid` = 0; then

... try

> $Pidfile  # instead of echo $! > $Pidfile

when you 'stop' or initialize $Pidfile

1 Like

Great, the Status-Test is working now. But do you know if it is possible to test if there is a screen on with a specified name instead? And how to combine these scripts into one?

Screen is a pretty complex monster. There is a command to list sessions...

screen -ls
screen --list

You might be able to incorporate that output into your scripts.
See man screen

1 Like

I know this command. But I don't know how to check if there is a screen with a specified name in this list... Do you know?

And do you know how to combine my startscripts? It's more important to me to start all servers with one command :slight_smile:

Sorry. :stuck_out_tongue:
I don't know enough about screen or servers to help you there. I'm sure someone will come along who does.

1 Like

Still a huge Thank you :slight_smile: You helped me out very good, now I have a running script, but not the way I want it to be :slight_smile: Hopefully someone else will know how to do :smiley:

The test can fail for small numbers.
Improvements are

if test `ps -e | grep -wc $Pid` = 0; then
if ps -p $Pid >/dev/null 2>&1; then :
else

Is not working. When I try to start the script, it says "Not starting Bungeecord - instance already running with PID: 5242" but when I type status, it says "Bungeecord not running".... Very confusing

So - what be the output of ps -e , and what's in $PIDFILE?

My examples were for the status section.
Simply add -w for each Pid grep!