I don't mean to sidetrack, but I've deployed Java applications on Ubuntu in production since 2010 and had very little success with Upstart. I use init.d scripts and start-stop-daemon. Side bonus: it works on more distros.

Create /etc/init.d/my-java-app:

#!/bin/sh
#
# my-java-app My Java App
#
# chkconfig: - 80 05
# description: Enable My Java Application
#

### BEGIN INIT INFO
# Provides:          my-java-app
# Required-Start:    $remote_fs $network
# Required-Stop:     $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description:       My Java Application
# Short-Description: Enable My Java Application
### END INIT INFO

DESC="my java app"
NAME=my-java-app
PIDFILE=/var/run/$NAME.pid
RUN_AS=ubuntu
WORK_DIR=/home/ubuntu/admin
DAEMON=/usr/bin/mvn
DAEMON_OPTS="spring-boot:run"

# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
. /lib/lsb/init-functions

do_start() {
    start-stop-daemon --start --quiet --make-pidfile --pidfile $PIDFILE \
        --background \
        --chuid $RUN_AS \
        --chdir $WORK_DIR \
        --exec $DAEMON -- $DAEMON_OPTS
}

do_stop() {
    start-stop-daemon --stop --quiet --pidfile $PIDFILE
    if [ -e $PIDFILE ]
        then rm $PIDFILE
    fi
}

case "$1" in
    start)
    echo -n "Starting $DESC: $NAME"
    do_start
    echo "."
    ;;
    stop)
    echo -n "Stopping $DESC: $NAME"
    do_stop
    echo "."
    ;;
    restart)
    echo -n "Restarting $DESC: $NAME"
    do_stop
    sleep 1
    do_start
    echo "."
    ;;
    status)
    status_of_proc -p $PIDFILE "$DAEMON" "$NAME" && exit 0 || exit $?
    ;;
    *)
    echo "usage: $NAME {start|stop|restart}"
    exit 1
    ;;
esac

Make it belong to root, make it executable, and set it up to run on startup with:

sudo chown root:root /etc/init.d/my-java-app
sudo chmod 755 /etc/init.d/my-java-app
sudo update-rc.d my-java-app defaults

To start the service you can run:

sudo service my-java-app start

To stop the service you can run:

sudo service my-java-app stop

This is based on a simplified version of the /etc/init.d/skeleton file included by Ubuntu.

The man page for start-stop-daemon is worth looking at if you want to tweak this further.b

Answer from Alain O'Dea on Stack Overflow
Top answer
1 of 1
7

I don't mean to sidetrack, but I've deployed Java applications on Ubuntu in production since 2010 and had very little success with Upstart. I use init.d scripts and start-stop-daemon. Side bonus: it works on more distros.

Create /etc/init.d/my-java-app:

#!/bin/sh
#
# my-java-app My Java App
#
# chkconfig: - 80 05
# description: Enable My Java Application
#

### BEGIN INIT INFO
# Provides:          my-java-app
# Required-Start:    $remote_fs $network
# Required-Stop:     $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description:       My Java Application
# Short-Description: Enable My Java Application
### END INIT INFO

DESC="my java app"
NAME=my-java-app
PIDFILE=/var/run/$NAME.pid
RUN_AS=ubuntu
WORK_DIR=/home/ubuntu/admin
DAEMON=/usr/bin/mvn
DAEMON_OPTS="spring-boot:run"

# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
. /lib/lsb/init-functions

do_start() {
    start-stop-daemon --start --quiet --make-pidfile --pidfile $PIDFILE \
        --background \
        --chuid $RUN_AS \
        --chdir $WORK_DIR \
        --exec $DAEMON -- $DAEMON_OPTS
}

do_stop() {
    start-stop-daemon --stop --quiet --pidfile $PIDFILE
    if [ -e $PIDFILE ]
        then rm $PIDFILE
    fi
}

case "$1" in
    start)
    echo -n "Starting $DESC: $NAME"
    do_start
    echo "."
    ;;
    stop)
    echo -n "Stopping $DESC: $NAME"
    do_stop
    echo "."
    ;;
    restart)
    echo -n "Restarting $DESC: $NAME"
    do_stop
    sleep 1
    do_start
    echo "."
    ;;
    status)
    status_of_proc -p $PIDFILE "$DAEMON" "$NAME" && exit 0 || exit $?
    ;;
    *)
    echo "usage: $NAME {start|stop|restart}"
    exit 1
    ;;
esac

Make it belong to root, make it executable, and set it up to run on startup with:

sudo chown root:root /etc/init.d/my-java-app
sudo chmod 755 /etc/init.d/my-java-app
sudo update-rc.d my-java-app defaults

To start the service you can run:

sudo service my-java-app start

To stop the service you can run:

sudo service my-java-app stop

This is based on a simplified version of the /etc/init.d/skeleton file included by Ubuntu.

The man page for start-stop-daemon is worth looking at if you want to tweak this further.b

🌐
DZone
dzone.com › coding › frameworks › run your java app as a service on ubuntu
Run Your Java App as a Service on Ubuntu
October 20, 2017 - Ubuntu has a built-in mechanism to create custom services, enabling them to get started at system boot time and start/stop them as a service. In this post, I am going to share a simple and elegant way to create a service wrapper for your JAR ...
🌐
Medium
medium.com › @sulmansarwar › run-your-java-application-as-a-service-on-ubuntu-544531bd6102
Run your Java application as a Service on Ubuntu | by Sulman Sarwar | Medium
October 15, 2023 - Ubuntu has built in mechanism to create custom services, enable them to get started at system boot time and start/stop them as a service. In this post I am going to share a simple and elegant way to create a service wrapper for your jar file ...
🌐
Developers Journal
developersjournal.in › home › java › how to run java app as a service in ubuntu?
How to Run Java App as a Service in Ubuntu? | Developers Journal
October 7, 2017 - Copy and Paste the following into newly created service file: /etc/systemd/system/java-webapp.service
🌐
ComputingForGeeks
computingforgeeks.com › home › how to run java jar application with systemd on linux
How To Run Java Jar Application with Systemd on Linux [Guide]
August 6, 2024 - Systemd is a system and service manager for Linux. It is now the default init system for a number of Distributions including Ubuntu, Debian, CentOS, Arch Linux e.t.c.Original content from ...
Top answer
1 of 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=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=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&
2 of 16
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.

🌐
Baeldung
baeldung.com › home › administration › service management › run a java application as a service on linux
Run a Java Application as a Service on Linux | Baeldung on Linux
March 18, 2024 - Before that, however, we should notify systemd that it has to rebuild its internal service database. Doing this will make it aware of the new system unit we introduced. We can do this by passing the daemon-reload command to systemctl. Now, we’re ready to run all the commands we mentioned: sudo systemctl daemon-reload sudo systemctl start javasimple.service sudo systemctl status javasimple.service ● javasimple.service - My Java driven simple service Loaded: loaded (/etc/systemd/system/javasimple.service; disabled; vendor preset: disabled) Active: active (running) since Sun 2021-01-17 20:10:19 CET; 8s ago Main PID: 8124 (java) CGroup: /system.slice/javasimple.service └─8124 /path/to/jvmdir/bin/java -jar javaapp.jar
🌐
GitHub
gist.github.com › pavankjadda › 972dcde09463a21d8c4727ab1f6017dd
How to create Spring Boot app as Ubuntu service? · GitHub
[Unit] Description=Employee Spring Boot application service [Service] User=ubuntu ExecStart=/usr/bin/java -Dspring.profiles.active=prod -Dusername="admin" -Dpassword="Password129388" -jar /home/ubuntu/Employee/target/employee-0.0.1-SNAPSHOT.jar ExitStatus=143 TimeoutStopSec=10 Restart=on-failure RestartSec=5 [Install] WantedBy=multi-user.target ·
Find elsewhere
🌐
Medium
medium.com › programming-with-pierre › start-a-java-service-from-jar-on-an-ubuntu-instance-40dff8acc4a5
Start a JAVA service from JAR on an Ubuntu instance | by Pierre Janineh | Coding with PierreJanineh | Medium
August 8, 2023 - Start a JAVA service from JAR on an Ubuntu instance Got a Jar file but struggling to execute it? Learn how to run a Jar file in Ubuntu and other Linux distributions. Starting: Make sure your …
🌐
The Art of Code
artofcode.wordpress.com › 2025 › 01 › 14 › running-java-application-as-a-linux-service
Running Java application as a Linux service | The Art of Code
April 1, 2025 - This post is a continuation of my previous one, “Turning a laptop into a Java app server in 2025”. In this part we’re going to setup a Java application as a service in Linux. We will use Systemd for this so a service will be automatically started on system boot and it will be run again if it crashed. All this was verified by myself on Ubuntu Server 24.04.1 LTS.
🌐
Medium
medium.com › @ameyadhamnaskar › running-java-application-as-a-service-on-centos-599609d0c641
Running Java application as Linux service with systemd | by Ameya Dhamnaskar | Medium
January 24, 2019 - I assume you have your java code ... Name_Of_File.jar · Ubuntu has a built-in mechanism to create custom services, enabling them to get started at system boot time and start/stop them as a service....
🌐
FOSS Linux
fosslinux.com › home › linux distributions › debian & ubuntu › ubuntu › how to run jar files and services on ubuntu (2026 guide)
How to Run JAR Files and Services on Ubuntu (2026 Guide)
April 24, 2026 - Learn how to install Java, run JAR files, manage JVM memory, and create systemd background services on Ubuntu. Professional DevOps strategies for Java apps.
🌐
Ask Ubuntu
askubuntu.com › questions › 947344 › how-to-run-a-jar-application-as-a-service-start-on-boot-of-system
systemd - How to run a .jar application as a service (Start on boot of system) - Ask Ubuntu
August 18, 2017 - Show us how your systemd service looked like. ... i) Create your bash script called mystartup.sh to run the .jar file in /etc/init.d/ directory(login as root) ... A simple way is provided by the upstart package, after installing it just save the following script in etc/init/onshutdown.conf : # /etc/init/onshutdown.conf start on starting rc RUNLEVEL=2 # execute single command exec /bin/sh /path/to/mystartup.sh # alternatively without an extra script script java -jar /path/to/jarfile.jar # your commands here end script
🌐
LinkedIn
linkedin.com › pulse › how-run-java-jar-application-systemd-linux-service-dhaval-gajjar
How to run java jar application with systemd on Linux as service
December 14, 2022 - [Unit] Description=Email service Requires=network.target remote-fs.target After=network.target remote-fs.target [Service] Type=simple User=ubuntu WorkingDirectory=/opt/apps/ ExecStart=/usr/bin/java -jar /opt/apps/email-1.0.0.jar Restart=on-failure RestartSec=10 [Install] WantedBy=multi-user.target
🌐
Fabian Lee
fabianlee.org › 2018 › 04 › 15 › java-spring-boot-application-as-a-service-using-systemd-on-ubuntu-16-04
Java: Spring Boot application as a service using systemd on Ubuntu 16.04 | Fabian Lee : Software Engineer
April 17, 2018 - With Ubuntu 16.04, we can use the built-in systemd supervisor to run a Spring Boot application at boot time. This will enable the Java based service to run in the background as a distinct user, fully integrated into the syslog framework.
🌐
CyberPanel
cyberpanel.net › blog › install-java-on-linux-ubuntu
Install Java on Linux Ubuntu Easily: Your Ultimate Guide
April 7, 2025 - You cover everything, from picking between JRE and JDK to compiling your first program. Need a simpler way to host Java applications? CyberPanel is your best partner to launch, manage, and secure Java-based services.
🌐
Ubuntu
ubuntu.com › tutorials › install-jre
Install the Java Runtime Environment | Ubuntu
The Java Runtime Environment (JRE) is required to run Java programs. Nowadays there are many JRE packages available from a variety of projects and companies, but the two most popular on Ubuntu are OpenJDK and Oracle HotSpot.
🌐
VPS Server
vpsserver.com › install-java-ubuntu
Install Java Ubuntu | Fast and Simple Java Installation
December 14, 2023 - Java's adaptability makes it a ... applications. On Ubuntu, Java provides a robust foundation for desktop development and ensures seamless compatibility for lightweight mobile experiences....