OS4X Core init script

From OS4X
Revision as of 14:22, 10 October 2024 by Admin (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

For runlevel scripts, this script can be used as a basis for further needs. It works good in an OS4XBox environment. Actual versions of init-scripts can be found here: https://www.os4x.com/downloads/

#!/bin/bash 
### BEGIN INIT INFO
# Provides: os4x
# Required-Start: mysql
# Required-Stop: mysql
# Default-Start: 2 3 5
# Default-Stop: 0 1 6
# Description: Start or stop OS4X
### END INIT INFO


#
# OS4X
#

# Check for existence of needed config file and read it
OS4X_CONFIG=/etc/os4x.conf
test -r $OS4X_CONFIG || { echo "$OS4X_CONFIG not existing";
        if [ "$1" = "stop" ]; then exit 0;
        else exit 6; fi; }  

# first, source the config file for database parameters
. $OS4X_CONFIG

# calculate MySQL client parameters
MYSQLCLIENT_PARAMS="--silent --user=$DB_USER --password=$DB_PASS --host=$DB_HOST $DB_NAME"
if [ ! -z "$DB_TLS_VERSION" ]
then
	MYSQLCLIENT_PARAMS="$MYSQLCLIENT_PARAMS --tls-version=$DB_TLS_VERSION"
fi
if [ ! -z "$DB_TLS_CIPHER" ]
then
	MYSQLCLIENT_PARAMS="$MYSQLCLIENT_PARAMS --ssl-cipher=$DB_TLS_CIPHER"
fi
if [ ! -z "$DB_TLS_KEY" ]
then
	MYSQLCLIENT_PARAMS="$MYSQLCLIENT_PARAMS --ssl-key=$DB_TLS_KEY"
fi
if [ ! -z "$DB_TLS_CERT" ]
then
	MYSQLCLIENT_PARAMS="$MYSQLCLIENT_PARAMS --ssl-cert=$DB_TLS_CERT"
fi
if [ ! -z "$DB_TLS_CA" ]
then
	MYSQLCLIENT_PARAMS="$MYSQLCLIENT_PARAMS --ssl-ca=$DB_TLS_CA"
fi
if [ ! -z "$DB_TLS_CAPATH" ]
then
	MYSQLCLIENT_PARAMS="$MYSQLCLIENT_PARAMS --ssl-capath=$DB_TLS_CAPATH"
fi

# retrieve directories and settings
OS4X_BINDIR=`echo "SELECT value FROM ${TABLEPREFIX}configuration WHERE name='bin_directory'" | $MYSQLCLIENT $MYSQLCLIENT_PARAMS 2>/dev/null`
OS4X_TMPDIR=`echo "SELECT value FROM ${TABLEPREFIX}configuration WHERE name='tmp_directory'" | $MYSQLCLIENT $MYSQLCLIENT_PARAMS 2>/dev/null`
OS4X_IS_PORTAL=`echo "SELECT value FROM ${TABLEPREFIX}configuration WHERE name='enterprise_is_this_portal_server'" | $MYSQLCLIENT $MYSQLCLIENT_PARAMS 2>/dev/null`

if [ -z "${OS4X_IS_PORTAL}" ]
then
	OS4X_IS_PORTAL=0
fi

# check license
if [ -e ${OS4X_BINDIR}/license.key ]
then
	LICFUNCTIONS=`${OS4X_BINDIR}/os4x_licinfo -A 2>/dev/null`
else
	LICFUNCTIONS=0
fi

if [ $? -ne 0 ]
then
	LICFUNCTIONS=0
fi

(((${LICFUNCTIONS}&1)==1)) && LIC_OS4XCORE=1 || LIC_OS4XCORE=0
(((${LICFUNCTIONS}&2)==2)) && LIC_OS4XENTERPRISE_LITE=1 || LIC_OS4XENTERPRISE_LITE=0
(((${LICFUNCTIONS}&4)==4)) && LIC_OS4XENTERPRISE_ALL=1 || LIC_OS4XENTERPRISE_ALL=0
(((${LICFUNCTIONS}&8)==8)) && LIC_OS4X_AS2=1 || LIC_OS4X_AS2=0
(((${LICFUNCTIONS}&16)==16)) && LIC_OS4X_PORTAL=1 || LIC_OS4X_PORTAL=0

if [ "${OS4X_IS_PORTAL}" -eq 1 -a ${LIC_OS4X_PORTAL} -eq 1 ]
then
	LIC_OS4X_PORTAL=0
	LIC_OS4XENTERPRISE_ALL=1
fi

OS4X_WEBINTERFACE=`echo "SELECT value FROM ${TABLEPREFIX}configuration WHERE name='webinterface_path'" | $MYSQLCLIENT $MYSQLCLIENT_PARAMS 2>/dev/null`

# Check for missing binaries (stale symlinks should not happen)
# Note: Special treatment of stop for LSB conformance
OS4XRD_BIN=$OS4X_BINDIR/os4xrd
OS4XSQD_BIN=$OS4X_BINDIR/os4xsqd
OS4XCLIENTD_BIN=$OS4X_BINDIR/os4xclientd
OS4XAS2_BIN=$OS4X_BINDIR/os4x_as2
OS4XPOLL_BIN=$OS4X_BINDIR/os4xpoll
OS4XPORTAL_BIN=$OS4X_BINDIR/os4xportald

# sanity checks for daemon existance
if [ ! -e "$OS4XCLIENTD_BIN" ]
then
	LIC_OS4XENTERPRISE_ALL=0
	LIC_OS4XENTERPRISE_LITE=0
fi
if [ ! -e "$OS4XPORTAL_BIN" ]
then
	LIC_OS4X_PORTAL=0
fi
if [ ! -e "$OS4XAS2_BIN" ]
then
	LIC_OS4X_AS2=0
fi

function killDaemon
{
	if [ -f ${OS4X_TMPDIR}/${1}.pid ]
	then
		kill `cat ${OS4X_TMPDIR}/${1}.pid`
	else
		killall ${1} >/dev/null 2>&1
	fi
}

case "$1" in
    stop)
	echo -n "Shutting down OS4X "
	pidof os4xrd >/dev/null 2>&1  
	if [ $? -eq 0 ]
	then
		echo -n "os4xrd... "
		killDaemon os4xrd
	fi
	pidof os4xsqd >/dev/null 2>&1  
	if [ $? -eq 0 ]
	then
		echo -n "os4xsqd... "
		killDaemon os4xsqd
	fi
	pidof os4xdebugd >/dev/null 2>&1  
	if [ $? -eq 0 ]
	then
		echo -n "os4xdebugd... "
		killDaemon os4xdebugd
	fi
	pidof os4xclientd >/dev/null 2>&1  
	if [ $? -eq 0 ]
	then
		echo -n "os4xclientd... "
		killDaemon os4xclientd
	fi
	pidof os4x_as2_master >/dev/null 2>&1  
	if [ $? -eq 0 ]
	then
		echo -n "os4x_as2... "
		killDaemon os4x_as2
	fi
	pidof os4xportald >/dev/null 2>&1  
	if [ $? -eq 0 ]
	then
		echo -n "os4xportald... "
		killDaemon os4xportald
	fi
	echo OK
	;;
    start)
    # pre-check lockfile
    if [ -d $OS4X_WEBINTERFACE ]
    then
      if [ -e ${OS4X_WEBINTERFACE}/blank_install ]
      then
        # try to recover installation
        if [ -e /sd_card/os4x.*.sql.gz ]
        then
          # backupfile exists, restore complete system
          /opt/os4x/scripts/systemrestore_from_sd.sh && rm -f ${OS4X_WEBINTERFACE}/blank_install
        else
          echo "not starting OS4X: blank installation, please configure OS4X"
          exit
        fi
      fi
    fi

	echo -n "Starting OS4X "
	if [ ${LIC_OS4XCORE} -eq 1 ]
	then
	 	echo -n "os4xrd... "
		$OS4XRD_BIN
                if [ $? -eq 0 ]; then
                    echo "INSERT INTO ${TABLEPREFIX}system_log (datum, code, message, pid, program) VALUES (NOW(), '0', 'Receive daemon started normally.', '$$', '$0')" | $MYSQLCLIENT $MYSQLCLIENT_PARAMS 2>/dev/null
                else
                    echo "INSERT INTO ${TABLEPREFIX}system_log (datum, code, message, pid, program) VALUES (NOW(), '1', 'Start of Receive daemon failed.', '$$', '$0')" | $MYSQLCLIENT $MYSQLCLIENT_PARAMS 2>/dev/null
                fi
		echo -n "os4xsqd... "
		$OS4XSQD_BIN
                if [ $? -eq 0 ]; then
                    echo "INSERT INTO ${TABLEPREFIX}system_log (datum, code, message, pid, program) VALUES (NOW(), '0', 'Send queue daemon started normally.', '$$', '$0')" | $MYSQLCLIENT $MYSQLCLIENT_PARAMS 2>/dev/null
                else
                    echo "INSERT INTO ${TABLEPREFIX}system_log (datum, code, message, pid, program) VALUES (NOW(), '1', 'Start of Send queue daemon failed.', '$$', '$0')" | $MYSQLCLIENT $MYSQLCLIENT_PARAMS 2>/dev/null
                fi
	fi
	if [ ${LIC_OS4XENTERPRISE_LITE} -eq 1 -o ${LIC_OS4XENTERPRISE_ALL} -eq 1 ]
	then
		echo -n "os4xclientd... "
		$OS4XCLIENTD_BIN
                if [ $? -eq 0 ]; then
                    echo "INSERT INTO ${TABLEPREFIX}system_log (datum, code, message, pid, program) VALUES (NOW(), '0', 'Client daemon started normally.', '$$', '$0')" | $MYSQLCLIENT $MYSQLCLIENT_PARAMS 2>/dev/null
                else
                    echo "INSERT INTO ${TABLEPREFIX}system_log (datum, code, message, pid, program) VALUES (NOW(), '1', 'Start of Client daemon failed.', '$$', '$0')" | $MYSQLCLIENT $MYSQLCLIENT_PARAMS 2>/dev/null
                fi
	fi
	if [ ${LIC_OS4X_AS2} -eq 1 -a -e $OS4XAS2_BIN ]
	then
	 	echo -n "os4x_as2... "
		$OS4XAS2_BIN
                if [ $? -eq 0 ]; then
                    echo "INSERT INTO ${TABLEPREFIX}system_log (datum, code, message, pid, program) VALUES (NOW(), '0', 'AS2 daemon started normally.', '$$', '$0')" | $MYSQLCLIENT $MYSQLCLIENT_PARAMS 2>/dev/null
                else
                    echo "INSERT INTO ${TABLEPREFIX}system_log (datum, code, message, pid, program) VALUES (NOW(), '1', 'Start of AS2 daemon failed.', '$$', '$0')" | $MYSQLCLIENT $MYSQLCLIENT_PARAMS 2>/dev/null
                fi
	fi
	if [ ${LIC_OS4X_PORTAL} -eq 1 -a -e $OS4XPORTAL_BIN ]
	then
	 	echo -n "os4xportald... "
		$OS4XPORTAL_BIN
                if [ $? -eq 0 ]; then
                    echo "INSERT INTO ${TABLEPREFIX}system_log (datum, code, message, pid, program) VALUES (NOW(), '0', 'Portal daemon started normally.', '$$', '$0')" | $MYSQLCLIENT $MYSQLCLIENT_PARAMS 2>/dev/null
                else
                    echo "INSERT INTO ${TABLEPREFIX}system_log (datum, code, message, pid, program) VALUES (NOW(), '1', 'Start of Portal daemon failed.', '$$', '$0')" | $MYSQLCLIENT $MYSQLCLIENT_PARAMS 2>/dev/null
                fi
	fi
	echo "OK"
	;;
    status)
	echo -n "Checking for service OS4X "
	if [ ${LIC_OS4XCORE} -eq 1 ]
	then
		pidof os4xrd >/dev/null 2>&1  
		if [ $? -ne 0 ]
		then
		  echo -n "... os4xrd is not running"
		else
		  echo -n "... os4xrd OK "
		fi 
		pidof os4xsqd >/dev/null 2>&1  
		if [ $? -ne 0 ]
	    then
		  echo -n "... os4xsqd is not running"
		else
		  echo -n "... os4xsqd OK "
	    fi
	fi
	if [ ${LIC_OS4XENTERPRISE_LITE} -eq 1 -o ${LIC_OS4XENTERPRISE_ALL} -eq 1 ]
	then
		pidof os4xclientd >/dev/null 2>&1  
    	if [ $? -ne 0 ]
    	then
   		   echo -n "... os4xclientd is not running"
   		else
   		   echo -n "... os4xclientd OK "
    	fi
   	fi
	if [ ${LIC_OS4X_AS2} -eq 1 ]
	then
		pidof os4x_as2 >/dev/null 2>&1  
		if [ $? -ne 0 ]
	    then
		  echo -n "... os4x_as2 is not running"
		else
		  echo -n "... os4x_as2 OK "
	    fi
	fi
	if [ ${LIC_OS4X_PORTAL} -eq 1 ]
	then
		pidof os4xportald >/dev/null 2>&1  
		if [ $? -ne 0 ]
	    then
		  echo -n "... os4xportald is not running"
		else
		  echo -n "... os4xportald OK "
	    fi
	fi
	pidof os4xdebugd > /dev/null 2>&1 
	if [ $? -ne 0 ]
    then
	  echo "... os4xdebugd is not running"
	else
	  echo "... os4xdebugd OK"
    fi
	;;
    update)
	echo -n "Initiating OS4X update... "
        $OS4XPOLL_BIN OS4X-Update
	echo "OK"
	;;
	restart)
	$0 stop ; sleep 2; $0 start
	;;
    *)
	echo "Usage: $0 {start|stop|restart|status|update}"
	exit 1
	;;
esac
# rc_exit