0

I have old sysv init script for start tomcat on CentOS7

I know: need create pid file in sysv init script.

Script have code for create pid file.

But pid file not created

How add create pidile to old sysv init script on CentOS7?

#!/bin/bash
#
# tomcat      This shell script takes care of starting and stopping Tomcat
#
# chkconfig: - 80 20
# pidfile: /var/run/tomcat.pid
#
### BEGIN INIT INFO
# Provides: tomcat
# Required-Start: $network $syslog
# Required-Stop: $network $syslog
# Default-Start:
# Default-Stop:
# Description: Release implementation for Servlet 2.5 and JSP 2.1
# Short-Description: start and stop tomcat


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

prog=tomcat
# Timeout for waiting stopping $prog
timeout=5
status_pattern='catalina.home=/var/lib/tomcat7'
STARTUP_LOG=/var/log/tomcat7/startup.out
export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-1.el7_6.x86_64/jre
pidfile=/var/run/tomcat.pid

get_pid () {
    PID=`ps -ef | grep ${status_pattern} | grep -v grep | sed 's/\s\+/ /g' | cut -f 2 -d ' '`
}

start () {
    if [ "$EUID" != "0" ]; then
        echo "User has insufficient privilege."
        exit 4
    fi

    # Check before running
    get_pid
    if [[ ! -z ${PID} ]]; then
        echo -n $"$prog is already running: ${PID}"
        echo
        exit 0
    fi

    echo -n $"Starting $prog: "
    runuser -s /bin/bash tomcat /var/lib/tomcat7/bin/startup.sh 2>&1 > $STARTUP_LOG && success || failure $"$prog start"
    RETVAL=$?
    if [ $RETVAL -eq 0 ]; then
       ps aux | grep $exec | grep -v grep | tr -s " " | cut -d " " -f2 > $pidfile
    fi
    echo
}

stop () {
    if [ "$EUID" != "0" ]; then
        echo "User has insufficient privilege."
        exit 4
    fi

    # Check before stopping
    get_pid
    if [[ -z ${PID} ]]; then
        echo -n $"$prog is not running"
        echo
        return 0
    fi

    echo -n $"Stopping $prog: "
    runuser -s /bin/bash tomcat /var/lib/tomcat7/bin/shutdown.sh 2>&1 >> $STARTUP_LOG

    # Check progress of the stopping
    ps -p ${PID} 2>&1 > /dev/null
    RUNNING=$?
    if [[ ${RUNNING} -eq 0 ]]; then
        for i in `seq ${timeout}`; do
            echo
            echo -n "Waiting for stopping..."
            sleep 1
            ps -p ${PID} 2>&1 > /dev/null
            if [[ $? -ne 0 ]]; then
                STOPPED="true"
                break
            fi
        done
        if [[ ${STOPPED} != "true" ]]; then
            failure $"Can not stopped ${prog} softly"
            echo
            echo "Forcibly killed ${PID}"
            kill -9 ${PID}
            return 0
        fi
    if [ ${STOPPED} == "true" ]; then
      rm -f $pidfile
      success; echo
    fi
    fi
    success
    echo
}

status () {
    get_pid
    if [[ -z ${PID} ]]; then
        echo -n $"$prog is not running"
        echo
        return 7
    else
        echo -n $"$prog current PID is ${PID}"
        echo
        return 0
    fi
}

case "$1" in
  start)
    start
    ;;

  stop)
    stop
    ;;

  status)
    status
    ;;

  restart)
    stop
    start
    ;;

  *)
    echo $"Usage: $0 {start|stop|restart|status}"
    exit 2

esac

systemctl status tomcat7.service say:

systemd[1]: Starting LSB: start and stop tomcat...
runuser[11651]: pam_unix(runuser:session): session opened for user tomcat by (uid=0)
tomcat7[11642]: [67B blob data]
tomcat7[11642]: Try 'grep --help' for more information.
systemd[1]: Failed to read PID from file /var/run/tomcat.pid: Invalid argument
systemd[1]: tomcat7.service start operation timed out. Terminating.
systemd[1]: Failed to start LSB: start and stop tomcat.
systemd[1]: Unit tomcat7.service entered failed state.
systemd[1]: tomcat7.service failed.

Thanks!

Anton Patsev
  • 111
  • 4
  • It seems `$exec` in function start in line `ps aux | grep $exec | grep -v grep | tr -s " " | cut -d " " -f2 > $pidfile`is not set (same as calling `grep` without parameter). Do you have any messages in `/var/log/tomcat7/startup.out`? – Freddy Jan 23 '19 at 08:41
  • I think the line `ps aux | grep $exec`... should like the same or nearly the same as the line in function `get_pid()`. In this function "`grep ${status_pattern}`" is used to find the string 'catalina.home=/var/lib/tomcat7' in the list of running processes. – Freddy Jan 23 '19 at 09:10

0 Answers0