Tuesday, August 28, 2012

Running multiple servers at once using linux bash script

You might need to run multiple servers at once for the easiness rather starting one by one. This is useful at development phase when we deal with number of servers.
Here is the sample script which can be used to start number of servers. I've used wso2 carbon servers as sample servers.

#!/bin/bash
#
# carbon        
#
# chkconfig:
# description:     Start/stop  Carbon  servers.
# Source function library.
#. /etc/rc.d/init.d/functions
RETVAL=$?
ESB_HOME="/home/ubuntu/wso2/servers/wso2esb-4.5.0"
APIMGR_HOME="/home/ubuntu/wso2/servers/wso2am-1.0.0"
AS1_HOME="/home/ubuntu/wso2/servers/wso2as_1-5.0.0"
AS2_HOME="/home/ubuntu/wso2/servers/wso2as_2-5.0.0"
GREG_HOME="/home/ubuntu/wso2/servers/wso2greg-4.5.0"
IS_HOME="/home/ubuntu/wso2/servers/wso2is-4.0.0"
BAM_HOME="/home/ubuntu/wso2/servers/wso2bam-2.0.0"
case "$1" in
 start)
        if [ -f $ESB_HOME/bin/wso2server.sh ];
          then
        echo "Starting servers"
             $ESB_HOME/bin/wso2server.sh start
             $AS1_HOME/bin/wso2server.sh start
             $APIMGR_HOME/bin/wso2server.sh start
             $AS2_HOME/bin/wso2server.sh start
             $GREG_HOME/bin/wso2server.sh start
             $IS_HOME/bin/wso2server.sh start
             $BAM_HOME/bin/wso2server.sh start             
     fi
    ;;
   
 stop)
        if [ -f $ESB_HOME/bin/wso2server.sh ];
          then
        echo "Stopping servers"
             $ESB_HOME/bin/wso2server.sh stop
             $AS1_HOME/bin/wso2server.sh stop
             $APIMGR_HOME/bin/wso2server.sh stop
             $AS2_HOME/bin/wso2server.sh stop
             $GREG_HOME/bin/wso2server.sh stop
             $IS_HOME/bin/wso2server.sh stop
             $BAM_HOME/bin/wso2server.sh stop
        fi             
     ;;
   
 *)
     echo $"Usage: $0 {start|stop}"
    exit 1
    ;;
esac
exit $RETVAL
You can simply use a notepad editor to write the script and save it in the /etc/init.d/ folder with your preferred name.Say the script name is "carbon".
Start/Stop the servers as follows
# /etc/init.d/carbon start
# /etc/init.d/carbon stop
You may face permission issue to run your scripts..Use following command to overcome it;

# chmod 744 /etc/init.d/carbon

No comments:

Post a Comment