Reputation: 1834
I have a Java app and I want to create a system service to execute that jar at boot. I know how to do it for upstart and SysV but I'm having troubles with systemd.
The difficult part is that I need that the jar be executed from within the directory where it resides, so that it pick up application.properties
next to it, and also some directories that are relative to the jar location.
For example, assume the following structure:
+-var
+-myapp
+-app.jar
+-app.log
+-application.properties
+-certificate.jks
+-images
+-img1.jpg
The way I want to execute the jar is:
$ cd /var/myapp
$ java -jar app.jar
So as I'm in the myapp directory when executing the jar, it logs to app.log
, it pick up application.properties
and reads the certificate.jks
. Also it could find the directory images
.
I've try this as unit file for systemd
[Unit]
Description=App Description
After=network.target
[Service]
Type=simple
User=sebasira
WorkingDirectory=/var/myapp
ExecStart=/usr/bin/java -jar app.jar
SuccessExitStatus=143
Restart=on-failure
[Install]
WantedBy=multi-user.target
It runs the jar file, but not from within /var/myapp
How can I solve it?
Upvotes: 1
Views: 319
Reputation: 1285
Make a shell script like in /var/myapp:
#!/bin/sh
# runMyJar.sh
java -Djava.awt.headless=true -jar /var/myapp/app.jar
Make it executable of course.
Then configure it as service to fork at boot:
[Unit]
Description=My App Jar
[Service]
Type=forking
ExecStart=/var/myapp/runMyJar.sh
[Install]
WantedBy=multi-user.target
Add other systemd options as you wish.
The jar file should execute with /var/myapp as default home or workspace directory.
EDIT (based on @sebasira comment)
The following .sh
script may also work with this systemd configuration:
#!/bin/sh
# runMyJar.sh
cd /var/myapp/;java -jar app.jar
Upvotes: 1