How I start a jar correctly

Hi,
Finally I deploy a jar.
First I create a user and a group with home directory for this user with out login shell.
I put my jar file in this home and I create system service in, /etc/systemd/system/genedatabank.service, to start my application like this:

[Unit]
Description=GeneDataBank java server application
Wants=network-online.target
After=network-online.target

[Service]
User=genedatabank
Group=genedatabank
Restart=always
Type=simple
ExecStart=/usr/lib/jvm/java-17-openjdk-amd64/bin/java \
    -jar /opt/genedatabank/genedatabank.jar \
    --server.port=8090

[Install]
WantedBy=multi-user.target

If I start the application with systemctl start genedatabank.service application start but not create/have access to directory .jmix/work and if I start with sudo -u genedatabank java -jar genedatabank.jar --server.port=8090 in directory /opt/genedatabank application start and can create/have access to .jmix/work...

So finally:

root@server:/opt/genedatabank# sudo -u genedatabank java -jar genedatabank.jar --server.port=8090

is correct, and

root@server:/# sudo -u genedatabank java -jar /opt/genedatabank/genedatabank.jar --server.port=8090

is not correct

What is argument necessary to add at my command to start correctly?

Thank you!

LE. > Home directory is /opt/genedatabank

Hi,
I found a solution, I modified the service file:

root@server:~# cat /etc/systemd/system/genedatabank.service 
[Unit]
Description=GeneDataBank java server application
Wants=network-online.target
After=network-online.target

[Service]
User=genedatabank
Group=genedatabank
Restart=always
Type=simple
ExecStart=/opt/genedatabank/genedatabank.sh

[Install]
WantedBy=multi-user.target

and in home directory I add bash script

root@server:~# cat /opt/genedatabank/genedatabank.sh 
#!/bin/bash
(cd /opt/genedatabank;java -jar genedatabank.jar --server.port=8090)

evident marked executable.

I am sure exist another solution, but this work for me now.

WorkingDirectory entry can work for you.

See example here: https://www.baeldung.com/linux/systemd-service-working-directory

[Service]
WorkingDirectory=/usr/local
ExecStart=/usr/local/bin/getcwd.sh
1 Like

Thank you Alexander,

Now work great, this solution I like more because is elegant.

florin@server:~$ cat /etc/systemd/system/genedatabank.service 
[Unit]
Description=GeneDataBank java server application
# depend by network and postgres
Wants=network-online.target postgresql.service
# start after network  and postgres service start
After=network-online.target postgresql.service

[Service]
# run as user genedatabank (need to exist before)
User=genedatabank
# from group genedatabank (need to exist before)
Group=genedatabank
# restart always
Restart=always
# activation state
Type=simple
# working directory for user (in this case the home is /opt/genedatabank)
WorkingDirectory=/opt/genedatabank/
# execute command to start the application
ExecStart=/usr/lib/jvm/java-17-openjdk-amd64/bin/java \
    -jar /opt/genedatabank/genedatabank.jar \
    --server.port=8090
# send stdout to syslog
StandardOutput=syslog
# send stderr to syslog
StandardError=syslog
# identifier for syslog
SyslogIdentifier=genedatabank

[Install]
WantedBy=multi-user.target

Also I added syslog in equation to have dedicated log for my application, tutorial in this link
https://www.baeldung.com/linux/redirect-systemd-output-to-file

for config rsyslog I have

florin@server:~$ cat /etc/rsyslog.d/genedatabank.conf
if $programname == 'genedatabank' then /var/log/genedatabank.log
& stop

and now I can see the log, for my application, very nice:

florin@server:~$ tail -F /var/log/genedatabank.log 
2024-08-29T10:15:42.086244+03:00 server genedatabank[351531]: #011at com.vaadin.flow.server.communication.PushAtmosphereHandler.onStateChange(PushAtmosphereHandler.java:54) ~[flow-server-24.3.13.jar!/:24.3.13]
2024-08-29T10:15:42.086299+03:00 server genedatabank[351531]: #011at org.atmosphere.cpr.DefaultBroadcaster.invokeOnStateChange(DefaultBroadcaster.java:1036) ~[atmosphere-runtime-3.0.5.slf4jvaadin1.jar!/:3.0.5.slf4jvaadin1]
2024-08-29T10:15:42.086335+03:00 server genedatabank[351531]: #011at org.atmosphere.cpr.DefaultBroadcaster.prepareInvokeOnStateChange(DefaultBroadcaster.java:1056) ~[atmosphere-runtime-3.0.5.slf4jvaadin1.jar!/:3.0.5.slf4jvaadin1]
2024-08-29T10:15:42.086382+03:00 server genedatabank[351531]: #011at org.atmosphere.cpr.DefaultBroadcaster.executeAsyncWrite(DefaultBroadcaster.java:870) ~[atmosphere-runtime-3.0.5.slf4jvaadin1.jar!/:3.0.5.slf4jvaadin1]
2024-08-29T10:15:42.086426+03:00 server genedatabank[351531]: #011at org.atmosphere.cpr.DefaultBroadcaster$2.run(DefaultBroadcaster.java:477) ~[atmosphere-runtime-3.0.5.slf4jvaadin1.jar!/:3.0.5.slf4jvaadin1]
2024-08-29T10:15:42.086476+03:00 server genedatabank[351531]: #011at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539) ~[na:na]
2024-08-29T10:15:42.086521+03:00 server genedatabank[351531]: #011at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[na:na]
2024-08-29T10:15:42.086568+03:00 server genedatabank[351531]: #011at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) ~[na:na]
2024-08-29T10:15:42.086627+03:00 server genedatabank[351531]: #011at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) ~[na:na]
2024-08-29T10:15:42.086662+03:00 server genedatabank[351531]: #011at java.base/java.lang.Thread.run(Thread.java:840) ~[na:na]
1 Like