138

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 16

244

I wrote another simple wrapper here:

#!/bin/sh
SERVICE_NAME=MyService
PATH_TO_JAR=/usr/local/MyProject/MyJar.jar
PID_PATH_NAME=/tmp/MyService-pid
case $1 in
    start)
        echo "Starting $SERVICE_NAME ..."
        if [ ! -f $PID_PATH_NAME ]; then
            nohup java -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &
            echo $! > $PID_PATH_NAME
            echo "$SERVICE_NAME started ..."
        else
            echo "$SERVICE_NAME is already running ..."
        fi
    ;;
    stop)
        if [ -f $PID_PATH_NAME ]; then
            PID=$(cat $PID_PATH_NAME);
            echo "$SERVICE_NAME stoping ..."
            kill $PID;
            echo "$SERVICE_NAME stopped ..."
            rm $PID_PATH_NAME
        else
            echo "$SERVICE_NAME is not running ..."
        fi
    ;;
    restart)
        if [ -f $PID_PATH_NAME ]; then
            PID=$(cat $PID_PATH_NAME);
            echo "$SERVICE_NAME stopping ...";
            kill $PID;
            echo "$SERVICE_NAME stopped ...";
            rm $PID_PATH_NAME
            echo "$SERVICE_NAME starting ..."
            nohup java -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &
            echo $! > $PID_PATH_NAME
            echo "$SERVICE_NAME started ..."
        else
            echo "$SERVICE_NAME is not running ..."
        fi
    ;;
esac 

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

nohup java -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &

lines for

nohup java -jar $PATH_TO_JAR >> myService.out 2>&1&
9
  • @PbxMan thanks for this. I might give it a go and see how we get on. Cheers.
    – dreza
    Jan 22, 2014 at 20:52
  • 2
    but how I can run this file? where I must put it? Oct 30, 2015 at 14:54
  • 3
    @JackDaniel on debian-based distros, like debian itself and ubuntu, you can add that script to /etc/init.d. Then you can invoke it like this: /etc/init.d/MyService start. And you can make it start automatically by running update-rc.d MyService defaults .
    – Andre
    Jan 26, 2016 at 14:15
  • 1
    @ThorbjørnRavnAndersen That would depend on your java program. If you cannot kill your java program Check out stackoverflow.com/questions/2541597/…. I would delete the MyService-pid instead of the kill and have deamon thread in the Java part that checks if it exists.
    – PbxMan
    Feb 13, 2017 at 15:29
  • 1
    Where will the ouput file of the jar be? how can i configure it's name?
    – M. Schena
    Apr 5, 2017 at 14:48
52

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

nohup java -jar myapplication.jar > log.txt 2> errors.txt < /dev/null &
PID=$!
echo $PID > pid.txt

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

PID=$(cat pid.txt)
kill $PID

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

2
  • 3
    Question: Wouldn't the kill $PID command cause the process to be killed without finishing? I'm writing a server program that interfaces with a database, and I would want all currently running threads to finish before the program exits, so as to ensure that the program doesn't die in the middle of a write to the DB or something. Dec 12, 2013 at 4:29
  • 2
    @scuba-steve sort of. kill will send the TERM signal, which will invoke any shutdown hooks that are in place, so use them to end your process gracefully. They won't execute if the process gets a kill signal (i.e., kill -9). The OS may interrupt your shutdown hooks if they're taking too long to complete, so keep them succinct
    – rjohnston
    May 30, 2014 at 4:44
34

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

service [yourservice] start|stop|restart

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

1
  • Looks promising. I will be taking a closer look into this. cheers
    – dreza
    Jun 26, 2012 at 9:43
14

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

7

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

0
5

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).

4

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

4

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

#!/bin/bash

### BEGIN INIT INFO
# Provides:                 MATH
# Required-Start:           $java
# Required-Stop:            $java
# Short-Description:        Start and stop MATH service.
# Description:              -
# Date-Creation:            -
# Date-Last-Modification:   -
# Author:                   -
### END INIT INFO

# Variables
PGREP=/usr/bin/pgrep
JAVA=/usr/bin/java
ZERO=0

# Start the MATH
start() {
    echo "Starting MATH..."
    #Verify if the service is running
    $PGREP -f MATH > /dev/null
    VERIFIER=$?
    if [ $ZERO = $VERIFIER ]
    then
        echo "The service is already running"
    else
        #Run the jar file MATH service
        $JAVA -jar /opt/MATH/MATH.jar > /dev/null 2>&1 &
        #sleep time before the service verification
        sleep 10
        #Verify if the service is running
        $PGREP -f MATH  > /dev/null
        VERIFIER=$?
        if [ $ZERO = $VERIFIER ]
        then
            echo "Service was successfully started"
        else
            echo "Failed to start service"
        fi
    fi
    echo
}

# Stop the MATH
stop() {
    echo "Stopping MATH..."
    #Verify if the service is running
    $PGREP -f MATH > /dev/null
    VERIFIER=$?
    if [ $ZERO = $VERIFIER ]
    then
        #Kill the pid of java with the service name
        kill -9 $($PGREP -f MATH)
        #Sleep time before the service verification
        sleep 10
        #Verify if the service is running
        $PGREP -f MATH  > /dev/null
        VERIFIER=$?
        if [ $ZERO = $VERIFIER ]
        then
            echo "Failed to stop service"
        else
            echo "Service was successfully stopped"
        fi
    else
        echo "The service is already stopped"
    fi
    echo
}

# Verify the status of MATH
status() {
    echo "Checking status of MATH..."
    #Verify if the service is running
    $PGREP -f MATH > /dev/null
    VERIFIER=$?
    if [ $ZERO = $VERIFIER ]
    then
        echo "Service is running"
    else
        echo "Service is stopped"
    fi
    echo
}

# Main logic
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        status
        ;;
    restart|reload)
        stop
        start
        ;;
  *)
    echo $"Usage: $0 {start|stop|status|restart|reload}"
    exit 1
esac
exit 0
2
  • For some reason this always reports service is already started. It appears that pgrep is returning 0 when run from inside the script, but if I enter the pgrep command manually it returns 1. Oct 22, 2018 at 12:06
  • The reason why pgrep thinks the service is running is because it detects "/bin/sh /sbin/service MATH start" and "/bin/bash /etc/init.d/MATH start" and returns 0 Oct 22, 2018 at 12:13
3

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.

1
  • supervisord is great, for those who don't know, it allows monitoring services (which must be foreground - not daemonized), it will then auto restart services (and can email alerts when restarts occur via plugins)
    – wired00
    Jul 6, 2016 at 23:35
2

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.
1

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:

#!/bin/bash
PID = ps ax | grep "name of your app" | cut -d ' ' -f 1
kill $PID
1
  • 2
    I'm not very good in linux but doesn't pkill nameofprocess do about the same thing ? Jun 26, 2012 at 19:10
1

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

1

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:

$ sudo ln -s /var/myapp/myapp.jar /etc/init.d/myapp

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

$ service myapp start

Tip 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

USE_START_STOP_DAEMON=false
1

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.

                    <embeddedLaunchScriptProperties>
                        <mode>service</mode>
                    </embeddedLaunchScriptProperties>

Next, setup your init.d with:

ln -s myapp.war /etc/init.d/myapp

and you will be able to run

service myapp start|stop|restart

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

1

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

[Unit]
Description=Netty service
After=network.target
[Service]
User=user
Type=forking
WorkingDirectory=/home/user/app
ExecStart=/home/user/app/start.sh
TimeoutStopSec=10
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target

start

#!/bin/sh
/usr/bin/screen -L -dmS netty_app java -cp app.jar classPath

from that point you can use systemctl [start|stop|status] service.

1

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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