0

I have a custom service managed by an init script in /etc/init.d and, after upgrading to CentOS 7, with systemd, 'systemctl restart service' is not working correctly. I need to execute two times 'systemctl restart service' to make it restart (the first time, it just stops it, it does not start it after stopping it). I don't know exactly what's happening but if I stop the service using '/etc/init.d/service stop', the process is killed and is not shown in 'ps aux', but 'systemctl status service' says it is "active".

This is my init script in /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

And this is my .service file in /usr/lib/systemd/system:

# 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

Hopping somebody can help. Thanks.

  • Can you post the relevant systemd log entries? – Mark Wagner Nov 15 '17 at 20:06
  • Why not remove the init script and let systemd manage the service directly? ExecStart=$JAVA_HOME/bin/java blablablablabla – Alex Forbes Nov 16 '17 at 15:29
0

The issue has been solved adding the PIDFile directive in the .service file.

  • You should also a lock file in /var/subsys/lock/ or somesuch. Otherwise you will find it does get runn at shutdown. Not sure if this is still relevant for EL7 though. Compare with others. – Cameron Kerr Nov 16 '17 at 15:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.