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:
java -jar /var/www/vhosts/myweb.com/phpserv/WebServer.jar &
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.
16
Answers
Another alternative, which is also quite popular is the Java Service Wrapper. This is also quite popular around the OSS community.
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:
Linux service init script are stored into
/etc/init.d
. You can copy and customize/etc/init.d/skeleton
file, and then callsee http://www.ralfebert.de/blog/java/debian_daemon/. Its for Debian (so, Ubuntu as well) but fit more distribution.
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.You can use Thrift server or JMX to communicate with your Java service.
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
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.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
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).The easiest way is to use
supervisord
. Please see full details here: http://supervisord.org/More info:
https://askubuntu.com/questions/779830/running-an-executable-jar-file-when-the-system-starts/852485#852485
https://www.digitalocean.com/community/tutorials/how-to-install-and-manage-supervisor-on-ubuntu-and-debian-vps
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:
Here is a sample shell script (make sure you replace the MATH name with the name of the your application):
To run Java code as daemon (service) you can write JNI based stub.
http://jnicookbook.owsiak.org/recipe-no-022/
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.
https://github.com/mkowsiak/jnicookbook/tree/master/recipes/recipeNo029
Have fun with JNI!
From Spring Boot Reference Guide
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 contentIt 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:
and you will be able to run
There are many other options that you can find in Spring Boot documentation, including Windows service.
Im having Netty java application and I want to run it as a service with systemd. Unfortunately application stops no matter of what Type I’m using. At the end I’ve wrapped java start in screen. Here are the config files:
service
start
from that point you can use
systemctl [start|stop|status] service
.