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?
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.
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.
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.
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.
You can run the sleep command before your ExecStart with ExecStartPre :
[Service] ExecStartPre=/bin/sleep 30
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With