![如何在 CentOS7 上将创建 pidfile 添加到旧的 sysv init 脚本中?](https://linux22.com/image/732526/%E5%A6%82%E4%BD%95%E5%9C%A8%20CentOS7%20%E4%B8%8A%E5%B0%86%E5%88%9B%E5%BB%BA%20pidfile%20%E6%B7%BB%E5%8A%A0%E5%88%B0%E6%97%A7%E7%9A%84%20sysv%20init%20%E8%84%9A%E6%9C%AC%E4%B8%AD%EF%BC%9F.png)
我有旧的 sysv init 脚本,用于在 CentOS7 上启动 tomcat
我知道:需要在 sysv 初始化脚本中创建 pid 文件。
脚本有创建 pid 文件的代码。
但 pid 文件未创建
如何在 CentOS7 上将创建 pidile 添加到旧的 sysv init 脚本中?
#!/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 说:
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.
谢谢!