Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

systemd

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?

like image 262
Sirish Renukumar Avatar asked Mar 24 '17 13:03

Sirish Renukumar


People also ask

How do I delay start of systemd services?

Create a timer trigger to start service 1 minute after the system boot. Notice, that the timer uses the same service name. Reload systemd configuration. Disable service at boot.

How do you check if a systemd service is running?

To check a service's status, use the systemctl status service-name command. I like systemd's status because of the detail given. For example, in the above listing, you see the full path to the unit file, the status, the start command, and the latest status changes.

Does systemd enable start service?

systemctl is the systemd command for controlling how services start on a Linux system. A service can be enabled, disabled, or masked, and it can be configured to start at boot, on demand, manually, or prevented from starting under any circumstances. Enabling a service means it will start at boot.

Can a systemd timer directly run a script?

If you have a Bash script that needs to run periodically, you can run it using a crontab entry. But you can also have it invoked by Systemd using systemd. timer. Furthermore, you can run Systemd services as user-level services instead of the typical system-level service for even further isolation.


2 Answers

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

[Service] ExecStartPre=/bin/sleep 30 
like image 197
Ortomala Lokni Avatar answered Sep 28 '22 21:09

Ortomala Lokni


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

So for example, to wait for 1 minute after boot-up before starting your foo.service, create a foo.timer file in the same directory with the contents:

[Timer] OnBootSec=1min 

It is important that the service is disabled (so it doesn't start at boot), and the timer enabled, for all this to work (thanks to user tride for this):

systemctl disable foo.service systemctl enable foo.timer 

You can find quite a few more options and all information needed here: https://wiki.archlinux.org/index.php/Systemd/Timers

like image 23
mj3c Avatar answered Sep 28 '22 21:09

mj3c