In this post, I will show you how you can easily manage Java jar applications with Systemd service. Systemd is a system and service manager for Linux. It is now the default init system for a number of Distributions including Ubuntu, Debian, CentOS, Arch Linux e.t.c.
In this deployment, I have a Jar file located under a /opt/prod/
directory. As a rule of thumb, we need to add a system user which will run the application with systemd.
Create an Application User and group
Start by creating a system group for the user.
sudo groupadd -r appmgr
Next, we create a system user jvmapps
with the default group:
sudo useradd -r -s /bin/false -g appmgr jvmapps
Confirm user created and if with the correct group:
$ id jvmapps
uid=992(jvmapps) gid=986(appmgr) groups=986(appmgr)
Create Systemd Service
We can now create a systemd service file to manage our application. You need to create this file as root user.
sudo vim /etc/systemd/system/myapp.service
It will have content like below:
[Unit]
Description=Manage Java service
[Service]
WorkingDirectory=/opt/prod
ExecStart=/bin/java -Xms128m -Xmx256m -jar myapp.jar
User=jvmapps
Type=simple
Restart=on-failure
RestartSec=10
[Install]
WantedBy=multi-user.target
Set User
to the one created earlier, and WorkingDirectory
to the directory with a jar file.
-Xms128m
and -Xmx256m
are used to set the minimum and maximum memory that the application can use.
When done with the change, give the user and group ownership permissions for the Project Directory:
sudo chown -R jvmapps:appmgr /opt/prod
Start Java Application service with systemd
The next thing to do is start the application service, but first, reload systemd so that it knows of the new application added.
sudo systemctl daemon-reload
Once reloaded, start the service:
sudo systemctl start myapp.service
To check the status, use:
systemctl status myapp
Sample output:
$ systemctl status myapp
● myapp.service - Manage Java service
Loaded: loaded (/etc/systemd/system/myapp.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2023-11-24 11:12:23 EAT; 23h ago
Main PID: 23220 (java)
CGroup: /system.slice/myapp.service
└─23220 /bin/java -Xms128m -Xmx256m -jar myapp.jar
Aug 25 10:50:00 server1 java[23220]: # Duration: 1000 µs
Aug 25 10:50:00 server1 java[23220]: # Total number of fields classified 0, 0 failed
Aug 25 10:50:00 server1 java[23220]: # ---------------------------------------------------------------
Aug 25 10:50:00 server1 java[23220]: # Completed step 'classify-tables'
Aug 25 10:50:00 server1 java[23220]: # Start: 2018-08-25T07:50:00.258Z
Aug 25 10:50:00 server1 java[23220]: # End: 2018-08-25T07:50:00.259Z
Aug 25 10:50:00 server1 java[23220]: # Duration: 1000 µs
Aug 25 10:50:00 server1 java[23220]: # Total number of tables classified 3, 0 updated
Aug 25 10:50:00 server1 java[23220]: #################################################################
Aug 25 10:50:00 server1 java[23220]: 08-25 10:50:00 INFO sync.util :: FINISHED: Analyze data for mysql Database 2 'Ch... (8 ms)
Hint: Some lines were ellipsized, use -l to show in full.
You can also enable the service to start on server boot:
$ sudo systemctl enable myapp
Created symlink from /etc/systemd/system/multi-user.target.wants/myapp.service to /etc/systemd/system/myapp.service.
To restart the application, use:
sudo systemctl restart myapp
You now have a Java Application being managed by Systemd. Replicate the same procedure for all other services you need to manage using Systemd init. Hope this was helpful, for any issues, let me know through the comments section.
Other guides:
- Best Books for Learning Java Programming
- Install Metabase with Systemd on Debian
- How to Install Metabase with Systemd on Ubuntu
Josphat I don’t typically write replies but MAN, i’ve went through so many guides and none of them was able to start the java app. Thank You for saving me! Beer on me whenever you are in chicago!
Thanks a lot for the positive comment. This an energizer to more work.
Thanks its did helped for me
Welcome always!
iam unable to start the service as below, can you please help me out
WindowsJava01@DemoLinuxVM:~$ systemctl status demoApplication
● demoApplication.service – Manage Java service
Loaded: loaded (/etc/systemd/system/demoApplication.service; enabled; vendor preset: enabled)
Active: activating (auto-restart) (Result: exit-code) since Tue 2022-02-15 07:30:31 UTC; 3s ago
Process: 4322 ExecStart=/bin/java -Xms128m -Xmx256m -jar demoApplication.jar (code=exited, status=1/FAILURE)
Main PID: 4322 (code=exited, status=1/FAILURE)
Can you run the app manually using the command below?
Could it be memory limits issue?
Thanks, Problem resolved
Josphat, How we can output the logs to file app.log?
hi, iam able to do all the steps provided above but how can i request the localhost to get the output in browser using Linix machine ip address?
The following is the output
demoApplication.service – Manage Java service
Loaded: loaded (/etc/systemd/system/demoApplication.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2022-02-17 06:57:36 UTC; 4s ago
Main PID: 11589 (java)
Tasks: 14 (limit: 1082)
Memory: 139.2M
CGroup: /system.slice/demoApplication.service
└─11589 /bin/java -Xms128m -Xmx256m -jar demoApplication.jar
Feb 17 06:57:36 LinuxVM systemd[1]: Started Manage Java service.
Feb 17 06:57:39 LinuxVM java[11589]: . ____ _ __ _ _
Feb 17 06:57:39 LinuxVM java[11589]: /\\ / ___’_ __ _ _(_)_ __ __ _ \ \ \ \
Feb 17 06:57:39 LinuxVM java[11589]: ( ( )\___ | ‘_ | ‘_| | ‘_ \/ _` | \ \ \ \
Feb 17 06:57:39 LinuxVM java[11589]: \\/ ___)| |_)| | | | | || (_| | ) ) ) )
Feb 17 06:57:39 LinuxVM java[11589]: ‘ |____| .__|_| |_|_| |_\__, | / / / /
Feb 17 06:57:39 LinuxVM java[11589]: =========|_|==============|___/=/_/_/_/
Feb 17 06:57:39 LinuxVM java[11589]: :: Spring Boot :: (v2.6.2)
Feb 17 06:57:39 LinuxVM java[11589]: 2022-02-17 06:57:39.720 INFO 11589 — [ main] com.example.demo.DemoProjectApplication : Starting DemoProjectApplicati>
Feb 17 06:57:39 LinuxVM java[11589]: 2022-02-17 06:57:39.722 INFO 11589 — [ main] com.example.demo.DemoProjectApplication : No active profile set, fallin>
-…skipping…
● demoApplication.service – Manage Java service
Loaded: loaded (/etc/systemd/system/demoApplication.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2022-02-17 06:57:36 UTC; 4s ago
Main PID: 11589 (java)
Tasks: 14 (limit: 1082)
Memory: 139.2M
CGroup: /system.slice/demoApplication.service
└─11589 /bin/java -Xms128m -Xmx256m -jar demoApplication.jar
Feb 17 06:57:36 LinuxVM systemd[1]: Started Manage Java service.
Feb 17 06:57:39 LinuxVM java[11589]: . ____ _ __ _ _
Feb 17 06:57:39 LinuxVM java[11589]: /\\ / ___’_ __ _ _(_)_ __ __ _ \ \ \ \
Feb 17 06:57:39 LinuxVM java[11589]: ( ( )\___ | ‘_ | ‘_| | ‘_ \/ _` | \ \ \ \
Feb 17 06:57:39 LinuxVM java[11589]: \\/ ___)| |_)| | | | | || (_| | ) ) ) )
Feb 17 06:57:39 LinuxVM java[11589]: ‘ |____| .__|_| |_|_| |_\__, | / / / /
Feb 17 06:57:39 LinuxVM java[11589]: =========|_|==============|___/=/_/_/_/
Feb 17 06:57:39 LinuxVM java[11589]: :: Spring Boot :: (v2.6.2)
Feb 17 06:57:39 LinuxVM java[11589]: 2022-02-17 06:57:39.720 INFO 11589 — [ main] com.example.demo.DemoProjectApplication : Starting DemoProjectApplicati>
F
Josphat, iam able to do the steps provided above but how can i browser the output like using the Linux ip address
The following is the output and how can i exit from this output
demoApplication.service – Manage Java service
Loaded: loaded (/etc/systemd/system/demoApplication.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2022-02-17 06:57:36 UTC; 4s ago
Main PID: 11589 (java)
Tasks: 14 (limit: 1082)
Memory: 139.2M
CGroup: /system.slice/demoApplication.service
└─11589 /bin/java -Xms128m -Xmx256m -jar demoApplication.jar
Feb 17 06:57:36 LinuxVM systemd[1]: Started Manage Java service.
Feb 17 06:57:39 LinuxVM java[11589]: . ____ _ __ _ _
Feb 17 06:57:39 LinuxVM java[11589]: /\\ / ___’_ __ _ _(_)_ __ __ _ \ \ \ \
Feb 17 06:57:39 LinuxVM java[11589]: ( ( )\___ | ‘_ | ‘_| | ‘_ \/ _` | \ \ \ \
Feb 17 06:57:39 LinuxVM java[11589]: \\/ ___)| |_)| | | | | || (_| | ) ) ) )
Feb 17 06:57:39 LinuxVM java[11589]: ‘ |____| .__|_| |_|_| |_\__, | / / / /
Feb 17 06:57:39 LinuxVM java[11589]: =========|_|==============|___/=/_/_/_/
Feb 17 06:57:39 LinuxVM java[11589]: :: Spring Boot :: (v2.6.2)
Feb 17 06:57:39 LinuxVM java[11589]: 2022-02-17 06:57:39.720 INFO 11589 — [ main] com.example.demo.DemoProjectApplication : Starting DemoProjectApplicati>
Feb 17 06:57:39 LinuxVM java[11589]: 2022-02-17 06:57:39.722 INFO 11589 — [ main] com.example.demo.DemoProjectApplication : No active profile set, fallin>
~
Press Q or CTRL+C
please assist/help
I can start the service if I put User=root in [Service] section
if I try to start with created user as per guide it gives:
…compute.internal systemd[1]: Started Unified service spring boot app for subscription tracker.
….compute.internal java[16737]: Error: Could not find or load main class org.springframework.boot.loader.JarLauncher
….compute.internal java[16737]: Caused by: java.lang.ClassNotFoundException: org.springframework.boot.loader.JarLauncher
….compute.internal systemd[1]: unified-service.service: main process exited, code=exited, status=1/FAILURE
seems like problem is with the user, but can not find how to resolve this