Start a jar file like service in linux
You need a Service Wrapper to run the Jar file.
There are examples and instructions for init.d here. or for systemd (ubuntu 16+) here
Run a Java Application as a Service on Linux
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&
Run java jar file on a server as background process
You can try this:
#!/bin/sh
nohup java -jar /web/server.jar &
The & symbol, switches the program to run in the background.
The nohup utility makes the command passed as an argument run in the background even after you log out.
Related Topics
How to Change Color of the Back Arrow in the New Material Theme
What Is the Jasperrepots-Fonts Jar for and How to Use It
Aes Java Encoding, Ruby Decoding
Is It Unnecessary to Put Super() in Constructor
Sockets: Discover Port Availability Using Java
How to Decompile a Whole Jar File
How to Sort a List Alphabetically
Gmail Rest API:400 Bad Request + Failed Precondition
Why Is String.Chars() a Stream of Ints in Java 8
How to Add Native Library to "Java.Library.Path" with Eclipse Launch (Instead of Overriding It)
How to View My Realm File in the Realm Browser
Initializing Multiple Variables to the Same Value in Java
Generate All Combinations from Multiple Lists
How to Convert a Char Array Back to a String