Start java app as service

Run a Java Application as a Service on Linux

A simple service in Linux is a program that runs in the background and performs a specific function or set of functions. These programs can be started automatically at boot time, and can be controlled using the command line or through a system service manager such as systemd or Upstart.

To create a simple service in Linux, you would first write the program that performs the desired function. This program should be designed to run in the background and to continue running even after the terminal window or SSH session is closed.

Next, you would create a script that can start, stop, and check the status of the program. This script is usually placed in the /etc/init.d/ directory and should be made executable. It would then be registered with the system service manager using command like systemctl or service .

Once registered, you would be able to control the service using standard command such as systemctl start|stop|status myservice or service myservice start|stop|status

Service file

A service file, also known as a unit file, is a configuration file used by the system service manager (such as systemd or Upstart) to control a service in Linux. It describes how the service should be started, stopped, and managed, and contains various settings and options that can be used to customize the behavior of the service.

Читайте также:  Collisions in java hashmap

A service file typically has a simple structure and is written in a declarative language. It consists of a series of sections, each of which contains specific configuration options. The most important sections are [Unit], [Service], and [Install].

  • [Unit] − contains information about the service, such as its name, description, and dependencies.
  • [Service] − contains information about how the service should be executed, such as the command to start and stop the service, and the user and group under which the service should run.
  • [Install] − contains information about how the service should be installed and activated, such as the runlevels at which the service should be started and stopped.

Here is an example of a simple service file for a service named «myservice» −

[Unit] Description=My Service After=network.target [Service] User=myservice Group=myservice ExecStart=/usr/bin/myservice Restart=always [Install] WantedBy=multi-user.target

Once the service file has been created, it should be placed in the appropriate directory (usually /etc/systemd/system or /etc/init) and then the service can be controlled using standard command such as systemctl start|stop|status myservice.

Forking Service

A forking service is a type of service that starts multiple instances of a program, each of which runs in its own process. In the context of a Java application, a forking service would involve starting multiple Java Virtual Machines (JVMs) to run the same application, with each JVM running in its own process.

Here’s one example of how you could create a forking service for a Java application −

Write a script that starts the Java application and forks a new process for each instance. This script should take command-line arguments to specify the number of instances to fork, as well as any other necessary configuration options.

Use the nohup command to run the script in the background and prevent it from being terminated when the terminal is closed.

Use a for loop to start n number of instance specified in the command line arguments

Create a service file and register it with the system service manager using systemctl or service command

[Unit] Description=My Java Forking Service [Service] ExecStart=/usr/local/bin/start-my-service.sh Restart=always User=myuser [Install] WantedBy=multi-user.target

Once registered, you would be able to control the service using standard command such as systemctl start|stop|status myservice.

Note that this is just one example of how you could create a forking service for a Java application, and there are other ways to achieve the same result depending on the specifics of the application and the environment it is running in.

Registering and Running the Service

Once you have created a service file for your Java application, you can register it with the system service manager to make it automatically start at boot time and allow you to control it using standard commands.

The process of registering and running the service will depend on which system service manager you are using.

For example, with systemd, you can use the following commands to register and run the service −

Copy the service file to the /etc/systemd/system directory.

Reload systemd to read the service file

Enable the service to start automatically at boot time

systemctl enable myservice
systemctl start myservice

Check the status of the service

systemctl status myservice

With sysvinit based distro like Ubuntu 14.04 and earlier uses update-rc.d

update-rc.d myservice defaults

With Upstart, the process is similar, but you will use the initctl command instead of systemctl .

It’s worth noting that if you’re running your application in a container like docker, you don’t have to worry about this process of registering and running the service. you should handle the container to start at boot.

Run a Java Application as a Service on Linux

Running a Java application as a service on Linux involves creating a service file that describes how the application should be started, stopped, and managed, and then registering that service file with the system service manager.

Here is an example of how you could run a Java application as a service on Linux −

Create a script that starts your Java application. This script should use the nohup command to run the application in the background and prevent it from being terminated when the terminal is closed.

nohup java -jar /path/to/your/application.jar &

Create a service file with the desired configuration for your service. The service file should contain information such as the name of the service, the user and group to run the service as, and the command to start and stop the service.

[Unit] Description=My Java Application Service After=network.target [Service] User=myuser Group=myuser ExecStart=/usr/local/bin/start-my-application.sh Restart=always [Install] WantedBy=multi-user.target

Register the service file with the system service manager. On systems using systemd, you can use the systemctl command to register the service, such as

systemctl enable myservice
systemctl start myservice

Check the status of the service

systemctl status myservice

It is worth noting that the above steps and examples are for a system that uses systemd as service manager. The process for other service managers (like SysV or Upstart) is slightly different, but the general idea is the same.

Conclusion

Running a Java application as a service on Linux involves creating a service file that describes how the application should be started, stopped, and managed, and then registering that service file with the system service manager.

The process involves creating a script that starts your Java application using the nohup command to run the application in the background and prevent it from being terminated when the terminal is closed.

Источник

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.

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.

Источник

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