How to run ofbiz as ubuntu Service.

Apache Ofbiz developer need write script to start and stop ofbiz server as Ubuntu service. In my projects production and test server management i need this, and after spending few times  i got this. Here below i share my experience. The script code given below.

#!/bin/bash -e

### BEGIN INIT INFO
# Provides:          ofbiz
# Required-Start:    $syslog $time $remote_fs
# Required-Stop:     $syslog $time $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: start Apache Ofbiz
# Description:       Debian init script for the Apache Ofbiz,
#                    the open source enterprise automation software
### END INIT INFO
set -e
######################################################################
export JAVA_HOME
#export JAVA_HOME=/usr/lib/jvm/java-7-oracle
export CLASSPATH=$JAVA_HOME
######################################################################
export JAVA_BINARY=$JAVA_HOME/bin/java
export JAVA=$JAVA_HOME/bin/java
OFBIZ_HOME=/home/tariq/projects/projects/ofbiz
OFBIZ_LOG=$OFBIZ_HOME/runtime/logs/script.log
OFBIZ_OUT=/tmp/OfbizOut
#JAVA_VMOPTIONS="-Xms768M -Xmx1024M -Duser.language=en"
JAVA_ARGS="-jar ${OFBIZ_HOME}/ofbiz.jar"
OFBIZ_USER=root
######################################################################
# shutdown settings
#ADMIN_PORT=10523
#ADMIN_KEY="InsertYourOwnKeyHered!"

# VM args
#ADMIN="-Dofbiz.admin.port=$ADMIN_PORT"
MEMIF="-Xms512M -Xmx1024M"
#MISC="-Duser.language=en"
VMARGS="$MEMIF"
#
SHUTDOWN_WAIT=45
ofbiz_pro() {
    echo `ps aux -u root | grep ofbiz/ofbiz.jar | grep -v grep | awk '{ print $2 }'`
}


start() {
    echo "Starting OFBiz: "
    echo "Testing running OFBiz: "

    pid=$(ofbiz_pro)

    if [ -n "$pid" ]
    then
     echo "OFBiz is already running...(pid: $pid)"
        return 1
    else
        # Start tomcat
        echo "Now really starting OFBiz: "
        echo "Now really starting OFBiz: "
    cd $OFBIZ_HOME && sudo ant start-batch
    echo "startup return value: " $?
    fi
    return 0

}

# Stop OFBiz
stop() {

    pid=$(ofbiz_pro)
    if [ -n "$pid" ]
    then
        echo "Stoping Ofbiz"
        cd $OFBIZ_HOME && sudo ant stop

    let kwait=$SHUTDOWN_WAIT
    count=0
    count_by=5
    until [ `ps -p $pid | grep -c $pid` = '0' ] || [ $count -gt $kwait ]
    do
        echo "Waiting for processes to exit. Timeout before we kill the pid: ${count}/${kwait}"
        sleep $count_by
        let count=$count+$count_by;
    done

    if [ $count -gt $kwait ]; then
        echo "Killing processes which didn't stop after $SHUTDOWN_WAIT seconds"
        kill -9 $pid
    fi
    else
        echo "Ofbiz is not running"
    fi

    return 0
}

. /lib/lsb/init-functions

case "$1" in
    start)
    log_daemon_msg "Starting Apache Ofbiz" "ofbiz"
    start
    log_end_msg $?
    ;;
  stop)
    log_daemon_msg "Stopping deferred Apache Ofbiz" "ofbiz"
    stop
    log_end_msg $?
    ;;
  force-reload|restart)
    stop
    start
    ;;
  status)
       pid=$(ofbiz_pro)
        if [ -n "$pid" ]
        then
           echo "Ofbiz is running with pid: $pid"
        else
           echo "Ofbiz is not running"
        fi
        ;;
  *)
    echo "Usage: /etc/init.d/atd {start|stop|restart|force-reload|status}"
    exit 1
    ;;
esac
exit 0

##End script code

Follow the below steps:
1.Open a text editor and copy  paste above code. rename the file as your convenient such as ofbiz.

2.You’ll need to make the script executable by running the chmod command:
sudo chmod 755 /etc/init.d/ofbiz

3.The last step is actually linking this script to the startup folders with a symbolic link. Execute these two commands and we should be on our way.
sudo ln -s /etc/init.d/ofbiz /etc/rc1.d/K99ofbiz
sudo ln -s /etc/init.d/ofbiz /etc/rc2.d/S99ofbiz

Comments

Popular posts from this blog

JPA vs Spring JPA vs Spring Data JPA vs Hibernate

Java Array Interview Questions for Entry-Level Developers Part 01