Friday, October 25, 2013

Start a WSO2 Server as a Linux service

Sometimes there can be a requirement to start WSO2 Servers as Linux services. For example to start the server automatically in the boot-up and {start|stop|restart} as a normal Linux service later. This can be achieved easily with a small startup script which can be added to Linux startup.

If you need to run a service in the boot up, first you have to create a startup script and add it to the boot sequence. There are three main parts in the script; start, stop and restart. The basic structure of a startup service script is as follows;   

#!/bin/bash
 
case$1″ in
start)
   echo “Starting Service”
;;
stop)
   echo “Stopping Service”
;;
restart)
   echo “Restarting Service”
;;
*)
   echo $”Usage: $0 {start|stop|restart}”
exit 1
esac

Following script is a startup script written for WSO2 Application Server 5.2.0 with the above format.

#! /bin/sh
export JAVA_HOME="/usr/lib/jvm/jdk1.7.0_07"

startcmd='/opt/WSO2/wso2as-5.2.0/bin/wso2server.sh start > /dev/null &'
restartcmd='/opt/WSO2/wso2as-5.2.0/bin/wso2server.sh restart > /dev/null &'
stopcmd='/opt/WSO2/wso2as-5.2.0/bin/wso2server.sh stop > /dev/null &'

case "$1" in
start)
   echo "Starting WSO2 Application Server ..."
   su -c "${startcmd}" sumedha
;;
restart)
   echo "Re-starting WSO2 Application Server ..."
   su -c "${restartcmd}" sumedha
;;
stop)
   echo "Stopping WSO2 Application Server ..."
   su -c "${stopcmd}" sumedha
;;
*)
   echo "Usage: $0 {start|stop|restart}"
exit 1
esac

In this script the server will be started as the user 'sumedha' rather than the root.

    i.e. :  su -c "${startcmd}" sumedha

Then you have to add this script to /etc/init.d/ directory. Optionally you can only add a symbolic link to the script in /etc/init.d/ while keeping the actual script in a separate place. Also you have to make the script executable.

Lets say my script is 'appserver' and it is in /opt/WSO2/, then the command for adding it to /etc/init.d/ will be as follows;

    Make executable :   sudo chmod a+x /opt/WSO2/appserver
    Add a link to /etc/inin.d/ :   sudo ln -snf /opt/WSO2/appserver /etc/init.d/appserver

After that you have to install this script to respective run levels. This can be done by update-rc.d command. For above script it is as follows;

    sudo update-rc.d appserver defaults

If defaults is used then update-rc.d  will  make the service  to  start  in runlevels 2,3,4,5 and to stop in runlevels 0,1,6.

Now when you boot the system, the App Server will be up and running. 

You can {start|stop|restart} it by 'service appserver {start|stop|restart} '. You have to give the password of the user (or root) who was used to start the service.

Hope this is helpful..!

No comments:

Post a Comment