How to run Java application on startup of Ubuntu Linux
Ideally you should create a service wrapper for your java application and then make this service run on startup example here.
Usesudo update-rc.d mytestserv defaults
to run your service wrapper on startup on Ubuntu
Linux start-up script for java application
If you want to run the app as linux daemon (service) checkout Java Wrapper: http://wrapper.tanukisoftware.com/doc/english/download.jsp
Check this answer too (is for windows, but for linux changes a little bit): How to install a Java application as a service
How to start the java (.jar) application at boot time of ubuntu
Check this link to see how to run a script during Ubuntu startup
How to run a script at the start up of Ubuntu?
The script could be a java -jar jarname call.
Off course java has to be installed.
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&
Linux Launch java program on startup (EC2 instance)
It's possible you can create a script java_server_launch.sh
like this:
#! /usr/bin/sh
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
JAVA=/usr/bin/java
MY_SERVER=/home/your_username/DocumentManager/DocumentServer-0.2.jar
USER=your_username
/bin/su - $USER -c "$JAVA -jar $MY_SERVER &"
Put your script under /etc/init.d directory, and then use the command:
update-rc.d java_server_launch.sh defaults
more on update-rc.d command by using man update-rc.d
.
Hope this help.
Regards.
Ubuntu System Start-up: What command to insert to run java app?
java -jar /home/john/this-folder/app.jar [optional arg if any]
- java - the Java application launcher
Related Topics
Why Isn't Calling a Static Method by Way of an Instance an Error For the Java Compiler
Scanner Double Value - Inputmismatchexception
Difference Between Dto, Vo, Pojo, Javabeans
Why Can't Static Methods Be Abstract in Java
How to Retrieve a File from a Server Via Sftp
How to Access Static Resources When Mapping a Global Front Controller Servlet on /*
Maven Dependencies Are Failing With a 501 Error
Calling a Servlet from Jsp File on Page Load
How to Check the Jdk Version Used to Compile a .Class File
What Is This Date Format? 2011-08-12T20:17:46.384Z
A Quick and How to Join Array Elements With a Separator (The Opposite of Split) in Java
Spring Security Does Not Allow CSS or Js Resources to Be Loaded
Add Buttons to Tabs and Tab Area Javafx
When Exactly Is It Leak Safe to Use (Anonymous) Inner Classes