Question

I have this script that runs every hour or so, and it is supposed to check to see if a minecraft server is running in a screen, and if its not, then it should start the server in the screen.

if ! screen -list | grep -q "mc"; then
        echo "starting"
        screen -dmS mc /home/mc/run_server.sh
fi

I am still new to Bash and i have been trying to debug this and have not come up with why it will not start.

Was it helpful?

Solution

It should work. What exactly doesn't start? Have you put it in a file?

#!/bin/bash
if ! screen -list | grep -q "mc"; then
    echo "starting"
    screen -dmS mc /home/mc/run_server.sh
else
    echo "minecraft server is already running!"
fi

Save it in, for example, your home directory as mc.sh file. Set executable permissions for it:

chmod +x mc.sh

And try to run it:

./mc.sh

The ! screen -list | grep -q "mc" condition works if there are no entries named "mc" found in screen -list (the command that lists all running sessions), so if there are NO "mc" screen sessions at the moment, it will show you a "starting" message and start a new screen session in which the run_server.sh will be ran.

Otherwise it will show "minecraft server is already running!" and do nothing.

If you have put it in a crontab and it doesn't work there, try to use every command with an absolute path.

0 * * * * /home/alandoyle/mc.sh

And replace every screen usage in script with a full path to screen (for example, /usr/bin/screen; you can determine your one using a which screen command)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top