1

I wrote the following script /etc/init.d/tomcat in order to manage my manually-installed Apache Tomcat server as a service:

#!/bin/sh
#
# /etc/init.d/tomcat -- startup script for the Tomcat servlet engine
#
### BEGIN INIT INFO
# Provides:          tomcat
# Required-Start:
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start Tomcat.
# Description:       Start the Tomcat servlet engine.
### END INIT INFO

export CATALINA_HOME="/opt/tomcat"
case "$1" in
start)
  if [ -f $CATALINA_HOME/bin/startup.sh ];
  then
    $CATALINA_HOME/bin/startup.sh
  fi
  ;;
stop)
  if [ -f $CATALINA_HOME/bin/shutdown.sh ];
  then
    $CATALINA_HOME/bin/shutdown.sh
  fi
  ;;
*)
  echo "Usage: $0 {start|stop}"
  exit 1
  ;;
esac

Then, I executed the following commands and now the service is working fine:

sudo chmod 755 /etc/init.d/tomcat
sudo update-rc.d tomcat defaults
sudo systemctl daemon-reload

As you can see, the tomcat script supports only start and stop, no restart case is present, but I noticed that if I launch sudo service tomcat restart, the service is effectively restarted. How is it possible? I find no way to see which commands are issued behind the scenes.

As a counterproof, if I issue sudo service tomcat qwerty I get Usage: /etc/init.d/tomcat {start|stop} Thanks.

3

You are using old style SysV init scripts but since Raspbian Jessie SysV init isn't used anymore. It is replaced by systemd. Only for compatibility systemd emulates SysV so your init.d/script in practice is a systemd unit and any systemd unit can be restarted by default. You can verify it with:

rpi ~$ systemctl status tomcat.service

This should give you in the second line:

Loaded: loaded (/etc/init.d/tomcat; generated)

The important word is "generated". You can look what systemd has done with your old service to get a unit with:

rpi ~$ systemctl cat tomcat.service

This is done by systemd-sysv-generator. Look at man systemd-sysv-generator and more general at man systemd.generator. Just for new services it is really time to switch over direct to systemd. Your init.d/service as systemd unit should look similar to this:

[Unit]
Description=Tomcat servlet engine
Wants=network.target
Before=network.target

[Service]
Type=simple
ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh

[Install]
WantedBy=multi-user.target

Probably this has to be tuned to your needs. I don't know what the scripts do. Maybe you have to use Type=forking and select other dependencies. Look at man systemd.unit and man systemd.service.

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.