117
117

Reputation: 197

Create screen for restart script? bash

I'm trying to use this script on an Ubuntu server.

#!/bin/sh
while (true)
do
    screen -S myserver java -server -Xms4G -Xmx4G -jar craftbukkit.jar
    echo "Server restarting in 5 seconds. Ctr+C to kill the server!"
    echo "Rebooting in:"
    for i in 5 4 3 2 1
    do
        echo "$i..."
        sleep 1
    done
    echo "Rebooting Server!"
done

However as soon as i run the script and detach from the screen, it starts the reboot process. How can I only start the reboot once the screen is closed?

Upvotes: 1

Views: 486

Answers (1)

Barmar
Barmar

Reputation: 782130

Test whether the screen session is still running:

#!/bin/sh
while :
do
    screen -S myserver java -server -Xms4G -Xmx4G -jar craftbukkit.jar
    if screen -ls myserver | grep -q "No Sockets found"
    then 
        echo "Server restarting in 5 seconds. Ctr+C to kill the server!"
        echo "Rebooting in:"
        for i in 5 4 3 2 1
        do
            echo "$i..."
            sleep 1
        done
        echo "Rebooting Server!"
    else 
        echo "Not rebooting yet"
    fi
done

But since this is in an infinite loop, if you detach the screen and it doesn't reboot, it will start up another screen session. I'm not sure why you put it in a loop.

Upvotes: 1

Related Questions