5

Sometimes a service will start and fork - giving systemd the illusion that it's ready - even though it hasn't finished "warming up". In this particular instance I'm using ApacheDS to provide LDAP services. When this unit is started, checking "systemctl status apacheds" will show it's running, along with the single log line:

Apr 04 15:34:33 daisy systemd[1]: Started Apache Directory Server.

But...it's not serving yet. Until a port test (like "lsof -i :389" or "netstat -pan | grep :389 | grep LISTEN") reveals that there's an active listener there's no available LDAP.

Whether it should do so or not - this takes about 2 minutes to fully startup. My question isn't whether or not ApacheDS is broken or should be replaced - it's how to deal with slow initializing services in systemd.

Is there a way of putting such a test into systemd, either to tell it to wait until valid to show the apacheds service as started, or giving it as a pre-condition for dependent services, without having them simply fail and still refuse to start?

Here's a working test script:

#!/bin/bash
TRIES=30
WAIT=10

while /bin/netstat -an | /bin/grep \:10389 | /bin/grep LISTEN ; [ $? -ne 0 ]; do
    let TRIES-=1
    if [ $TRIES -gt 1 ]; then
            sleep $WAIT
    fi
done
Daniel Miller
  • 199
  • 3
  • 7
  • See also https://unix.stackexchange.com/questions/247755/can-i-delay-a-systemd-script-from-running-at-boot/529218#529218 – rogerdpack Jul 09 '19 at 19:03

1 Answers1

3

You can add an ExecStartPost=foo to the unit file where foo is a script that checks (and rechecks) until the service is available.

Edit: you may need to increase TimeoutStartSec as well. The default is 90s.

Mark Wagner
  • 17,764
  • 2
  • 30
  • 47
  • That's...exactly what I needed. And to complete the answer for the archive - while my tests are valid they can't be used directly in systemd without encapsulating in a script (as you inferred already). So I created a script: `/usr/local/bin/isldap.sh #!/bin/sh netstat -pan | grep :389 | grep LISTEN` and reference that via ExecStartPost – Daniel Miller Apr 04 '18 at 23:41
  • Actually - I jumped on this too soon. ExecStartPost will run - and fail - and now the service is shown as failed and won't start because systemd will kill it. So this isn't the answer yet - or my script is wrong. – Daniel Miller Apr 05 '18 at 00:23
  • It would help if I knew how to write scripts properly...I'm placing the working script back into my question (since I'm not sure how else to get the formatting to appear correctly here). – Daniel Miller Apr 05 '18 at 00:51