Linux Start-Up Script for Java Application

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

Use

sudo update-rc.d mytestserv defaults to run your service wrapper on startup on Ubuntu

Startup Script to Run a Jar

The --pid parameter is invalid on your OSX. You should change ps --pid on isrunning() to ps -p. This will work on Linux and OSX.

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&

Launch Java application at startup on Centos

I highly recommend that you explore the /etc/init.d directory of your server and the /etc/rc3.d directory. See how the names of the files in /etc/rc3.d are symbolically linked to the names in the /etc/init.d directory. Notice how the files in /etc/rc3.d all start with Sxx or Kxxwherexx is a number between 00 to 99.

What I am about to tell you is officially all wrong. These startup scripts are way more complicated today that what I describe, but it's a basic outline of what's going on.

In standard Unix and Linux, startup scripts were normally stored in /etc/init.d and then linked to the /etc/rcX.d directory where X stood for what was called the Init States of the server. (Yes, I'm linking to an SCO Unix page, but they were all pretty similar).

Note that Init State 3 is running in multi-user mode and that all the daemons are started. This is why I am telling you to look in /etc/rc3.d.

When the server enters that init state, it runs all of the script starting with S in alphabetical order. It runs each script with the parameter start after it. So, S01xxxx starts before S03xxx which starts before S99xxxxx.

When the server exits that init state, it runs all of the scripts that start with K in alphabetical order, and passes the stop parameter to them.

Now, Centos, Redhat, and Fedora setup handles a lot of this for you. You specify which service you depend upon, and it figures out startup and shutdown order. However, nothing is preventing you from munging a startup script and creating your own links.

By the way, speaking about Java programs that startup and shutdown... Jenkins is a Java program that's started in a very similar way as your program. Here's the /etc/init.d script I got off of Jenkins website:

#!/bin/bash
#
# Startup script for Jenkins
#
# chkconfig: - 84 16
# description: Jenkins CI server

# Source function library.
. /etc/rc.d/init.d/functions
[ -z "$JAVA_HOME" -a -x /etc/profile.d/java.sh ] && . /etc/profile.d/java.sh

JENKINS_HOME=/var/jenkins
WAR="$JENKINS_HOME/jenkins.war"
LOG="/var/log/jenkins.log"
LOCK="/var/lock/subsys/jenkins"
export JENKINS_HOME

RETVAL=0

pid_of_jenkins() {
pgrep -f "java.*jenkins"
}

start() {
[ -e "$LOG" ] && cnt=`wc -l "$LOG" | awk '{ print $1 }'` || cnt=1

echo -n $"Starting jenkins: "

cd "$JENKINS_HOME"
nohup java -jar "$WAR" --httpPort=-1 --ajp13Port=8010 --prefix=/jenkins >> "$LOG" 2>&1 &

while { pid_of_jenkins > /dev/null ; } &&
! { tail +$cnt "$LOG" | grep -q 'Winstone Servlet Engine .* running' ; } ; do
sleep 1
done

pid_of_jenkins > /dev/null
RETVAL=$?
[ $RETVAL = 0 ] && success $"$STRING" || failure $"$STRING"
echo

[ $RETVAL = 0 ] && touch "$LOCK"
}

stop() {
echo -n "Stopping jenkins: "

pid=`pid_of_jenkins`
[ -n "$pid" ] && kill $pid
RETVAL=$?
cnt=10
while [ $RETVAL = 0 -a $cnt -gt 0 ] &&
{ pid_of_jenkins > /dev/null ; } ; do
sleep 1
((cnt--))
done

[ $RETVAL = 0 ] && rm -f "$LOCK"
[ $RETVAL = 0 ] && success $"$STRING" || failure $"$STRING"
echo
}

status() {
pid=`pid_of_jenkins`
if [ -n "$pid" ]; then
echo "jenkins (pid $pid) is running..."
return 0
fi
if [ -f "$LOCK" ]; then
echo $"${base} dead but subsys locked"
return 2
fi
echo "jenkins is stopped"
return 3
}

# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
stop
start
;;
*)
echo $"Usage: $0 {start|stop|restart|status}"
exit 1
esac

exit $RETVAL

It'll give you something to work with.

Start java application in background automatically after boot on embedded linux

I solve this problem with starting my device's watchdog like application in my script. My application works in background but bash scripts stays open until my application stops.



Related Topics



Leave a reply



Submit