Skip to content

Instantly share code, notes, and snippets.

@FilBot3
Created September 13, 2014 15:25
Show Gist options
  • Save FilBot3/74b6a8c2e3d64d64329a to your computer and use it in GitHub Desktop.
Save FilBot3/74b6a8c2e3d64d64329a to your computer and use it in GitHub Desktop.
MegaMek init script issue - Related to: https://github.com/predatorian3/megamek-install
[root@fenix-centos ~]# service megamek start
+ DAEMON=/opt/megamek-0.38.0/MegaMek.jar
+ DAEMONPATH=/usr/bin/java
+ DAEMONPATHARGS='-jar -Xmx768'
+ DAEMONARGS='-dedicated -port 2346'
+ NAME=megamek
+ PIDFILE=/var/run/megamek.pid
+ SCRIPTNAME=/etc/init.d/megamek
+ USER=megamek
+ case "$1" in
+ start
+ '[' '!' -e /usr/bin/java ']'
+ '[' '!' -e /opt/megamek-0.38.0/MegaMek.jar ']'
+ echo 'Starting megamek...'
Starting megamek...
++ su - megamek -c '/usr/bin/java -jar -Xmx768 /opt/megamek-0.38.0/MegaMek.jar -dedicated -port 2346 > /dev/null 2>&1 & echo $! '
+ PID=2462
+ '[' -z 2462 ']'
+ echo 2462
+ printf '%s\n' 'Ok, PIDFILE is written'
Ok, PIDFILE is written
[root@fenix-centos ~]# ps aux | grep megamek
root 2310 0.0 0.0 11092 3648 pts/0 S+ 10:12 0:00 vim /etc/init.d/megamek
root 2465 0.0 0.0 4356 748 pts/1 S+ 10:13 0:00 grep megamek
[root@fenix-centos ~]# service megamek start
+ DAEMON=/opt/megamek-0.38.0/MegaMek.jar
+ DAEMONPATH=/usr/bin/java
+ DAEMONPATHARGS='-jar -Xmx768'
+ DAEMONARGS='-dedicated -port 2346'
+ NAME=megamek
+ PIDFILE=/var/run/megamek.pid
+ SCRIPTNAME=/etc/init.d/megamek
+ USER=megamek
+ case "$1" in
+ start
+ '[' '!' -e /usr/bin/java ']'
+ '[' '!' -e /opt/megamek-0.38.0/MegaMek.jar ']'
+ echo 'Starting megamek...'
Starting megamek...
++ su - megamek -c '$DAEMONPATH $DAEMONPATHARGS $DAEMON $DAEMONARGS > /dev/null 2>&1 & echo $! '
+ PID=2558
+ '[' -z 2558 ']'
+ echo 2558
+ printf '%s\n' 'Ok, PIDFILE is written'
Ok, PIDFILE is written
#!/bin/bash
# chkconfig: 2345 99 01
# description: AppDynamics Machine Agent init script
#
set -x
# This is the name, or path to the executable
DAEMON="/opt/megamek-0.38.0/MegaMek.jar"
# This is the path to any precursor programs, for instance Java, or Bash
DAEMONPATH="/usr/bin/java"
# Daemon Path Args
DAEMONPATHARGS="-jar -Xmx768"
#
DAEMONARGS="-dedicated -port 2346"
# This is used to create the PID file and lock file
NAME="megamek"
# This is the location of the PID file
PIDFILE=/var/run/$NAME.pid
# This is the location of the init script
SCRIPTNAME=/etc/init.d/$NAME
# The user who will execute this script
USER="megamek"
start()
{
# Check for the existance of the Daemon
if [ ! -e $DAEMONPATH ]
then
echo "The file $DAEMONPATH does not exist."
exit 2
fi
# Check for the existance of the Daemon executable
if [ ! -e $DAEMON ]
then
echo "The file $DAEMON does not exist."
exit 2
fi
echo "Starting $NAME..."
# execute the command, and retrive the PID of the command
PID=`su - $USER -c '$DAEMONPATH $DAEMONPATHARGS $DAEMON $DAEMONARGS > /dev/null 2>&1 & echo $! '`
#echo "Saving PID" $PID " to " $PIDFILE
if [ -z $PID ]
then
# If the PID is not available, the program failed to start
printf "%s\n" "Failed to start"
exit 3
else
# If the PID is available, the program started.
echo $PID > $PIDFILE
printf "%s\n" "Ok, PIDFILE is written"
fi
}
stop()
{
# Kill them all
echo "Stopping $NAME..."
# Retrieve the PID from the PIDFILE
PID=`cat $PIDFILE`
if [ -f $PIDFILE ]
then
kill -HUP $PID
echo "$NAME has stopped"
rm -f $PIDFILE
else
echo "$PIDFILE not found"
fi
}
status()
{
# Show the running or stopped status of the process
echo "Checking $NAME..."
if [ -f $PIDFILE ]
then
PID=`cat $PIDFILE`
if [ -z "`ps aux | grep ${PID} | grep -v grep`" ]
then
echo "Process is dead but pidfile exists"
else
echo "Running"
fi
else
echo "Service $NAME is not running."
fi
}
usage()
{
# Spit the RTFM
echo "Usage: $0 {start|stop|status|restart}"
exit 1
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
stop
sleep 5
start
;;
*)
usage
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment