0

I'm currently setting up a RHEL 7.8 tomcat service and I'm having trouble with the service and systemctl commands.

Context: the following file was set up on /etc/init.d/tomcat:

#!/bin/bash
#
#
# chkconfig: 2345 20 80
# description:  Start up the Tomcat servlet engine.

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


RETVAL=$?
CATALINA_HOME="<tomcat 9 folder location>"
JAVA_HOME="/usr/java/jdk1.8.0_251"

case "$1" in
 start)
        if [ -f $CATALINA_HOME/bin/startup.sh  ];
          then
            echo $"Starting Tomcat"
            /bin/su saveweb -c "JAVA_HOME=$JAVA_HOME $CATALINA_HOME/bin/startup.sh"
        fi
        ;;
 stop)
        if [ -f $CATALINA_HOME/bin/shutdown.sh ];
          then
            echo $"Stopping Tomcat"
            /bin/su saveweb -c "JAVA_HOME=$JAVA_HOME $CATALINA_HOME/bin/shutdown.sh 15 -force"
        fi
        ;;
 *)
        echo $"Usage: $0 {start|stop}"
        exit 1
        ;;
esac

exit $RETVAL

And this is the /run/systemd/generator.late/tomcat.service file:

# Automatically generated by systemd-sysv-generator

[Unit]
Documentation=man:systemd-sysv-generator(8)
SourcePath=/etc/rc.d/init.d/tomcat
Description=SYSV: Start up the Tomcat servlet engine.
Before=runlevel2.target
Before=runlevel3.target
Before=runlevel4.target
Before=runlevel5.target
Before=rhnsd.service
After=network-online.target
After=network.service

[Service]
Type=forking
Restart=no
TimeoutSec=5min
IgnoreSIGPIPE=no
KillMode=process
GuessMainPID=no
RemainAfterExit=yes
ExecStart=/etc/rc.d/init.d/tomcat start
ExecStop=/etc/rc.d/init.d/tomcat stop

(I'll be honest here, I don't know how or who generated these files. I'm working with what was given to me)

This works fine for most use cases (starting service on system boot, using systemctl start tomcat and systemctl stop tomcat etc). We also set tomcat in a way that its setenv.sh defines a CATALINA_PID variable, so that starting/stopping the tomcat server directly through its scripts or through systemctl will work.

This is all because some users might, due to human error, start the tomcat service twice. We definitely don't want two tomcat instances.

So while testing it out, everything seemed to be working properly. When I use systemctl start tomcat twice, only the first instance of the tomcat service will be up - as expected.

However, it was pointed out that if someone uses service tomcat start while the previous service was online, the service program will first stop and then start the service.

So, in short:

# this works fine!
systemctl start tomcat
systemctl start tomcat # effectivelly ignored, maybe some log saying that there was a previous instance online. Good!
service start tomcat
service start tomcat # logs show that "stop" was called and then the service was started again. not good.

I'm super confused because everywhere I looked says that service is just a wrapper for systemctl. So what gives? Why does service have a different behavior than systemctl in this situation, and what should be done to fix it so that they are consistent?

Any help is welcome.

Badashi
  • 103
  • 2

1 Answers1

2

service is indeed a wrapper, but the way service start works in the presence of /etc/init.d/tomcat file is by stopping the existing instance, then starting another.

You can see how it works by cat /sbin/service - it's really a small script, and it states the reasoning to having to stop the service:

LSB daemons that dies abnormally in systemd looks alive in systemd's eyes due to RemainAfterExit=yes lets reap them before next start

The file /run/systemd/generator.late/tomcat.service is generated by SystemD itself from the legacy startup script /etc/init.d/tomcat. The tomcat.service is required for SystemD to be able to launch the service at boot time, or when you run it via systemctl start, etc.

If you must have the service behave exactly as systemctl, the best route would be having a "real" SystemD unit file for your Tomcat instance.

The tomcat package from repositories does come with a SystemD unit (note the /usr/lib/systemd/system/tomcat.service in the files section), so in all the likelihood you have installed Tomcat in an exotic/non-standard way.

Danila Vershinin
  • 5,286
  • 5
  • 17
  • 21