If you run systemctl start youservice.service
, systemd doesn't wait until your service is actually started. This can cause problems if your application have slow initialization process. In this case systemd considers your application as started, but actually starting process is in progress.
This library allows you to notify systemd system about your application is ready/started and systemd will wait until it happen. It uses linux systemd-notify tool to do it.
The library is lightweight and doesn't have any dependencies on logging, JNA, JNI native code. It is pure Java solution.
Two steps are required.
Add following lines to your service unit file.
[Service]
Type=notify
NotifyAccess=all
You can also configure TimeoutStartSec. Default value is 90s.
Add maven dependency to your pom.xml
.
<dependency>
<groupId>com.pixonic</groupId>
<artifactId>simple-systemd-service</artifactId>
<version>0.1</version>
</dependency>
And call it when your app is ready.
public static void main(String[] args) {
// ... your slow initialization code goes here ...
//notify systemd about we are ready
Systemd.sendReady();
}
Java 8 is required.
mvn package
This library doesn't support Spring Boot.
You should run your app as root because of a bug in systemd.