init.d daemon/service

1,019 views
Skip to first unread message

Ray Madigan

unread,
Sep 3, 2015, 2:56:41 PM9/3/15
to iDempiere
I have tried to find a linux init.d daemon for idempiere.  Does one already exist or do I build it myself?

Peter Snizek

unread,
Sep 3, 2015, 3:14:53 PM9/3/15
to iDempiere
This is what I am using and it's based almost 100% on a script that is included in the idempiere package.

Put the file into /etc/init.d

and then sudo update-rc.d idempiere defaults

I hope I didn't forget anything. It's a while ago since I did that.
Once it's done, the server should automatically start the idempiere service after booting the OS.
The service can be started, restarted or stopped with

service idempiere [start] [restart] [stop]

Filename: idempiere

### BEGIN INIT INFO
# Provides:             idempiere
# Required-Start:       $local_fs $remote_fs $network $syslog
# Required-Stop:        $local_fs $remote_fs $network $syslog
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:    iDempiere 2.1 server
# Description:          Provides iDempiere ERP-CRM Server startup and shutdown s                                                                                                      cript. Requires PostgreSQL server.
# FileTarget:   /etc/init.d/idempiere
# FileOwner:    root.root
# FilePerms:    0755
#
# chkconfig:    2345 97 06
### END INIT INFO

# initialization
# adjust these variables to your environment
IDEMPIERE_HOME=/srv/idempiere/idempiere-server
IDEMPIEREUSER=psnizek
# Instead of using ENVFILE you can set JAVA_HOME, IDEMPIERE_HOME and add JAVA_HO                                                                                                      ME/bin to PATH
# in this case you can comment the source lines for ENVFILE below
# detected some problems with Hardy Heron ubuntu using the bash source command
ENVFILE=$IDEMPIERE_HOME/utils/myEnvironment.sh

. /lib/lsb/init-functions

RETVAL=0
IDEMPIERESTATUS=
MAXITERATIONS=60

getidempierestatus() {
    IDEMPIERESTATUSSTRING=$(ps ax | grep -v grep | grep $IDEMPIERE_HOME)
    echo $IDEMPIERESTATUSSTRING | grep -q $IDEMPIERE_HOME
    IDEMPIERESTATUS=$?
}

start () {
    getidempierestatus
    if [ $IDEMPIERESTATUS -eq 0 ] ; then
        echo "iDempiere is already running"
        return 1
    fi
    echo -n "Starting iDempiere ERP: "
    cd $IDEMPIERE_HOME/utils
    . $ENVFILE
    export LOGFILE=$IDEMPIERE_HOME/log/idempiere_`date +%Y%m%d%H%M%S`.log
    su $IDEMPIEREUSER -c "mkdir -p IDEMPIERE_HOME/log"
    su $IDEMPIEREUSER -c "cd $IDEMPIERE_HOME;$IDEMPIERE_HOME/idempiere-server.sh                                                                                                       &> $LOGFILE &"
    RETVAL=$?
    if [ $RETVAL -eq 0 ] ; then
        # wait for server to be confirmed as started in logfile
        STATUSTEST=0
        ITERATIONS=0
        while [ $STATUSTEST -eq 0 ] ; do
            sleep 2
            tail -n 9 $LOGFILE | grep -q '.*WebUIServlet.*started successfully.*                                                                                                      ' && STATUSTEST=1
            echo -n "."
            ITERATIONS=`expr $ITERATIONS + 1`
            if [ $ITERATIONS -gt $MAXITERATIONS ]
                then
                break
            fi
        done
        if [ $STATUSTEST -eq 0 ]
        then
            log_warning_msg "Service hasn't started within the timeout allowed,                                                                                                       please review file $LOGFILE to see the status of the service"
        else
            log_success_msg "Service started"
        fi
        echo
    else
        log_failure_msg "Service not started"
    echo
    fi
    RETVAL=$?
    return $RETVAL
}

stop () {
    getidempierestatus
    if [ $IDEMPIERESTATUS -ne 0 ] ; then
          echo "iDempiere is already stopped"
          return 1
    fi
    echo -n "Stopping iDempiere ERP: "
    cd $IDEMPIERE_HOME/utils
    . $ENVFILE
    log_warning_msg "Trying direct kill with signal -15"
    # try direct kill with signal 15, then signal 9
    kill -15 -`ps ax o pgid,command | grep -v grep | grep $IDEMPIERE_HOME | sed                                                                                                       -e 's/^ *//g' | cut -f 1 -d " " | sort -u`
    sleep 5
    getidempierestatus
    if [ $IDEMPIERESTATUS -ne 0 ] ; then
          log_success_msg "Service stopped with kill -15"
    else
          echo "Trying direct kill with signal -9"
          kill -9 -`ps ax o pgid,command | grep -v grep | grep $IDEMPIERE_HOM                                                                                                          sed -e 's/^ *//g' | cut -f 1 -d " " | sort -u`
          sleep 5
          getidempierestatus
          if [ $IDEMPIERESTATUS -ne 0 ] ; then
            log_success_msg "Service stopped with kill -9"
          else
            log_warning_msg "Service hasn't stopped"
          fi
    fi
    return $RETVAL
}

restart () {
    stop
    sleep 1
    start
}

condrestart () {
    getidempierestatus
    if [ $IDEMPIERESTATUS -eq 0 ] ; then
           restart
    fi

}

status () {
    getidempierestatus
    if [ $IDEMPIERESTATUS -eq 0 ] ; then
                echo
                echo "iDempiere is running:"
                ps ax | grep -v grep | grep $IDEMPIERE_HOME | sed 's/^[[:s                                                                                                            ]*\([[:digit:]]*\).*:[[:digit:]][[:digit:]][[:space:]]\(.*\)/\1 \2/'
                echo
            else
                echo "iDempiere is stopped"
    fi
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        restart
        ;;
    condrestart)
        condrestart
        ;;
    status)
        status
        ;;
    *)
        echo $"Usage: $0 {start|stop|restart|condrestart|status}"
        exit 1
esac

exit 0

mhernandezve

unread,
Sep 3, 2015, 4:01:24 PM9/3/15
to iDempiere
Hi Ray,

You can find daemon init scripts for some linux distributions on "./idempiere-server/utils/unix/" on binary distribution file.

Peter Snizek

unread,
Mar 7, 2017, 8:43:34 PM3/7/17
to iDempiere
I recommend to add a line for loading the entropy daemon "haveged" (if need to install: sudo apt-get install haveged) into the script, before idempiere is executed.
On some servers idempiere 2.x is pausing for a long time during start up. This solves it.

Ayaz Ahmed

unread,
Mar 10, 2018, 6:54:12 AM3/10/18
to iDempiere
@Peter Snizek
Thanks Peter it works perfectly after installation "haveged".
Reply all
Reply to author
Forward
0 new messages