How to ensure that there is a delay before a service is started in systemd?

Sirish Renukumar picture Sirish Renukumar · Mar 24, 2017 · Viewed 112.6k times · Source

I am having a service that depends on Cassandra coming up gracefully and the cluster being up and ready.

To ensure that the dependency order is met, I have the following unit file

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

[Service]
Environment=JAVA_HOME=/usr/java/jre
[email protected]@/webapps/bringup-app/bin/bringup
TimeoutStartSec=0
ExecStop=
[email protected]@/logs/bringup.pid
Restart=always

[Install]
WantedBy=multi-user.target

How do I ensure that the bringup-app process waits for 30 seconds before it attempts to start up? Currently although it is started after Cassandra, I have noticed that the Cassandra cluster is not up yet and hence any attempt from the bringup-app to connect to Cassandra as part of startup fails.

I therefore want to add a delay. Is that possible via the unit file?

Answer

Ortomala Lokni picture Ortomala Lokni · Jun 24, 2017

You can run the sleep command before your ExecStart with ExecStartPre :

[Service]
ExecStartPre=/bin/sleep 30