Skip to content

Instantly share code, notes, and snippets.

@Techcable
Created June 20, 2015 04:10
Show Gist options
  • Select an option

  • Save Techcable/770892f8974d0b03ef4c to your computer and use it in GitHub Desktop.

Select an option

Save Techcable/770892f8974d0b03ef4c to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# /etc/init.d/techbot
#
# techbot Manages techbot
#
### BEGIN INIT INFO
# Provides: techbot
# Required-Start: $local_fs $remote_fs screen-cleanup
# 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: TechBot
# Description: Manages TechBot
### END INIT INFO
#
### SETTINGS
#
SERVICE="TechBot"
SERVICE_JAR="${SERVICE}.jar"
# Linux Username
USERNAME='nicholas'
# Root Folder
SERVICE_PATH="/home/${USERNAME}/${SERVICE}"
# RAM to Allocate
RAM='30M'
# Server Java Command
INVOCATION="java -server -Xmx${RAM} -Xms5M -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=256M -jar $SERVICE_JAR > latest.log"
ME=`whoami`
as_user() {
if [ $ME == $USERNAME ] ; then
bash -c "$1"
else
su - $USERNAME -c "$1"
fi
}
bot_start() {
if pgrep -u $USERNAME -f $SERVICE_JAR > /dev/null
then
echo "$SERVICE is already running."
else
echo "Starting $SERVICE..."
cd $SERVICE_PATH
as_user "cd $SERVICE_PATH && screen -dmS $SERVICE $INVOCATION"
# Wait 5 seconds to start up
sleep 5
if pgrep -u $USERNAME -f $SERVICE_JAR > /dev/null
then
# Print output
tail -n $[`wc -l "$SERVICE_PATH/latest.log" | awk '{print $1}'`] "$SERVICE_PATH/latest.log"
else
echo "Could not start $SERVICE."
fi
fi
}
bot_stop() {
if pgrep -u $USERNAME -f $SERVICE_JAR > /dev/null
then
pre_log_len=`wc -l "$SERVICE_PATH/latest.log" | awk '{print $1}'`
echo "$SERVICE is running. Stopping server..."
as_user "screen -p 0 -S $SERVICE -X eval 'stuff \"!stop\"\015'"
# Wait 15 seconds to finish stopping
sleep 15
# Print output
tail -n $[`wc -l "$SERVICE_PATH/latest.log" | awk '{print $1}'`-$pre_log_len] "$SERVICE_PATH/latest.log"
else
echo "$SERVICE is not running..."
fi
}
bot_command() {
command="$1";
if [ "$command" == "" ]
then
usage
return
fi
if pgrep -u $USERNAME -f $SERVICE_JAR > /dev/null
then
pre_log_len=`wc -l "$SERVICE_PATH/latest.log" | awk '{print $1}'`
echo "$SERVICE is running. Executing $command..."
as_user "screen -p 0 -S $SERVICE -X eval 'stuff \"$command\"\015'"
# Wait 3 seconds for the command to run and print to the log file
sleep 3
# Print output
tail -n $[`wc -l "$SERVICE_PATH/latest.log" | awk '{print $1}'`-$pre_log_len] "$SERVICE_PATH/latest.log"
else
echo "$SERVICE is not running..."
fi
}
usage() {
echo "Usage: bot [ start | stop | restart | status | run \"command\" ]"
}
#Start-Stop here
case "$1" in
start)
bot_start
;;
stop)
bot_stop
;;
restart)
bot_stop
bot_start
;;
status)
if pgrep -u $USERNAME -f $SERVICE_JAR > /dev/null
then
echo "$SERVICE is running."
else
echo "$SERVICE is not running."
fi
;;
run)
bot_command "$*"
;;
*)
usage
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment