Java Service Wrapper Example

The Java Service Wrapper uses a configuration file wrapper.conf to control the various Wrapper settings. For example: 10m = 10 megabytes. Wrapper.logfile.maxsize=0 # Maximum number of rolled log files which will be allowed before old # files are deleted. The default value of 0 implies no limit. Wrapper.logfile.maxfiles=0 # Log Level for sys. Run a Java Application as a Service on Linux. Ask Question Asked 7 years. Which is also quite popular is the Java Service Wrapper. This is also quite popular around the OSS community. You can start and stop the service in the usual way. For example, on a Debian based system: $ service myapp start.

Active6 months ago

I have written a Java server application that runs on a standard virtual hosted Linux solution. The application runs all the time listening for socket connections and creating new handlers for them. It is a server side implementation to a client-server application.

The way I start it is by including it in the start up rc.local script of the server. However once started I do not know how to access it to stop it and if I want to install an update, so I have to restart the server in order to restart the application.

On a windows PC, for this type of application I might create a windows service and then I can stop and start it as I want. Is there anything like that on a Linux box so that if I start this application I can stop it and restart it without doing a complete restart of the server.

My application is called WebServer.exe. It is started on server startup by including it in my rc.local as such:

I am a bit of a noob at Linux so any example would be appreciated with any posts. However I do have SSH, and full FTP access to the box to install any updates as well as access to a Plesk panel.

javaPlease42
2,6031 gold badge28 silver badges49 bronze badges
drezadreza
1,8605 gold badges35 silver badges50 bronze badges

15 Answers

Java Service Wrapper Example

I wrote another simple wrapper here:

You can follow a full tutorial for init.d here and for systemd (ubuntu 16+) here

If you need the output log replace the 2

lines for

PbxManPbxMan
6,5921 gold badge29 silver badges38 bronze badges

Yet Another Java Service Wrapper

A simple solution is to create a script start.sh that runs Java through nohup and then stores the PID to a file:

Then your stop script stop.sh would read the PID from the file and kill the application:

Of course I've left out some details, like checking whether the process exists and removing pid.txt if you're done.

WernseyWernsey

Linux service init script are stored into /etc/init.d. You can copy and customize /etc/init.d/skeleton file, and then call

see http://www.ralfebert.de/blog/java/debian_daemon/. Its for Debian (so, Ubuntu as well) but fit more distribution.

akauppi
8,7549 gold badges63 silver badges79 bronze badges
ArcadienArcadien

Maybe not the best dev-ops solution, but good for the general use of a server for a lan party or similar.

Use screen to run your server in and then detach before logging out, this will keep the process running, you can then re-attach at any point.

Workflow:

Start a screen: screen

Start your server: java -jar minecraft-server.jar

Detach by pressing: Ctl-a, d

Re-attach: screen -r

More info here: https://www.gnu.org/software/screen/manual/screen.html

Peter PetersonPeter Peterson

Another alternative, which is also quite popular is the Java Service Wrapper. This is also quite popular around the OSS community.

carlspringcarlspring
19.6k20 gold badges85 silver badges157 bronze badges

Referring to Spring Boot application as a Service as well, I would go for the systemd version, since it's the easiest, least verbose, and best integrated into modern distros (and even the not-so-modern ones like CentOS 7.x).

Community
yglodtyglodt
7,5907 gold badges55 silver badges93 bronze badges

The easiest way is to use supervisord. Please see full details here: http://supervisord.org/

More info:

Community
Alan ThompsonAlan Thompson
16.7k3 gold badges27 silver badges36 bronze badges

Here is a sample shell script (make sure you replace the MATH name with the name of the your application):

Matheus OliveiraMatheus Oliveira

From Spring Boot application as a Service, I can recommend the Python-based supervisord application. See that stack overflow question for more information. It's really straightforward to set up.

Community
DuffJDuffJ
2,0091 gold badge26 silver badges23 bronze badges

Other answers do a good job giving custom scripts and setups depending on your platform. In addition to those, here are the mature, special purpose programs that I know of:

  • JSW from TanukiSoftware
  • YAJSW is an open source clone from the above. It is written in Java, and it is a nanny process that manages the child process (your code) according to configurations. Works on windows / linux.
  • JSVC is a native application. Its also a nanny process, but it invokes your child application through the JNI, rather than as a subprocess.

Java Wrapper Service

Mori BellamyMori Bellamy

You can use Thrift server or JMX to communicate with your Java service.

tienthanhakaytienthanhakay
Java Service Wrapper Example

To run Java code as daemon (service) you can write JNI based stub.

for a sample code that is based on JNI. In this case you daemonize the code that was started as Java and main loop is executed in C. But it is also possible to put main, daemon's, service loop inside Java.

Have fun with JNI!

Web Service Wrapper Java Example

Oo.oOOo.oO
6,6073 gold badges14 silver badges29 bronze badges

However once started I don't know how to access it to stop it

You can write a simple stop script that greps for your java process, extracts the PID and calls kill on it. It's not fancy, but it's straight forward.Something like that may be of help as a start:

hovanessyanhovanessyan
26.1k6 gold badges44 silver badges67 bronze badges

From Spring Boot Reference Guide

Installation as an init.d service (System V)

Simply symlink the jar to init.d to support the standard start, stop, restart and status commands. Assuming that you have a Spring Boot application installed in /var/myapp, to install a Spring Boot application as an init.d service simply create a symlink:

Once installed, you can start and stop the service in the usual way. For example, on a Debian based system:

If your application fails to start, check the log file written to /var/log/<appname>.log for errors.

Ubuntu 16+ Java Service Wrapper Example For Systemd

Continue reading to know how to secure a deployed service.

After doing as written I've discovered that my service fails to start with this error message in logs: start-stop-daemon: unrecognized option --no-close. And I've managed to fix it by creating a config file /var/myapp/myapp.conf with the following content

naXanaXa
17.2k12 gold badges112 silver badges163 bronze badges

It is possible to run the war as a Linux service, and you may want to force in your pom.xml file before packaging, as some distros may not recognize in auto mode. To do it, add the following property inside of spring-boot-maven-plugin plugin.

Next, setup your init.d with:

Java Service Wrapper Example Code

and you will be able to run

There are many other options that you can find in Spring Boot documentation, including Windows service.

Eder Luis JorgeEder Luis Jorge

Not the answer you're looking for? Browse other questions tagged javalinuxservice or ask your own question.