Thursday, June 4, 2015

How to write systemd service to start stop wso2 server in RHEL7 CentOS7



In rhel7/centos7 we use systemd services.

One of the major changes in RHEL  / CentOS 7.0 is the swtich to systemd, a system and service manager, that replaces SysV and Upstart used in previous releases of Red Hat Enterprise Linux. 

Ex.
#systemctl start myservice.service

Here I explain how to write systemd service in rhel7 to work with any wso2 (carbon) server.
Before creating systemd service we have create a service call file in your wso2Home/bin directory (if not exist a service file),

#cd <wso2HOME>/bin
#vi myService.sh

Then add below code to the file,

#!bin/sh
USER="user"           
PRODUCT_CODE="DSS"                               
CARBON_HOME="wso2HOME"               
LOCK_FILE="${CARBON_HOME}/wso2carbon.lck"
PID_FILE="${CARBON_HOME}/wso2carbon.pid"
CMD="${CARBON_HOME}/bin/wso2server.sh"  
JAVA_HOME="/opt/jdk1.7.0_60"          

export JAVA_HOME=$JAVA_HOME

# Status the service
status() {
 if [ -f $PID_FILE ]
     then
  PID=`cat $PID_FILE`
  ps -fp $PID > /dev/null 2>&1
  PIDVAL=$?
     else
  PIDVAL=3
 fi

 if [ $PIDVAL -eq 0 ]
     then
  echo "WSO2 $PRODUCT_CODE server is running ..."
     else
  echo "WSO2 $PRODUCT_CODE server is stopped."
 fi
 return $PIDVAL
}

# Start the service
start() {
 if [ -f $PID_FILE ]
     then
  PID=`cat $PID_FILE`
  ps -fp $PID > /dev/null 2>&1
  PIDVAL=$?
     else
  PIDVAL=3
 fi

 if [ $PIDVAL -eq 0 ]
     then
        echo "WSO2 $PRODUCT_CODE server is running ..."
     else
        echo -n "Starting WSO2 $PRODUCT_CODE server: "
        touch $LOCK_FILE
        su - $USER -c "$CMD start > /dev/null 2>&1 &"
        sleep 5
        if [ -f $PID_FILE ]
     then
   PID=`cat $PID_FILE`
   ps -fp $PID > /dev/null 2>&1
   PIDVAL=$?
   if [ $PIDVAL -eq 0 ]
       then
    echo "success"
       else
    echo "failure"
   fi
     else
   echo "failure"
   PIDVAL=2
        fi
 fi
 echo
 return $PIDVAL
}

# Restart the service
restart() {
 echo -n "Restarting WSO2 $PRODUCT_CODE server: "
 touch $LOCK_FILE
 su - $USER -c "$CMD restart > /dev/null 2>&1 &"
 sleep 15
 if [ -f $PID_FILE ]
     then
  PID=`cat $PID_FILE`
  ps -fp $PID > /dev/null 2>&1
  PIDVAL=$?
  if [ $PIDVAL -eq 0 ]
      then
   echo "success"
      else
   echo "failure"
  fi
     else
  echo "failure"
  PIDVAL=2
 fi
 echo
 return $PIDVAL
}

# Stop the service
stop() {
 if [ -f $PID_FILE ]
     then
  PID=`cat $PID_FILE`
  ps -fp $PID > /dev/null 2>&1
  PIDVAL=$?
  if [ $PIDVAL -eq 0 ]
      then
   echo -n "Stopping WSO2 $PRODUCT_CODE server: "
   su - $USER -c "$CMD stop > /dev/null 2>&1 &"
   rm -f $LOCK_FILE
   sleep 10
   PID=`cat $PID_FILE`
   ps -fp $PID > /dev/null 2>&1
   PIDVAL=$?
   if [ $PIDVAL -eq 0 ]
       then
    echo "failure"
    PIDVAL=2
       else
    echo "success"
    PIDVAL=0
   fi
      else
         echo "WSO2 $PRODUCT_CODE server is not running."
         PIDVAL=0
  fi
     else
        echo "WSO2 $PRODUCT_CODE server is not running."
        PIDVAL=0
 fi
 echo
 return $PIDVAL
}

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

Change first 7 lines with your data. Reffere exmples in comments.

Now let’s create systemd service,

Systemd services are located in /etc/systemd/system directory.go to the directory,

#cd /etc/systemd/system

Create a file with .service extention(this will be service you are going to use),

#vi myservice.service

Then add below code to your created file,

[Unit]
Description=Wso2 Identity Server
After=syslog.target network.target

[Service]
Type=oneshot
ExecStart=/<wso2serverHOME>/bin/myService.sh start
ExecStop=/<wso2serverHOME>/bin/ myService.sh stop
RemainAfterExit=yes
StandardOutput=syslog
StandardError=syslog

[Install]
WantedBy=multi-user.target

That’s all. Now you have successfully created the systemd service.

Check the service using below commands and accessing wso2 product consoles,

#systemctl start myservice.service
#systemctl start myservice.service

** note that if you face any error starting stopping the service, check privileges granted to the service.

6 comments:

  1. Your service call has to have #!bin/sh, if not it will return fail.

    ReplyDelete
  2. thank you Akila's Tech Blog and Faizal Kamil

    ReplyDelete
  3. I'm testing in ubuntu an the first line looks like are wrong, this works for me:

    #! /bin/sh

    The rest works like a charm... thanks for sharing...

    ReplyDelete
  4. Writing a systemd service is essential for smooth system management. This process ensures a program starts on boot, enhancing system efficiency. Best Games Explore By carefully following the steps, administrators can guarantee reliable performance, seamless automation.

    ReplyDelete