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&
Answer from PbxMan on Stack Overflow
🌐
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 - Since we use a shell script to start the service, the JVM will be started by the shell (bash) process. This operation is known as fork, and it’s why we set the service Type to forking.
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.

Discussions

Running Java process as Service in Linux - Stack Overflow
I need to run a Java process as a service in (Red Hat 6.4) Linux (It needs to run at boot time and stay up). I have it mostly working, except for it doesn't seem to status correctly in the "Service More on stackoverflow.com
🌐 stackoverflow.com
June 12, 2013
linux - How do I run Java as a service on Ubuntu? - Stack Overflow
No log is created and the java app is not started.. ? ... It does.. I will get someone to take a look at this in 2016.. now it's time for X-mas ;) ... This Q is not about programming as defined for StackOverflow. It may be more appropriate on the related sites askubuntu.com OR ServerFault.com. Consider using the flag link at the bottom of your Q and ask the moderator to move it there. Good luck. ... @shellter how is it not programming? Service ... More on stackoverflow.com
🌐 stackoverflow.com
How to execute linux service from java code - Stack Overflow
Create new user with LimitedRoot capabilities and use VISUDO to allow the user to run systemctl/systemd services without entering a password. ... Your welcome. login into your Linux machine and type sudo su and then enter your admin password and then type visudo command 2020-08-27T12:17:33.09Z+00:00 More on stackoverflow.com
🌐 stackoverflow.com
Is there an easy, free way to make a server for only 2 people?
Well, you could just set up your own server . Costs nothing, and pretty much anyone can do it with enough effort. More on reddit.com
🌐 r/Minecraft
18
10
June 27, 2018
🌐
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.
🌐
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 - sudo systemctl daemon-reload sudo systemctl enable Service_Name sudo systemctl start Service_Name sudo systemctl stop Service_Name · This should get your Java Application up and running as a System Service. Enjoy and Feel free to reach out to me in case of any doubts/discrepancies.
🌐
TutorialsPoint
tutorialspoint.com › run-a-java-application-as-a-service-on-linux
Run a Java Application as a Service on Linux
January 24, 2023 - In containerized environments like ... as a Linux service involves creating a proper service file, registering it with the system service manager, and using standard commands for control....
🌐
@Upnxtblog
upnxtblog.com › home › 2019 › june › 12 › how to run java application as service on linux
How to run Java application as service on Linux
April 28, 2020 - Next step is to start the service by systemctl start <service name> command and you can check the status by using systemctl status <service name> command. ... As the last step, if we want to stop the service, use systemctl stop <service name> ...
🌐
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 file so you can run it as a service. Here we go. ... [Unit] Description=My Webapp Java REST Service [Service] User=ubuntu # The configuration file application.properties should be here: #change this to your workspace WorkingDirectory=/home/ubuntu/workspace #path to executable.
🌐
DEV Community
dev.to › davey › running-a-java-application-as-a-service-1c0o
Running a Java Application as a Service - DEV Community
August 11, 2021 - In this post we've seen how to run a Java application as a service using systemd, and how to control it. We've seen how to start the service at boot time and how to restart the service upon failure.
Find elsewhere
🌐
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 - It is now the default init system ... under a /opt/prod/directory. As a rule of thumb, we need to add a system user which will run the application with systemd. Start ......
🌐
Medium
snehalchaure.medium.com › running-an-application-as-a-service-with-systemctl-63a51dece4c5
Run an Application as a Service with Systemctl | by Snehal Chaure | Medium
June 15, 2023 - Let’s start….. First create a simple java application ‘App.java’ using springboot. Run the ‘mvn package’ command to build the package using maven and the corresponding jar file will be created. In Java, JAR stands for Java ARchive, whose format is based on the zip format. The JAR files format is mainly used to aggregate a collection of files into a single one. To run java app as system service you need to create a unit file with ‘.service’ suffix in ‘/etc/systemd/system’ directory.
🌐
Tanuki Software
wrapper.tanukisoftware.com › doc › english › launch-nix.html
Launching Your Application (Linux / UNIX) - Java Service Wrapper
Launching as Windows Service · Launching as Daemon Linux/Unix · Starting at Reboot (Debian) Starting at Reboot (Solaris) Configurations Overview · JVM Configurations · Logging Configurations · Windows Configurations · Linux/UNIX Configurations · WrapperW (GUI) Configurations ·
🌐
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 and run it as a service. Here we go: ... [Unit] Description=My Webapp Java REST Service [Service] User=ubuntu # The configuration file application.properties should be here:#change this to your workspace WorkingDirectory=/home/ubuntu/workspace#path to executable.
🌐
Enesaltinkaya
blog.enesaltinkaya.com › linux › howto-create-a-linux-service-for-your-java-application
How to Create a Linux Service for Your Java Application | Enes Altınkaya
February 23, 2018 - [Unit] Description=listener After=syslog.target [Service] User=root ExecStart=/usr/bin/java -jar ....... SuccessExitStatus=143 [Install] WantedBy=multi-user.target · use systemctl enable command to start your service on boot
🌐
Source-code
source-code.biz › snippets › java › 7.htm
How to run a Java Program as a daemon (service) on Linux (SUSE/openSUSE) using a shell script
Start/stop the Java daemon process, according to the Linux system init script convention. Run the Java daemon as a different (non-root) Linux user. Log error messages and redirect StdOut/StdErr output into a log file. Install/uninstall the daemon as a Linux service.
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

🌐
LinuxVox
linuxvox.com › blog › start-a-jar-file-like-service-in-linux
How to Run a JAR File as a Linux Service: Start, Stop, and Manage with systemd — linuxvox.com
We’ll create a custom .service file for your JAR and place it in /etc/systemd/system/ (the standard directory for system-wide services). ... Move your JAR to a permanent location (avoid temporary directories like Downloads). For example: sudo mkdir -p /opt/myapp # Create a directory for your app sudo cp /path/to/your/myapp.jar /opt/myapp/ # Move JAR to the directory · Verify the JAR runs manually (to ensure no issues before creating the service): java -jar /opt/myapp/myapp.jar If the app starts successfully, stop it with Ctrl+C and proceed.
🌐
Stack Overflow
stackoverflow.com › questions › 63556969 › how-to-execute-linux-service-from-java-code
How to execute linux service from java code - Stack Overflow
execute the command line with the correct function: Process exec(String[] cmdarray) This method executes the specified command and arguments in a separate process. ... yes I think this is the problem (password is problem), is there a way to execute these command without providing password ? ... create user and give him rights to execute systemctl command, see this thread allowing user to run systemctl/systemd services without password ... your java code should be: Runtime.getRuntime().exec(new String[]{"systemctl", "stop", "appService.service"});