- Joined
- Aug 3, 2013
- Messages
- 7
- Reaction score
- 0
I needed a way to start my server without human interaction when the server restarts (or w/e happens) and a easier way to restart and issue commands (since the console goes nuts with notifications and you can't even execute commands via the console) This script is based on my modified version of the MineCraft startup script I had released some years ago. Feel free to use it!
#!/bin/bash
# /etc/init.d/starmade
# version 0.0.1 2013-08-04 (YYYY-MM-DD)
### BEGIN INIT INFO
# Provides: starmade
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
# Should-Start: $network
# Should-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: StarMade Dedicated server
# Description: Starts the StarMade Dedicated Server
### END INIT INFO
#Settings
SERVICE='StarMade.jar'
USERNAME='root'
SCREEN='starmade'
SMPATH='/home/starmade/server/StarMade'
MAXHEAP=1024
MINHEAP=128
INVOCATION="java -Xms${MINHEAP}m -Xmx${MAXHEAP}m -jar $SERVICE -server -nogui"
# Text color variables
txtund=$(tput sgr 0 1) # Underline
txtbld=$(tput bold) # Bold
bldred=${txtbld}$(tput setaf 1) # red
bldblu=${txtbld}$(tput setaf 4) # blue
bldgre=${txtbld}$(tput setaf 2) # green
bldwht=${txtbld}$(tput setaf 7) # white
txtrst=$(tput sgr0) # Reset
info=${bldwht}*${txtrst} # Feedback
pass=${bldblu}*${txtrst}
warn=${bldred}*${txtrst}
ques=${bldblu}?${txtrst}
ME=`whoami`
as_user() {
if [ $ME == $USERNAME ] ; then
bash -c "$1"
else
su - $USERNAME -c "$1"
fi
}
sm_start() {
if pgrep -u $USERNAME -f $SERVICE > /dev/null
then
echo "$SERVICE is already running!"
else
echo "Starting $SERVICE..."
as_user "cd $SMPATH && screen -S $SCREEN -A -m -d $INVOCATION"
sleep 12
if pgrep -u $USERNAME -f $SERVICE > /dev/null
then
echo "${bldgre}$SERVICE is now running.${txtrst}"
else
echo "${bldred}Error! Could not start $SERVICE!${txtrst}"
fi
fi
}
sm_stop() {
if pgrep -u $USERNAME -f $SERVICE > /dev/null
then
echo "Stopping $SERVICE"
as_user "screen -p 0 -S $SCREEN -X eval 'stuff \"/say Shutting down in 10 seconds...\"\015'"
sleep 1
as_user "screen -p 0 -S $SCREEN -X eval 'stuff \"/shutdown 9\"\015'"
sleep 12
else
echo "$SERVICE was not running."
fi
if pgrep -u $USERNAME -f $SERVICE > /dev/null
then
echo "${bldred}Error! $SERVICE could not be stopped.${txtrst}"
else
echo "${bldred}$SERVICE is stopped.${txtrst}"
fi
}
sm_restart() {
if pgrep -u $USERNAME -f $SERVICE > /dev/null
then
echo "Stopping $SERVICE"
as_user "screen -p 0 -S $SCREEN -X eval 'stuff \"/say Server restarting. Log back in 30 seconds. Saving map...\"\015'"
sleep 10
as_user "screen -p 0 -S $SCREEN -X eval 'stuff \"/shutdown 1\"\015'"
sleep 12
if pgrep -u $USERNAME -f $SERVICE > /dev/null
then
echo "${bldred}${SERVICE} could not be stopped!${txtrst}"
else
echo "Starting $SERVICE..."
cd $SMPATH
as_user "cd $SMPATH && screen -S $SCREEN -A -m -d $INVOCATION"
sleep 12
if pgrep -u $USERNAME -f $SERVICE > /dev/null
then
echo "${bldgre}$SERVICE is now running.${txtrst}"
else
echo "${bldred}Error! Could not start $SERVICE!${txtrst}"
fi
fi
else
echo "$SERVICE was not running."
fi
}
sm_command() {
command="$1";
if pgrep -u $USERNAME -f $SERVICE > /dev/null
then
echo "$SERVICE is running... executing command"
as_user "screen -p 0 -S $SCREEN -X eval 'stuff \"/$command\"\015'"
fi
}
# Command Reference
case "$1" in
start)
sm_start
;;
stop)
sm_stop
;;
restart)
sm_restart
;;
status)
if pgrep -u $USERNAME -f $SERVICE > /dev/null
then
echo "${bldgre}${SERVICE} is running.${txtrst}"
else
echo "${bldred}${SERVICE} is not running.${txtrst}"
fi
;;
command)
if [ $# -gt 1 ]; then
shift
sm_command "$*"
else
echo "Must specify server command (try 'help'?)"
fi
;;
help)
echo "Usage: $0 {start|stop|status|restart|command \"server command\"}"
exit 1
;;
*)
echo "Usage: $0 {start|stop|status|restart|command \"server command\"}"
exit 1
;;
esac
exit 0
Installation
Firstly, you will need screen installed. If you do not have it, run apt-get install screen
CD into /etc/init.d/ and create a new file called "starmade" with nano or your favorite editor. Paste in the script above and make the changes to the paths as needed. Save the script.
Next give the script proper permissions with chmod a+x /etc/init.d/starmade
Next update your serice with update-rc.d /etc/init.d/starmade defaults
Usage
You server should start with your linux machine upon it's next reboot. To start the server manually run service starmade start
Additionally run service starmade help for a list of commands.
#!/bin/bash
# /etc/init.d/starmade
# version 0.0.1 2013-08-04 (YYYY-MM-DD)
### BEGIN INIT INFO
# Provides: starmade
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
# Should-Start: $network
# Should-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: StarMade Dedicated server
# Description: Starts the StarMade Dedicated Server
### END INIT INFO
#Settings
SERVICE='StarMade.jar'
USERNAME='root'
SCREEN='starmade'
SMPATH='/home/starmade/server/StarMade'
MAXHEAP=1024
MINHEAP=128
INVOCATION="java -Xms${MINHEAP}m -Xmx${MAXHEAP}m -jar $SERVICE -server -nogui"
# Text color variables
txtund=$(tput sgr 0 1) # Underline
txtbld=$(tput bold) # Bold
bldred=${txtbld}$(tput setaf 1) # red
bldblu=${txtbld}$(tput setaf 4) # blue
bldgre=${txtbld}$(tput setaf 2) # green
bldwht=${txtbld}$(tput setaf 7) # white
txtrst=$(tput sgr0) # Reset
info=${bldwht}*${txtrst} # Feedback
pass=${bldblu}*${txtrst}
warn=${bldred}*${txtrst}
ques=${bldblu}?${txtrst}
ME=`whoami`
as_user() {
if [ $ME == $USERNAME ] ; then
bash -c "$1"
else
su - $USERNAME -c "$1"
fi
}
sm_start() {
if pgrep -u $USERNAME -f $SERVICE > /dev/null
then
echo "$SERVICE is already running!"
else
echo "Starting $SERVICE..."
as_user "cd $SMPATH && screen -S $SCREEN -A -m -d $INVOCATION"
sleep 12
if pgrep -u $USERNAME -f $SERVICE > /dev/null
then
echo "${bldgre}$SERVICE is now running.${txtrst}"
else
echo "${bldred}Error! Could not start $SERVICE!${txtrst}"
fi
fi
}
sm_stop() {
if pgrep -u $USERNAME -f $SERVICE > /dev/null
then
echo "Stopping $SERVICE"
as_user "screen -p 0 -S $SCREEN -X eval 'stuff \"/say Shutting down in 10 seconds...\"\015'"
sleep 1
as_user "screen -p 0 -S $SCREEN -X eval 'stuff \"/shutdown 9\"\015'"
sleep 12
else
echo "$SERVICE was not running."
fi
if pgrep -u $USERNAME -f $SERVICE > /dev/null
then
echo "${bldred}Error! $SERVICE could not be stopped.${txtrst}"
else
echo "${bldred}$SERVICE is stopped.${txtrst}"
fi
}
sm_restart() {
if pgrep -u $USERNAME -f $SERVICE > /dev/null
then
echo "Stopping $SERVICE"
as_user "screen -p 0 -S $SCREEN -X eval 'stuff \"/say Server restarting. Log back in 30 seconds. Saving map...\"\015'"
sleep 10
as_user "screen -p 0 -S $SCREEN -X eval 'stuff \"/shutdown 1\"\015'"
sleep 12
if pgrep -u $USERNAME -f $SERVICE > /dev/null
then
echo "${bldred}${SERVICE} could not be stopped!${txtrst}"
else
echo "Starting $SERVICE..."
cd $SMPATH
as_user "cd $SMPATH && screen -S $SCREEN -A -m -d $INVOCATION"
sleep 12
if pgrep -u $USERNAME -f $SERVICE > /dev/null
then
echo "${bldgre}$SERVICE is now running.${txtrst}"
else
echo "${bldred}Error! Could not start $SERVICE!${txtrst}"
fi
fi
else
echo "$SERVICE was not running."
fi
}
sm_command() {
command="$1";
if pgrep -u $USERNAME -f $SERVICE > /dev/null
then
echo "$SERVICE is running... executing command"
as_user "screen -p 0 -S $SCREEN -X eval 'stuff \"/$command\"\015'"
fi
}
# Command Reference
case "$1" in
start)
sm_start
;;
stop)
sm_stop
;;
restart)
sm_restart
;;
status)
if pgrep -u $USERNAME -f $SERVICE > /dev/null
then
echo "${bldgre}${SERVICE} is running.${txtrst}"
else
echo "${bldred}${SERVICE} is not running.${txtrst}"
fi
;;
command)
if [ $# -gt 1 ]; then
shift
sm_command "$*"
else
echo "Must specify server command (try 'help'?)"
fi
;;
help)
echo "Usage: $0 {start|stop|status|restart|command \"server command\"}"
exit 1
;;
*)
echo "Usage: $0 {start|stop|status|restart|command \"server command\"}"
exit 1
;;
esac
exit 0
Installation
Firstly, you will need screen installed. If you do not have it, run apt-get install screen
CD into /etc/init.d/ and create a new file called "starmade" with nano or your favorite editor. Paste in the script above and make the changes to the paths as needed. Save the script.
Next give the script proper permissions with chmod a+x /etc/init.d/starmade
Next update your serice with update-rc.d /etc/init.d/starmade defaults
Usage
You server should start with your linux machine upon it's next reboot. To start the server manually run service starmade start
Additionally run service starmade help for a list of commands.