Step 1. Add the JitPack repository to your build file
Add it in your root settings.gradle at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
Add it in your settings.gradle.kts at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url = uri("https://jitpack.io") }
}
}
Add to pom.xml
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Add it in your build.sbt at the end of resolvers:
resolvers += "jitpack" at "https://jitpack.io"
Add it in your project.clj at the end of repositories:
:repositories [["jitpack" "https://jitpack.io"]]
Step 2. Add the dependency
dependencies {
implementation 'com.github.taimos:daemon-framework:v2.15'
}
dependencies {
implementation("com.github.taimos:daemon-framework:v2.15")
}
<dependency>
<groupId>com.github.taimos</groupId>
<artifactId>daemon-framework</artifactId>
<version>v2.15</version>
</dependency>
libraryDependencies += "com.github.taimos" % "daemon-framework" % "v2.15"
:dependencies [[com.github.taimos/daemon-framework "v2.15"]]
Little framework to create Java programs that behave like Linux daemons/Windows services.
There are some small steps to follow to create a Linux daemon with Java:
There are three different startup modes which can be specified with -DstartupMode=<mode>
:
The default mode is dev
. If you want to run the program in production mode make sure you start it with -DstartupMode=start
or -DstartupMode=run
.
In start mode the console logging is disabled.
In development mode the program's behavior will differ in some small points:
This method is called to start your program. It must be non blocking. If this method throws any Exception the DaemonFramework stops execution of the daemon.
This method is called on OS signal to stop your program. It must be non blocking. If this method throws any Exception the DaemonFramework stops execution of the daemon.
This method is called when the daemon completed startup successfully.
This method is called when the daemon completed shutdown successfully. Immediatly after this method System.exit(0) is called.
This method is called when the daemon received shutdown signal.
This method is called when the daemon aborts execution. Immediatly after this method System.exit(1) is called.
This method is called when the daemon received the OS signal USR2. You can do what you want within this method.
This method is called when an exception occurs in DaemonStarter. It provides the current phase and the thrown exception.
This method is called to obtain the daemon properties. All properties provided in this map will be available via System.getProperty later on. The properties found in de.taimos.daemon.DaemonProperties are used by the daemon framework itself and should be filled.
In your main method just call startDaemon to run the daemon framework. You have to provide a service name and an instance of your subclass of DaemonLifecycleAdapter;
DaemonStarter.startDaemon("my-service-name", new MyLifecycleAdapter());
You just have to create a copy of initscript from the projects linux folder and customize it. For basic usage it is enough to change the variables on top of the script
The name of the daemon
The name and location of the PID file. It defaults to /var/run/<daemonname>
The location of the daemon program. It defaults to /opt/<daemonname>
The user under which the daemon is run. The init script then calls java with su -c <command> RUNUSER
The name of the jarfile as it is found in $RUNDIR. The script calls java -jar <jarname>.
Some Java options like memory size etc.
You just have to create a copy of the contents of the windows folder and customize it. For basic usage it is enough to change the variables on top of the script install.bat and to rename the service.exe file.
set svcName=CloudConductor set svcFullName="CloudConductor Server" set svcDesc="Cinovo CloudConductor" set svcJAR=de.cinovo.cloudconductor.cloudconductor-server.jar set svcStarter=de.cinovo.cloudconductor.server.ServerStarter
The short name of the service (no spaces)
The full name of the service
The description of the service
The name of the jarfile as it is found in the installation folder.
The java main class to start the service