Startup script for GNU screen

Here’s how you start up a GNU screen session at startup and give it a command to run.

#!/bin/bash
cd /home/minesrv/minecraft_server/
screen -dmS minesrv java -server -Xincgc -Xmx3G -jar minecraft_server.jar nogui

So in this case we’re starting a minecraft server in a screen session.

Per the man page -dm does the following:

-d -m   Start screen in "detached" mode. This creates a new session but
               doesn't  attach  to  it.  This  is  useful  for  system startup
               scripts.

the -S gives the screen session a name so it’s easier to find later on.

Then it runs the command, java, with its parameters.

———————————————————————————

Another useful tool is sudo, so we can run our above startup script as a different user than root.

#!/bin/bash
sudo -u minesrv -i /home/minesrv/scripts/startMinecraft

startMinecraft is the first bash script above. Here we’re using sudo to run it as the user ‘minesrv’.

The -i makes sudo perform all the standard login processes before running the script.

—————————————————————-

And finally, you can just reference the startup script inside /etc/rc.local for most linux distros. That will make the script run at startup.

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

/home/minesrv/scripts/startAtBoottimeMinecraft
exit 0

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *