PDFelement Anniversary Sale 40% OFF
Coursera: Build New Skills Anytime, Anywhere with 100% online courses. Start Now!
Start your future with a Business Analytics Certificate.

How to run Java application as service on Linux

In this post,we check out how to run Java application as service on Linux and also start automatically if and when system restarts.

Before we move on to article,lets understand what is systemd .Its part of Linux kernel that provides the System and Service Manager to bootstrap user space and manage user processes that includes startup shell scripts like pm-utils, inetd, acpid, syslog, watchdog, cron and atd. systemd’s core components includes the following:

  • System and service manager for Linux operating systems.
  • Systemctl to introspect and control the state of the systemd system and service manager.
  • systemd-analyze package to determine system boot-up performance statistics and retrieve other state and tracing information from the system and service manager.

Image – systemd Architecture

In the next section,we can take look at how to make Java application as Linux Daemon

#1.Create new file on /etc/systemd with your service name

sudo vi /etc/systemd/system/springbootapp.service

nordvpn
[Unit]
Description=SpringBoot Service

[Service]
User=nobody
WorkingDirectory=/home/demo1
ExecStart=/usr/bin/java -Xmx256m -jar /home/demo1/spring-boot-rest-postgresql-0.0.1-SNAPSHOT.jar --server.port=8081
SuccessExitStatus=143
TimeoutStopSec=10
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target

Modify Description, User and ExecStart fields based on your application needs.WantedBy=multi-user.target denotes that this service will only be started when the system boots up to this target (a non-graphical multi-user environment).

Image – Daemon definition file

Use the code UFC20 during checkout for Ivacy VPN subscription plans,You'll get an additional 20% off.

Subscribe to Emails

More than 100,000 subscribers across the world trust & read Upnxtblog articles. Subscribe to emails updates!
* indicates required

#2.Reload Linux Daemon

Use command systemctl daemon-reload to reload the service before we enable the newly added service

Image – Reload Daemon

#3.Enable newly added service

Enable the new service by using systemctl enable <service name> command.

A powerful and intuitive video editing experience

Image – Enable service

#4.Start the service & check the status

Next step is to start the service by systemctl start <service name> command and you can check the status by using systemctl status <service name> command.

Image – Start the service

#5.Stop / Disable service

As a last step,if we want to stop the service, use systemctl stop <service name> command.

Image – Stop the service

Magoosh

If you want to disable the service,use systemctl disable <service name> command.

Congrats! today we have learnt how to Java application as Linux daemon service.If you re-look at the steps,its same for any kind of application.

There are also other options like System V init script but Systemd is preferred option as of now.

Like this post? Don’t forget to share it!

Summary
How to run Java application as service on Linux
Article Name
How to run Java application as service on Linux
Description
In this post,we check out how to run Java application as service on Linux and also start automatically if and when system restarts.
Author
Publisher Name
Upnxtblog
Publisher Logo
%d bloggers like this: