Flodding

[Script] init.d service script

13 posts in this topic

Hi friends... 

I created a VERY VERY basic init.d Script to start and stop my server...

here it is:

#!/bin/bash

### BEGIN INIT INFO
# Provides:		a3exileserver
# Required-Start:	$remote_fs $network
# Required-Stop:	$remote_fs $network
# Should-Start:	mysql
# Should-Stop:	mysql
# Default-Start:	2 3 4 5
# Default-Stop:	0 1 6
# Description:	ArmA 3 Exile init.d Script by Flodding
### END INIT INFO
#
#=======================================================================
#========               CONFIGURATION PARAMETERS                ========
#======== MUST BE EDITED MANUALLY TO FIT YOUR SYSTEM PARAMETERS ========
#=======================================================================

USERNAME=a3exile
ARMA_DIR='/home/a3exile/server'
NAME=instance_exile_altis
CONFIGFOLDER=${ARMA_DIR}/instance_exile_altis
CONFIG=${CONFIGFOLDER}/server.cfg
CFG=${CONFIGFOLDER}/basic.cfg
BEPATH=${CONFIGFOLDER}/battleye
PORT=2314
PIDFILE=${ARMA_DIR}/${PORT}.pid
SERVICE=arma3server
BACKUPPATH=/home/a3exile/backup
CPU_COUNT=2
BACKUPDAYS=3
MODS=@exile
#MODS=@allinarmaterrainpack"\;"@esseker"\;"@exile
SERVERMOD=@exileserver"\;"@a3xai
OPTIONS="-port=${PORT} -pid=${PIDFILE} -cfg=${CFG} -config=${CONFIG} -name=${NAME} -mod=${MODS} -servermod=${SERVERMOD} -autoinit"

#=======================================================================

a3_start() {
  echo ""
  if [ ! -f $ARMA_DIR/$SERVICE ]
  then
    echo "$SERVICE not found! STOPPING !!!"
    echo ""
    sleep 1
    exit
  else
    if  pgrep -u $USERNAME -f $SERVICE > /dev/null
    then
      echo "$SERVICE is already running!"
      echo ""
    else
      echo "Setting Permissions to needed..."
      echo ""
	  chmod -R 0755 $ARMA_DIR
	  chown -R $USERNAME:$USERNAME /home/$USERNAME
      echo "Starting $SERVICE..."
      echo ""
         cd $ARMA_DIR
         su $USERNAME -c "screen -dmS $USERNAME ./$SERVICE $OPTIONS" > stdout.log 2> stderr.log &
      echo "Searching Process $SERVICE"
      echo ""
	  sleep 8
	  if pgrep -u $USERNAME -f $SERVICE > /dev/null
	  then
	    echo "$SERVICE is now running."
           echo ""
	  else
           echo "Error! Could not start $SERVICE!"
           echo ""
         fi
    fi
  fi
}

a3_stop() {
  echo ""
  if pgrep -u $USERNAME -f $SERVICE > /dev/null
  then
    echo "Stopping $SERVICE !!"
    echo ""
	killall -9 -u $USERNAME
  else
    echo "$SERVICE is stopped."
    echo ""
  fi
}


case "$1" in
  start)
    a3_start
    ;;
  stop)
    a3_stop

    if [ -f ${PIDFILE} ]; then
    rm -f ${PIDFILE}
    fi
    sleep 5s
    ;;
  restart)
    a3_stop
    a3_start
    ;;
  status)
    if [ -f ${PIDFILE} ]; then
      PID=$(< ${PIDFILE})
      echo "PID-File existiert (PID=${PID})..."
        if [ -f /proc/${PID}/cmdline ]; then
          echo "Server Prozess scheint zu laufen..."
        fi
      fi
    ;;

*)
echo "$0 (start|stop|restart|status)"
exit 1
;;
esac

exit 0

now i have a small problem regarding the log files...

The server starts up normal, the logfiles are being created, but they are not filled with data. they remain at 0byte.

Question is how to format line 60 so the files are filled with data? Im sure theres a problem with this ";<" shit but i cant figure qut how to format right.

Thanks 

Edited by Flodding

Share this post


Link to post
Share on other sites
Advertisement
su $USERNAME -c "screen -dmS $USERNAME -L ./$SERVICE $OPTIONS >> stdout.log 2> stderr.log &"
su $USERNAME -c "screen -dmS $USERNAME -L ./$SERVICE $OPTIONS >> stdout.log 2>> stderr.log &"
su $USERNAME -c "screen -dmS $USERNAME -L ./$SERVICE $OPTIONS"  >> stdout.log 2> stderr.log &
su $USERNAME -c "screen -dmS $USERNAME -L ./$SERVICE $OPTIONS"  >> stdout.log 2>> stderr.log &

doesnt work :(

Share this post


Link to post
Share on other sites

Hello all!

I don't know much about screen.
Maybe using tmux?

#!/bin/bash
### BEGIN INIT INFO
# Provides:		    exile
# Required-Start:   $remote_fs $network
# Required-Stop:	$remote_fs $network
# Should-Start:	    mysql
# Should-Stop:      mysql
# Default-Start:    2 3 4 5
# Default-Stop:     0 1 6
# Description:      ArmA 3 Exile init.d Script by Flodding
### END INIT INFO

#=======================================================================
#========               CONFIGURATION PARAMETERS                ========
#======== MUST BE EDITED MANUALLY TO FIT YOUR SYSTEM PARAMETERS ========
#=======================================================================

USERNAME="steam" # This user *MUST* exist on the system.
EXILE_DIR="/home/${USERNAME}/servers/exile" # Change to your path...
NAME="INSTANCE_NAME_NO_SPACES" # You can use any name here, your server, or clan...
CONFIGFOLDER="${EXILE_DIR}/${NAME}"
CONFIG="${NAME}/config.cfg" # Remember to move config files from @exileserver/*.cfg to YOUR_INSTANCE_NAME/!
CFG="${NAME}/basic.cfg" # Remember to move config files from @exileserver/*.cfg to YOUR_INSTANCE_NAME/!
BEPATH="${EXILE_DIR}/battleye"
LOG_DIR="${CONFIGFOLDER}/logs"
PORT=2302
PIDFILE="${CONFIGFOLDER}/${PORT}.pid"
if [ -f ${PIDFILE} ]; then
	RUNNING=1
	PID=$(cat ${PIDFILE} > /dev/null)
else
	RUNNING=0
fi
SERVICE="arma3server"
MODS="@exile"
SERVERMOD="@exileserver"
#CPU_COUNT=2

# Some common options
# -ip=0.0.0.0
# -port=2302 (default)
# -mod=@exile;kart;mark;heli;
# -servermod=@exileserver
# -config=C:\Arma\Server\@exileserver\config.cfg
# -cfg=C:\Arma\Server\@exileserver\basic.cfg
# -name=INSTANCE
# -profiles=INSTANCE
# -log
# -nolog
# -world=empty
# -nosplash
# -nosound
# -nopause
# -malloc=system -malloc=tbbmalloc
# -autoinit

OPTIONS="-port=${PORT} -pid=${PIDFILE} -name=${NAME} -profiles=${NAME} -cfg=${CFG} -config=${CONFIG} -mod=${MODS} -servermod=${SERVERMOD} -nopause -nosound -nosplash -autoinit"
TMUX_SESSION="exile" # You can use any name here.

#=======================================================================
# CONFIG END
#=======================================================================

TMUX=$(which tmux)

[ ! -x "$TMUX" ] && echo "Tmux not found" >&2 && exit 1

if [ ! -d "$LOG_DIR" ]; then
    echo "${LOG_DIR} not found. Creating..."
    mkdir -p $LOG_DIR
fi

exile_start() {
    if [ ! -f $EXILE_DIR/$SERVICE ]
    then
        echo "$SERVICE not found! Stopping..."
        sleep 1
        exit
    else
        if  [ ${RUNNING} -eq 1 ];
        then
            echo "$SERVICE is already running!"
        else
            echo "Setting Permissions..."
            #chmod -R 0755 $EXILE_DIR
            chown -R $USERNAME:$USERNAME /home/$USERNAME
            echo "Starting $SERVICE..."
            cd $EXILE_DIR
			# Fix: http://exile.majormittens.co.uk/topic/6054-linux-dedicated-server-setup-guide-debian-7/?do=findComment&comment=28274
			find @exile/ -depth -exec rename 's/(.*)\/([^\/]*)/$1\/\L$2/' {} \;
			find @exileserver/ -depth -exec rename 's/(.*)\/([^\/]*)/$1\/\L$2/' {} \;
            if [ "${2}" == "-silent" ]; then
                su ${USERNAME} -c "${TMUX} new-session -d -s ${TMUX_SESSION} \"./${SERVICE} ${OPTIONS} > ${LOG_DIR}/exile.log 2> ${LOG_DIR}/errors.log\""
            else
                su ${USERNAME} -c "${TMUX} new-session -d -s ${TMUX_SESSION} \"./${SERVICE} ${OPTIONS} 2> ${LOG_DIR}/errors.log | tee ${LOG_DIR}/exile.log\""
            fi
            echo "Searching Process ${SERVICE}..."
            sleep 8
            if pgrep -u $USERNAME -f $SERVICE > /dev/null
            then
                echo "$SERVICE is now running."
				RUNNING=1
            else
                echo "Error! Could not start $SERVICE!"
				RUNNING=0
            fi
        fi
    fi
}

exile_stop() {
    if [ ${RUNNING} -eq 1 ];
    then
        echo "Stopping ${SERVICE}..."
        su $USERNAME -c "$TMUX kill-session -t $TMUX_SESSION"
        $TMUX kill-session -t $TMUX_SESSION
        killall -9 $SERVICE
    else
        echo "$SERVICE is stopped."
    fi

    if [ -f ${PIDFILE} ]; then
        rm -f ${PIDFILE}
    fi
}

exile_status() {
    if [ -f ${PIDFILE} ]; then
        PID=$(cat ${PIDFILE})
        echo "Server is running (PID=${PID})..."
    else
        echo "Server not running..."
        exit 0
    fi
}

case "$1" in
    start)
        exile_start
    ;;

    stop)
        exile_stop
    ;;

    restart)
        exile_stop
        exile_start
    ;;

    status)
        exile_status
    ;;

    attach)
        su $USERNAME -c "$TMUX at -t $TMUX_SESSION"
    ;;

    *)
        echo "$0 (start|stop|restart|status|attach)"
        exit 1
    ;;
esac

exit 0

Gist & Gist RAW.

 

Edited by [BRG] Pé de Porco
  • Like 1

Share this post


Link to post
Share on other sites
Guest

Hi @[BRG] Pé de Porco good script, i have a few question though, i have it 'working' on CentoOS 7, part from when i start it, it has a number of ./arma3server processes open? any ideas why, ive only changed path of where my exile is installed.

Share this post


Link to post
Share on other sites
On 11/25/2015 at 0:39 AM, AshleyIn1080p said:

Hi @[BRG] Pé de Porco good script, i have a few question though, i have it 'working' on CentoOS 7, part from when i start it, it has a number of ./arma3server processes open? any ideas why, ive only changed path of where my exile is installed.

I have the same issue with Debian 8.1. When you kill the process and launch it again, it will say "server is already running" when it clearly is not. Did you manage to fix this issue?

Share this post


Link to post
Share on other sites

Maybe this:

if pgrep -u $USERNAME -f $SERVICE > /dev/null

to this:

if pgrep arma3server >/dev/null 2>&1

...

BTW, i found a simple approach to do this...

crontab -e -u exile

* * * * * /bin/bash /home/exile/scripts/restart.sh

/home/exile/scripts/restart.sh

#!/bin/bash

. /home/exile/conf/config.cfg

if pgrep arma3server >/dev/null 2>&1
then
        exit 0
else
        cd $ARMAPATH
        ./arma3server $ARMAPARAMS &
fi
exit 0

/home/exile/conf/config.cfg

ARMAPATH="/home/exile/server/"
ARMAPARAMS="-port=2302 -name=exile -profile=exile -cfg=exile/basic.cfg -config=exile/config.cfg -mod=@exile -servermod=@exileserver;@marma -profiles -maxMem=2047 -noPause -enableHT -loadMissionToMemory -exThreads=7 -noSound -autoInit -noLogs"

Remember to change the PATHs and USERs.

Good luck.

Share this post


Link to post
Share on other sites

On centos7 you can use systemd, just create unit like this:

/var/lib/systemd/system/arma3.service

[Unit]
Description=Arma3 exilemod server
After=syslog.target network.target
Conflicts=

[Service]
Type=simple
PIDFile=/tmp/arma3.pid
#ExecStartPost=/usr/bin/systemctl restart arma3_hc.service
WorkingDirectory=/home/steam/steamcmd/exile
ExecStart=/home/steam/steamcmd/exile/arma3server -config=config.cfg -mod=@exile -servermod="@exileserver;" -autoinit -cpuCount=2 -exThreads=2
User=steam
Restart=always
TimeoutSec=60
RestartSec=60

[Install]
WantedBy=multi-user.target

systemctl daemon-reload 

systemctl start arma3 

  • Like 1

Share this post


Link to post
Share on other sites
Advertisement

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.