- Web Service Wrapper Java Example
- Java Windows Service Wrapper
- Tanuki Java Service Wrapper Example
- Java Service Wrapper Examples
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.
Article updated on Monday, 20 November 2017 Linux Java Service Wrapper Example. This is a simple wrapper to run a Java program as service. You need to be a root user.
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.
javaPlease4215 Answers
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
PbxManPbxManA 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.
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.
akauppiMaybe 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
Another alternative, which is also quite popular is the Java Service Wrapper. This is also quite popular around the OSS community.
carlspringcarlspringReferring 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).
The easiest way is to use supervisord
. Please see full details here: http://supervisord.org/
More info:
Here is a sample shell script (make sure you replace the MATH name with the name of the your application):
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.
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.
You can use Thrift server or JMX to communicate with your Java service.
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!
Oo.oOOo.oOWeb Service Wrapper Java Example
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:
hovanessyanhovanessyanFrom 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.
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
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.
Java Windows Service Wrapper
Next, setup your init.d with:
and you will be able to run
There are many other options that you can find in Spring Boot documentation, including Windows service.