M
M
Mika Slepinin2016-12-06 18:29:25
bash
Mika Slepinin, 2016-12-06 18:29:25

How to modify the init script so that it does not duplicate processes?

Good afternoon.
There is one init script, it works fine, but if you run restart, then the process is doubled - restart again - it starts, and so many processes are started that can only be killed using kill by pid.
I have so far removed restart from the script, but can it be fixed in some way?
Here is the script itself:

#!/bin/bash
#
# tomcat
#
# chkconfig: 345 96 30
# description:  Start up the Tomcat servlet engine.
#
# processname: java
# pidfile: /var/run/tomcat.pid
#
### BEGIN INIT INFO
# Provides: tomcat
# Required-Start: $network $syslog
# Required-Stop: $network $syslog
# Should-Start: distcache
# Short-Description: start and stop Apache HTTP Server
# Description: implementation for Servlet 2.5 and JSP 2.1
## END INIT INFO

# Source function library.
. /etc/init.d/functions

## tomcat installation directory
PROCESS_NAME=tomcat

CATALINA_HOME="/tomcat"

## run as a diffent user
TOMCAT_USER=tomcat

##  Path to the pid, runnning info file
pidfile=${PIDFILE-/var/run/${PROCESS_NAME}.pid};
lockfile=${LOCKFILE-/var/lock/subsys/${PROCESS_NAME}};

RETVAL=0

case "$1" in
 start)
        PID=`pidofproc -p ${pidfile} ${PROCESS_NAME}`
        if ; then
                logger -s "${PROCESS_NAME}(pid ${PID}) is  already running."
                exit;
        fi
        if [ -f $CATALINA_HOME/bin/startup.sh ];
          then
            logger -s "Starting Tomcat"
            /bin/su -l ${TOMCAT_USER} -c "$CATALINA_HOME/bin/startup.sh -Dprocessname=${PROCESS_NAME}"
            PID=`ps -eaf|grep processname=${PROCESS_NAME}|grep -v grep|awk '{print $2}'`
            RETVAL=$?
            [ $RETVAL = 0 ] && touch ${lockfile}
            [ $RETVAL = 0 ] && echo "${PID}" > ${pidfile}
        fi
        ;;
 stop)
        PID=`pidofproc -p ${pidfile} ${PROCESS_NAME}`
        ## if PID valid run shutdown.sh
        if ;then
            logger -s "${PROCESS_NAME} is not running."
            exit;
        fi

        if ;
          then
            logger -s "Stopping Tomcat"
            /bin/su -l ${TOMCAT_USER} -c "$CATALINA_HOME/bin/shutdown.sh"
            RETVAL=$?
            [ $RETVAL = 0 ] && rm -f ${lockfile}
            [ $RETVAL = 0 ] && rm -f ${pidfile}
        fi
        ;;
 status)
        status -p ${pidfile} ${PROCESS_NAME}
        RETVAL=$?
        ;;
 restart)
         $0 stop
         $0 start
         ;;
version)
        if [ -f $CATALINA_HOME/bin/version.sh ];
          then
            logger -s "Display Tomcat Version"
            /bin/su -l ${TOMCAT_USER} -c "$CATALINA_HOME/bin/version.sh"
            RETVAL=$?
        fi
        ;;
 *)
         echo $"Usage: $0 {start|stop|restart|status|version}"
        exit 1
        ;;
esac
exit $RETVAL

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mika Slepinin, 2016-12-09
@mikalaikaia

I redesigned the script so that now it stops everything and starts it as it should, without doubling the processes. The 20 second timeout is needed because the application (.war) is quite heavy and Tomcat needs time to exit gracefully.

#!/bin/bash
#
# tomcat
#
# chkconfig: 345 96 30
# description:  Start up the Tomcat servlet engine.
#
# processname: java
# pidfile: /var/run/tomcat.pid
#
### BEGIN INIT INFO
# Provides: tomcat
# Required-Start: $network $syslog
# Required-Stop: $network $syslog
# Should-Start: distcache
# Short-Description: start and stop Apache HTTP Server
# Description: implementation for Servlet 2.5 and JSP 2.1
## END INIT INFO

# Source function library.
. /etc/init.d/functions

## tomcat installation directory
PROCESS_NAME=tomcat

CATALINA_HOME="/tomcat"

## run as a diffent user
TOMCAT_USER=tomcat

##  Path to the pid, runnning info file
pidfile=${PIDFILE-/var/run/${PROCESS_NAME}.pid};
lockfile=${LOCKFILE-/var/lock/subsys/${PROCESS_NAME}};

RETVAL=0

SHUTDOWN_WAIT=20

case "$1" in
  start)
    PID=`pidofproc -p ${pidfile} ${PROCESS_NAME}`
    if ; then
      logger -s "${PROCESS_NAME} (pid ${PID}) is already running."
      exit;
    fi
    if [ -f $CATALINA_HOME/bin/startup.sh ];
    then
      logger -s "Starting Tomcat"
      /bin/su -l ${TOMCAT_USER} -c "$CATALINA_HOME/bin/startup.sh -Dprocessname=${PROCESS_NAME}"
      PID=`ps -eaf|grep processname=${PROCESS_NAME}|grep -v grep|awk '{print $2}'`
      RETVAL=$?
      [ $RETVAL = 0 ] && touch ${lockfile}
      [ $RETVAL = 0 ] && echo "${PID}" > ${pidfile}
    fi
  ;;
  stop)
    PID=`pidofproc -p ${pidfile} ${PROCESS_NAME}`
    ## if PID valid run shutdown.sh
    if ;then
      logger -s "${PROCESS_NAME} is not running."
      exit;
    fi
    
    if ;
    then
      logger -s "Stopping Tomcat"
      /bin/su -l ${TOMCAT_USER} -c "$CATALINA_HOME/bin/shutdown.sh"
      RETVAL=$?
      [ $RETVAL = 0 ] && rm -f ${lockfile}
      [ $RETVAL = 0 ] && rm -f ${pidfile}
      
      let kwait=$SHUTDOWN_WAIT
      count=0;
      until [ `ps -p $PID | grep -c $PID` = '0' ] || [ $count -gt $kwait ]
      do
        logger -s "Waiting for processes ${PROCESS_NAME} (pid ${PID}) to exit.";
        sleep 1
        let count=$count+1;
      done
    fi
    
    if [ $count -gt $kwait ]; then
      logger -s "killing processes ${PROCESS_NAME} (pid ${PID}) didn't stop after $SHUTDOWN_WAIT seconds"
      logger -s "Terminating ${PROCESS_NAME} (pid ${PID})"
      kill -9 $PID
    fi
  ;;
  status)
    status -p ${pidfile} ${PROCESS_NAME}
    RETVAL=$?
  ;;
  restart)
    $0 stop
    $0 start
  ;;
  version)
    if [ -f $CATALINA_HOME/bin/version.sh ];
    then
      logger -s "Display Tomcat Version"
      /bin/su -l ${TOMCAT_USER} -c "$CATALINA_HOME/bin/version.sh"
      RETVAL=$?
    fi
  ;;
  *)
    echo $"Usage: $0 {start|stop|restart|status|version}"
    exit 1
  ;;
esac
exit $RETVAL

S
sim3x, 2016-12-06
@sim3x

Judging by mana

SYNOPSIS

       checkproc  [-v]  [-L]  [-k]  [-p  pid_file] [-c root] [-z]
       /full/path/to/executable

       checkproc [-v] [-k] [-n] name_of_kernel_thread

       pidofproc [-k] basename_of_executable


DESCRIPTION

       checkproc checks for running processes that use the speci­
       fied executable.

you need checkproc and /full/path/to/executable or `which ${PROCESS_NAME}`
You can also just do
killall -15 ${PROCESS_NAME}
sleep 1
pgrep ${PROCESS_NAME} && { echo "what a pain"; killall -9 ${PROCESS_NAME} }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question