Run a spring boot jar as a linux service with systemd
May
1st,
2019
Generate a new spring boot camel project by running a maven archetype (maven settings.xml supplied at the end of this article if you need the repositories):
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Create a new user and copy the jar to a runnable location
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Create a new systemd configuration file at /etc/systemd/system/simple-camel-logger.service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
You should now be able to use systemctl to enable, start, and stop the service:
[user@localhost somedir]$ systemctl enable simple-camel-logger.service
[user@localhost somedir]$ systemctl start simple-camel-logger.service
[user@localhost somedir]$ systemctl status simple-camel-logger.service
● simple-camel-logger.service - simple-camel-logger spring boot application
Loaded: loaded (/etc/systemd/system/simple-camel-logger.service; disabled; vendor preset: disabled)
Active: active (running) since Wed 2019-05-01 09:29:19 BST; 8s ago
Main PID: 8968 (java)
Tasks: 44 (limit: 4915)
Memory: 556.1M
CGroup: /system.slice/simple-camel-logger.service
└─8968 /usr/bin/java -jar /var/springboot/simple-camel-logger-1.0.jar
May 01 09:29:25 somebox java[8968]: 09:29:25.356 [main] INFO o.a.camel.spring.SpringCamelContext - StreamCaching is not in use. If using streams then its reco>
May 01 09:29:25 somebox java[8968]: 09:29:25.382 [main] INFO o.a.camel.spring.SpringCamelContext - Route: route1 started and consuming from: timer://foo?perio>
May 01 09:29:25 somebox java[8968]: 09:29:25.383 [main] INFO o.a.camel.spring.SpringCamelContext - Total 1 routes, of which 1 are started
May 01 09:29:25 somebox java[8968]: 09:29:25.384 [main] INFO o.a.camel.spring.SpringCamelContext - Apache Camel 2.21.0.fuse-720050-redhat-00001 (CamelContext:>
May 01 09:29:25 somebox java[8968]: 09:29:25.448 [main] INFO o.s.b.c.e.u.UndertowEmbeddedServletContainer - Undertow started on port(s) 8081 (http)
May 01 09:29:25 somebox java[8968]: 09:29:25.454 [main] INFO o.s.c.s.DefaultLifecycleProcessor - Starting beans in phase 0
May 01 09:29:25 somebox java[8968]: 09:29:25.464 [main] INFO o.s.b.a.e.jmx.EndpointMBeanExporter - Located managed bean 'healthEndpoint': registering with JMX>
May 01 09:29:25 somebox java[8968]: 09:29:25.512 [main] INFO o.s.b.c.e.u.UndertowEmbeddedServletContainer - Undertow started on port(s) 8080 (http)
May 01 09:29:25 somebox java[8968]: 09:29:25.515 [main] INFO com.codergists.Application - Started Application in 5.419 seconds (JVM running for 5.913)
May 01 09:29:26 somebox java[8968]: 09:29:26.393 [Camel (MyCamel) thread #1 - timer://foo] INFO route1 - >>> Hello World[user@localhost somedir]$ systemctl stop simple-camel-logger.service
Maven Repositories
Add the following to your ~/.m2/settings.xml file so maven knows how to resolve the fuse and redhat java artefacts.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters