Tuesday 9 August 2011

e1agent as a service on linux with auto start

Even with OEL templates, the auto start an stop of aspects of JDE is not great, so I’m on a mission to change this.  I’m a little stunned that the templates do not have this built in, to be honest.   I’m going to write service control scripts (to go into /etc/init.d) to allow you to easily start and stop processes with “service” controls.  You might even be able to use the GUI interface that gnome-session provides (see diagram later in this post).

With the installation of the above, you’ll be able to see if the agent is running and easily stop and start the agent.

This requires a couple of things:

Firstly,  a config file called e1Agent in /etc/sysconfig, with contents like the below:  remember to change your e1agent home and e1_agent_user to the site specific values

#Config values for e1Agent
e1_agent_home=/u01/jdedwards/jde_home
e1_agent_user=oracle

Once this is done, you need to create the /etc/init.d/e1Agent file with the following contents:

#!/bin/sh
#
# /etc/rc.d/init.d/e1Agent
#
# Starts JDE Management Agent
#
# chkconfig: 345 99 00
# description: e1Agent service control script
#

## get success, failure function
. /etc/init.d/functions

#Get the E1 specifics
if [ -f /etc/sysconfig/e1Agent ]; then
    . /etc/sysconfig/e1Agent
fi

# userid to run server as
: ${e1_agent_user:=oracle}
# CHANGETHIS: Installation home for Agent
: ${e1_agent_home:="/u01/jdedwards/jde_home"}


prog="               0racle E1 Agent"
: ${pidfile:="/var/run/e1Agent.pid"}
: ${e1_agent_start_log:=${e1_agent_home}/logs/e1agent_0.log}

## Summary
## Start:
##    Background a runAgent.sh by the installation user,
## Stop:
##    Kill the e1agent ("java") processes

RETVAL=0
case "$1" in
  start)
    if [ ! -x ${e1_agent_home}/bin/runAgent ]; then
        echo ${e1_agent_home}/bin/runAgent not executable. >&2
                failure
        exit 1
    fi
        if [ `ps -ef |grep java |grep -v grep | grep scfagent.jar | wc -l` -gt 0 ]; then
                echo "Agent is already running"
                failure
                exit 1
        fi

    ## runAgent
    ## - should be started as installation user (ex.bea,weblogic,oracle)
    ## - runs in foreground, so background here. Record pid
        echo -n $"Starting $prog: "
    sudo -u ${e1_agent_user} ${e1_agent_home}/bin/runAgent > ${e1_agent_start_log} 2>&1 < /dev/null &
    e1_agent_pid=$!    ;# assume sudo exec(2)s, not fork

    echo $e1_agent_pid > ${pidfile}

    touch /var/lock/subsys/e1Agent
    success $"         e1 Agent startup"
    ;;
  stop)
     ##  Try stopAgent first
    if [ ! -x ${e1_agent_home}/bin/stopAgent ]; then
        echo ${e1_agent_home}/bin/stopAgent not executable. >&2
        exit 1
    fi

    read e1_agent_pid < ${pidfile}
    test -z "$e1_agent_pid" && exit 1
   
    #sudo -u ${e1_agent_user} ${e1_agent_home}/bin/stopAgent > ${e1_agent_start_log} 2>&1 < /dev/null
    sudo -u ${e1_agent_user} ${e1_agent_home}/bin/stopAgent > ${e1_agent_start_log} 2>&1

    if [ `ps -ef |grep -v grep |grep $e1_agent_pid |wc -l` -eq 0 ] ; then
          rm -f /var/lock/subsys/e1Agent
          rm -f ${pidfile}
      success $"          e1 agent shutdown"
        else
          echo 'stop agent unsuccessful, try forcestop'
    fi         
    ;;
  forcestop)
    ## easy way: kill away all weblogic-ish java
    echo -n $"Stopping $prog: "
    kill -9 `ps -ef |grep scfagent.jar |grep -v grep | awk '{print($2)}'`
        rm -f /var/lock/subsys/e1Agent
        rm -f ${pidfile}
    ;;
  statusverbose)
    ## give some nerdy information
        echo "Config file: /etc/sysconfig/e1Agent"
        if [ `ps -ef |grep scfagent.jar |grep -v grep | wc -l` -eq 1 ]; then
          success $"          gent is running"
        else
          failure
        fi
        read e1_agent_pid < ${pidfile}
        echo ${pidfile} indicates pid is ${e1_agent_pid}
        if [ -f /var/lock/subsys/e1Agent ]; then
          ls -l /var/lock/subsys/e1Agent
          echo "Lock files exists"
        else
          echo "no lockfile"
        fi
        echo "refer to " ${e1_agent_home}/logs/e1agent_0.log " for more info"
    ;;
  status)
        if [ `ps -ef |grep scfagent.jar |grep -v grep | wc -l` -eq 1 ]; then
            read e1_agent_pid < ${pidfile}
            echo e1Agent \(pid ${e1_agent_pid}\) is running
        else
            echo "e1Agent is stopped"
        fi
    ;;
  restart)
    ## check that its running and then restart
    read e1_agent_pid < ${pidfile}
    test -z "$e1_agent_pid" && exit 1
    if [ `ps -ef |grep -v grep |grep scfagent.jar | grep $e1_agent_pid | wc -l` -gt 0 ]; then
            #assumption is that proc is alive, restart!
            ${e1_agent_home}/bin/restartAgent > ${e1_agent_start_log} 2>&1 < /dev/null &
            e1_agent_pid=$!
        echo $e1_agent_pid > ${pidfile}
        touch /var/lock/subsys/e1Agent
            success $"           Oracle e1 Agent restart"
        else
            echo "Agent not running, cannot restart - try starting"
            exit 1
        fi
    ;;
  *)
    echo $"Usage: $0 {start|stop|forcestop|restart|status|statusverbose}"
    exit 2
esac

exit $RETVAL

finally, as root, execute the following to enable autostart:

chkconfig e1Agent on

So, you can now execute service e1Agent start|stop|restart|status|statusverbose and it’ll start or stop or do what you need.

image

You’ll also see the service listed if you look at the e1Agent service through something like gnome-session.

No comments: