Service Script for LifeRay on Ubuntu

Liferay developer need write script to start and stop liferay as service. In my projects production and test server management i need this, and after spending few times  i got this. Here below i share my experience. The script code given below.

#!/bin/bash
#
# tomcat     This shell script takes care of starting and stopping Tomcat
#
# chkconfig: - 80 20
#
### BEGIN INIT INFO
# Provides: tomcat
# Required-Start: $network $syslog
# Required-Stop: $network $syslog
# Default-Start:
# Default-Stop:
# Short-Description: start and stop tomcat
### END INIT INFO

#TOMCAT_HOME="/home/tariq/projects/projects/bundles/tomcat-7.0.42"
TOMCAT_HOME="Home of your liferay tomcat"
SHUTDOWN_WAIT=45

tomcat_pid() {
#filter running process, here you can add extra filter criteria by adding | grep #criteria text
    echo `ps aux | grep org.apache.catalina.startup.Bootstrap | grep -v grep | awk '{ print $2 }'`
}

start() {
    pid=$(tomcat_pid)
    if [ -n "$pid" ]
    then
        echo "Tomcat is already running (pid: $pid)"
    else
        # Start tomcat
        echo "Starting tomcat"
        /bin/su - -c "cd $TOMCAT_HOME/bin && $TOMCAT_HOME/bin/startup.sh"
    fi
    return 0
}

stop() {
    pid=$(tomcat_pid)
    if [ -n "$pid" ]
    then
        echo "Stoping Tomcat"
        /bin/su - -c "cd $TOMCAT_HOME/bin && $TOMCAT_HOME/bin/shutdown.sh"

    let kwait=$SHUTDOWN_WAIT
    count=0
    count_by=5
    until [ `ps -p $pid | grep -c $pid` = '0' ] || [ $count -gt $kwait ]
    do
        echo "Waiting for processes to exit. Timeout before we kill the pid: ${count}/${kwait}"
        sleep $count_by
        let count=$count+$count_by;
    done

    if [ $count -gt $kwait ]; then
        echo "Killing processes which didn't stop after $SHUTDOWN_WAIT seconds"
        kill -9 $pid
    fi
    else
        echo "Tomcat is not running"
    fi

    return 0
}

case $1 in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    status)
       pid=$(tomcat_pid)
        if [ -n "$pid" ]
        then
           echo "Tomcat is running with pid: $pid"
        else
           echo "Tomcat is not running"
        fi
        ;;
    *)
    echo "Usage: /etc/init.d/atd {start|stop|restart|status}"
    exit 1
    ;;
esac

exit 0

##End script code

Follow the below steps:
1.Open a text editor and copy  paste above code. rename the file as your convenient such as liferay.

2.You’ll need to make the script executable by running the chmod command:
sudo chmod 755 /etc/init.d/liferay

3.The last step is actually linking this script to the startup folders with a symbolic link. Execute these two commands and we should be on our way.
sudo ln -s /etc/init.d/liferay /etc/rc1.d/K99liferay
sudo ln -s /etc/init.d/liferay /etc/rc2.d/S99liferay
Reference Link:
https://www.liferay.com/web/brett.swaim/blog/-/blogs/trackback/sample-tomcat-startup-scripts

Comments

Popular posts from this blog

How to run ofbiz as ubuntu Service.

JPA vs Spring JPA vs Spring Data JPA vs Hibernate

Java Array Interview Questions for Entry-Level Developers Part 01