我有一个由 /etc/init.d 中的 init 脚本管理的自定义服务,在升级到 CentOS 7 后,使用 systemd,“systemctl restart service”无法正常工作。我需要执行两次“systemctl restart service”才能使其重新启动(第一次,它只是停止它,停止后不会启动它)。我不知道到底发生了什么,但如果我使用“/etc/init.d/service stop”停止服务,该进程将被终止,并且不会显示在“ps aux”中,但“systemctl status service”显示它处于“活动”状态。
这是我在 /etc/init.d 中的初始化脚本:
#!/bin/bash
#
# Startup script for the service
# chkconfig: 2345 80 20
# description: a service
PID_FILE=/var/run/pidfile.pid
LOG_FILE=/var/log/serviceslogs
#Necessary environment variables
export JAVA_HOME="/usr/java/latest"
#export JAVA_HOME="/root/java-8-oracle"
if test \! -d "${JAVA_HOME}"; then
echo "$0: the JAVA_HOME exported value is not defined correctly"
exit 2
fi
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
start() {
if [ -e $PID_FILE ]
then
checkpid `cat /var/run/pidfile.pid`
res=$?
if [ $res -eq 0 ]
then
echo "Standalone service is already running!"
return;
fi
fi
$JAVA_HOME/bin/java blablablablabla > $LOG_FILE 2>&1 & echo $! > $PID_FILE
echo $! > $PID_FILE
echo "Starting Standalone service "
}
stop() {
echo "Shutting down Standalone service: "
checkpid `cat /var/run/pidfile.pid`
res=$?
if [ $res -eq 0 ]
then
kill -9 `cat /var/run/pidfile.pid`
rm `cat /var/run/pidfile.pid`
echo "Standalone service stopped."
else
echo "Standalone service is not running!"
fi
}
status() {
checkpid `cat /var/run/pidfile.pid`
res=$?
if [ $res -eq 0 ]
then
echo "Standalone service is running."
else
echo "Standalone service is stopped."
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
status
;;
*)
echo "Usage: service {start|stop|restart|status}"
exit 1
esac
这是我在 /usr/lib/systemd/system 中的 .service 文件:
# Automatically generated by systemd-sysv-generator
[Unit]
Documentation=man:systemd-sysv-generator(8)
SourcePath=/etc/rc.d/init.d/service
Description=SYSV: A service
Before=runlevel2.target
Before=runlevel3.target
Before=runlevel4.target
Before=runlevel5.target
Before=shutdown.target
Before=jexec.service
After=network-online.target
After=network.service
After=mysql.service
Conflicts=shutdown.target
[Service]
Type=forking
Restart=no
TimeoutSec=5min
IgnoreSIGPIPE=no
KillMode=process
GuessMainPID=no
RemainAfterExit=yes
ExecStart=/etc/rc.d/init.d/service start
ExecStop=/etc/rc.d/init.d/service stop
希望有人能帮忙。谢谢。
答案1
PIDFile
通过在文件中添加指令,问题已得到解决.service
。