2

I have installed distribution packages of Redis and Tomcat on Ubuntu 18.04. I'm trying to start the Tomcat only after a successful start of Redis, but so far no luck.

  • The tomcat8 package has sysv init script in /etc/init.d/tomcat8 and I see the generated /run/systemd/generator.late/tomcat8.service.

  • Redis-server package has the service unit in /lib/systemd/system/redis-server.service

I have created a symlink to redis-server in these directories:

ln -s /lib/systemd/system/redis-server.service  /etc/systemd/system/tomcat8.service.requires/redis-server.service
ln -s /lib/systemd/system/redis-server.service  /etc/systemd/system/tomcat8.service.after/redis-server.service

However if I purposedly provoke that Redis fails to start, Tomcat starts anyway. I want Tomcat to not to start in case Redis fails.

I see this output of the Tomcats' service status command, with "Dependency failed for LSB", but Tomcat started anyway:

root@elkarel:/home/elkarel# service tomcat8 status
  tomcat8.service - LSB: Start Tomcat.
   Loaded: loaded (/etc/init.d/tomcat8; generated)
   Active: active (running) since Mon 2019-07-01 14:57:51 CEST; 2min 7s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 690 ExecStart=/etc/init.d/tomcat8 start (code=exited, status=0/SUCCESS)
    Tasks: 44 (limit: 2340)
   CGroup: /system.slice/tomcat8.service
           └─737 /usr/lib/jvm/java-8-oracle/bin/java -Djava.util.logging.config.file=/var/lib/tomcat8/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.awt.head

Jul 01 14:57:45 elkarel systemd[1]: Starting LSB: Start Tomcat....
Jul 01 14:57:45 elkarel systemd[1]: Dependency failed for LSB: Start Tomcat..
Jul 01 14:57:45 elkarel systemd[1]: tomcat8.service: Job tomcat8.service/start failed with result 'dependency'.
Jul 01 14:57:46 elkarel tomcat8[690]:  * Starting Tomcat servlet engine tomcat8
Jul 01 14:57:51 elkarel tomcat8[690]:    ...done.

I execute systemctl daemon-reload every time I make any change.

I didn't alter the package files and I don't want to, but now I doubt if is it even possible to declare such dependency only with symlinks.

1 Answer 1

3

There is no .after/, only .wants/ and .requires/. As a result, your both services are queued to start at the same time. The failing Requires= dependency should cause tomcat8 to be stopped again, but ... SysV generated services are weird when it comes to that.

You need to add the additional settings to a .conf file inside <unit>.d/ instead (e.g. by using systemctl edit tomcat8):

# /etc/systemd/system/tomcat8.service.d/deps.conf

[Unit]
Requires=redis.service
After=redis.service

Note: You should avoid using .wants/ or .requires/ for general customization, because symlinks in these dirs may be unexpectedly removed during "systemctl enable/disable". Create .conf files or full unit overrides for everything else.

1
  • Thanks @grawity for the answer! It works exactly as you put it. Only in case of ubuntu 18.04, the Redis service is called "redis-server.service" BR
    – elkarel
    Commented Jul 15, 2019 at 9:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .