Linux start jar as service

Convert a java application into a Linux service

First of all, a Linux Service is an application that runs in the background and performs an essential task. One of the advantages to use a service is that you do not need to start it manually, you can configure it to start with the system using this command:

You can list all the services inside your Server using

systemctl list-units --type=service 

Also you can see the status of an specific service using:

[ernesto.lopez@test-vm ~]$ systemctl status chronyd.service ● chronyd.service - NTP client/server Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled) Active: active (running) since Tue 2021-11-02 12:31:04 MST; 3 weeks 3 days ago Docs: man:chronyd(8) man:chrony.conf(5) Main PID: 1212 (chronyd) Tasks: 1 (limit: 98556) Memory: 1.1M CGroup: /system.slice/chronyd.service └─1256 /usr/sbin/chronyd 
  • chronyd.service — NTP client/server: is the name of the service and a small description of it.
  • Loaded: loaded (/usr/lib/systemd/system/chronyd.service; : means that the services located in the path that appears was loaded in memory
  • enabled; vendor preset: enabled) : this part indicate that the service is enable, so it will start at boot, this is achieved using the command sudo systemctl enable
  • Active: active (running) : this means that the service is up and running in the system, this line usually appears on green if you are using a normal user. This line also notifies the user when this service was started.
  • Main PID: 1212 (chronyd) : this line shows the process ID for this service. you can then use ps 1212 to get the command associated with the service.

These lines represent an important aspect to evaluate a service, cGroup deserve a single post entry.

NOTE Before converting anything to a service, make sure you have installed java on your server. A quick using dnf and java 8 example:

sudo dnf -y update sudo dnf -y install java-1.8.0-openjdk-devel java –version #Find your java home path sudo alternatives --config java 

Add this line into your profile file (/etc/profile) to easier the following steps:

export JAVA_HOME=$(dirname $(dirname $(readlink $(readlink >$(which javac)))))
export PATH=$PATH:$JAVA_HOME/bin
export >CLASSPATH=.:$JAVE_HOME/jre/lib:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar

Save the file and set the system to use this profile

You can verify that the configurations were correct, by running:

echo $JAVA_HOME echo $PATH echo $CLASSPATH 

Back to the starting our java app, suppose our app is in a .jar file called my-own-app.jar

JAR stands for Java ARCHIVE, it is a file format used to aggregate many java files into one.

More on the official documentation from Oracle

And for example we have our application stored inside /opt/my-own-app/ directory

First thing that we are going to do is to create a script that start the app, imagine we are going to convert our java app in a bash script (Just an imagination exercise!)

cd /opt/my-own-app touch my-own-app.sh vi !$ 

Then, we need to tell the script to start the app in the .jar file

#!/bin/bash # Script to start MyOwnApp echo "--------------------WAIT-------------------" # Start the app # minimum heap 512MB, maximum heap 1GB cd /opt/my-own-app java -Xms512M -Xmx1G -jar my-own-app.jar & 

Here we are creating a script that move tho the directory where the .jar file is located and then execute the java command which is used to launch a java application. The elements to consider:

  • -Xms512M specifies the initial sizeo of the memory allocation pool, in this case we append M to indicates it is megabyte. This value must be determined based on app consumption.
  • -Xmx1G is the maximum size of the memory allocation pool, this case indicates 1G but can vary depending on your app.
  • -jar define the location of the jar file, because in the previous step in the script we move to the /opt/my-own-app dir, we use just the name of the file in this case.
Читайте также:  Линукс где мой компьютер

Save the file and change the permissions to make it an executable:

sudo chmod u+rwx my-own-app.sh ls -l | grep my-own-app.sh 

Next step is to create a service unit:

vi /etc/systemd/system/my-own-app.service 

Note A service unit describes how to manage a service or application on the server.

Insert the following information on the file:

[Unit] Description=my own app starting After=network.target [Service] Type=forking ExecStart=/opt/my-own-app/my-own-app.sh TimeoutStartSec=0 [Install] WantedBy=default.target 

The important aspects of this:

  • WantedBy=default.target defines how a unit should be enabled, this represents the default and will create a file .want append inside /etc/systemd/directory
  • Type=forking tells the systemd that the process is still running even though the parent process exited.
  • ExecStart=/opt/my-own-app/my-own-app.sh is the file to be executed.
  • After=network.target the service must be started after network services.

The following step is to reload the systemd daemon

sudo systemctl daemon-reload 

And finally enable and start the service

sudo systemctl start my-own-app.service sudo systemctl enable my-own-app.service 

You can verify if the service started ok or if there was any trouble using

sudo systemctl status my-own-app.service 

Источник

Start a jar file like service in linux [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.

I want to start and stop my jar file as follows service myService start service myService stop my current jar file running as follows

cd /home/alex/IdeaProjects/myService java -jar target/myService-SNAPSHOT-1.jar server config.yml 

Implement a wrapper service. A little search will show up some 3rd party implementation you could use.

Читайте также:  Nfs most wanted linux

3 Answers 3

You need a Service Wrapper to run the Jar file.

There are examples and instructions for init.d here. or for systemd (ubuntu 16+) here

Just an info for people who are on Ubuntu 16+. This doesn’t work with Ubuntu 16+. You will get ‘Failed to start MyService.service: Unit MyService.service not found’

I prefer a light weight, free, bash script rather than a more elaborate system that requires licensing.

If you want to try the DIY way, you can place a startup script in your /etc/init.d directory as said here.

However, to implement a clean yourScript stop command, I would recommend that you split your functionality into a launcher and a daemon, and make your launcher able to start or communicate with your existing daemon in order to send orders to it. Then your startup script would only invoke your launcher, which in its turn would start a new daemon, or send orders to the existing one.

Источник

Running a Java Application as a Service

To control our application, we’re going to run it as a systemd service. Wikipedia defines systemd as:

a software suite that provides an array of system components for Linux operating systems. Its main aim is to unify service configuration and behavior across Linux distributions; systemd’s primary component is a «system and service manager» — an init system used to bootstrap user space and manage user processes.

To create a service, we need to have the relevant access to the server, so we need to ensure we have sudo access.

The first stage in defining the application as a service, is to create a file within the /etc/systemd/system directory with a name of myapp.service

/etc/systemd/system/myapp.service 

Within this file, we need to add 3 sections. [Unit] defines the description of the service, [Service] defines how the service is executed and [Install] tells the operating system when to run the application. For example

[Unit] Description=My Application As A Service [Service] User=myapp_user Group=myapp_group Type=simple ExecStart= java -jar /home/ubuntu/myapp/myapp.jar -Xmx512m –Xms512m -XX:MaxNewSize=384m -XX:MaxPermSize=128m SuccessExitStatus=143 [Install] WantedBy=multi-user.target 

Let’s take a look at each of these sections in turn.

[Unit]

This section simply contains one entry which is the description of the service. Note, the name of the service is defined by the name of the file within the /etc/systemd/system directory.

[Service]

This section defines details that the operating system requires to be able to start the service. Firstly, the User and Group specify the security details for the application to run. The application is run as this specified user and group and has their permissions.

Type can be set to simple for most cases. This defines that the application will run immediately without forking any other processes. For a definition of the other options available here, check out the man pages for systemd.

Читайте также:  Zoom linux на русском

Next, ExecStart specifies the command that is used to run the application. Here, we specify the entire command line (including java and any parameters) to run the application. This can be very useful as we can specify which version of Java to use here, so an application can be configured to run with any required JVM, and not only with the system’s default JVM. Any JVM properties or application variables can be configured here.

Finally, as we’re running a Java application, we need to tell it how to play properly with systemd. When a Java application is killed via a SIGTERM event, the JVM will close down cleanly but will return an exit code of 143. Adding this as a SuccessExitStatus tells systemd that the application has closed cleanly in this situation.

[Install]

The last section basically tells systemd that if the application is configured to start at server boot time, then do so as part of the normal boot process. This tells systemd that the application can start at boot time, but not that it necessarily will. Read on for how we do that.

Controlling the Service

Once the .service file has been created, we need to tell systemd that we have a new service. This is achieved by executing:

sudo systemctl daemon-reload 

Once executed, we can start and stop the service via:

sudo systemctl start myapp sudo systemctl stop myapp 

We can get the status of the application by executing:

sudo systemctl status myapp 

Finally, if we want to tell the systemd to start the application at boot time, we can execute the following command:

sudo systemctl enable myapp 

Restarting upon Failure

Now that we’ve seen how to create and manage a service, the only thing remaining is to tell systemd to restart the service in case of a failure.

That can be achieved by adding the following entries into the [Service] section of the file.

Restart=on-failure RestartSec=10s 

This configuration tells systemd to restart the service 10s after a failure (you can obviously customise this to your required time interval).

There are other options available here, for example, when the service is to restart and how many attempts to be made. For more information, check out the man pages for systemd.

Conclusion

In this post we’ve seen how to run a Java application as a service using systemd, and how to control it. We’ve seen how to start the service at boot time and how to restart the service upon failure.

Источник

Оцените статью
Adblock
detector