How to provide a delay before starting a service in systemd?

I have a service that depends on whether Kassandra is suitable and the cluster is ready and ready.

To make sure the dependency order is satisfied, I have the following file

[Unit] Requires=cassandra.service After=cassandra.service [Service] Environment=JAVA_HOME=/usr/java/jre ExecStart=@bringup.instance.path @/webapps/bringup-app/bin/bringup TimeoutStartSec=0 ExecStop= PIDFile=@bringup.instance.path @/logs/bringup.pid Restart=always [Install] WantedBy=multi-user.target 

How can I guarantee that the appup-app process waits for 30 seconds before it tries to start? Currently, although it started after Cassandra, I noticed that the Cassandra cluster has not yet been completed, and therefore any attempt from the application-application to connect to Cassandra as part of the launch failed.

So I want to add a delay. Is this possible through a device file?

+27
source share
5 answers

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

 [Service] ExecStartPre=/bin/sleep 30 
+39
source

You can create a .timer file to control the execution of the .service file.

So, for example, to wait 1 minute after loading, before running foo.service , create the file foo.timer in the same directory with the contents:

 [Timer] OnBootSec=1min 

It is important that the service is disabled (so that it does not start at boot), and the timer is turned on so that it all works (thanks to user tride for this):

 systemctl disable foo.service systemctl enable foo.timer 

You can find several more options and all the necessary information here: https://wiki.archlinux.org/index.php/Systemd/Timers

+14
source

This answer to the super user, I think, is the best answer. From https://superuser.com/a/573761/67952

"But since you asked for a method without using Before and After, you can use:

 Type=idle 

which as man systemd.service

Downtime is very similar to downtime; however, the actual execution of the service program is delayed until all active jobs are sent. This can be used to avoid alternating the output of shell services with the status output on the console. Please note that this type is only useful for improving console output, it is not useful as a general tool for organizing modules, and the effect of this type of service depends on a waiting time of 5 s, after which the service program is still called. "

+5
source

Instead of editing the delivery service, add a delay after starting to the service on which it depends. Edit cassandra.service like this:

 ExecStartPost=/bin/sleep 30 

Thus, the added hibernation should not slow down restarts of the started services that depend on it (although it slows down its own startup, maybe this is desirable?).

+4
source

systemd way to do this is to make the process "talk" when it is configured, for example, by opening a socket or sending a notification (or exiting the parent script). Which, of course, is not always easy, especially with third-party materials: |

You could do something inline like

 ExecStart=/bin/bash -c '/bin/start_cassandra &; do_bash_loop_waiting_for_it_to_come_up_here' 

or a script that does the same thing. Or put do_bash_loop_waiting_for_it_to_come_up_here in ExecStartPost

Or create a helper .service that waits for it to appear, so the helper service depends on cassandra and waits for it to start, then your other process may depend on the helper service.

(You may also need to increase TimeoutStartSec from the 90s by default)

0
source

Source: https://habr.com/ru/post/1265873/


All Articles