Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Launch Java application at startup on Centos

I need to launch a Java application on Centos (5.9) startup.

I am trying to start a simple script (named "lanzar.sh") on Centos at boot time:

#!/bin/sh
cd /home/someuser/Desktop/Dist
java -jar SomeApp.jar

I append the line "/bin/sh /home/someuser/Desktop/Dist/lanzar.sh" to /etc/rc.d/rc.local. But the java application does not start. I have:

  • Granted 755 rights to the /etc/rc.d/rc.local file
  • Write the content of the "lanzar.sh" into /etc/rc.d/rc.local. Separated with semicolon, and in different lines.
  • Changing "lanzar.sh" of location.
  • Other things, taken from other threads that did not work for me.

My rc.loca looks like:

#!/bin/sh

#

# This script will be executed *after* all the other init scripts.

# You can put your own initialization stuff in here if you don't

# want to do the full Sys V style init stuff.
#
#Some comment

#Some comment

#Some comment
touch /var/lock/subsys/local
/bin/sh /home/fernando/Desktop/Dist/lanzar.sh

Note: I know similar questions have been asked before, but after testing many of the answers that I have found by googling with no success, I had to ask this myself.

like image 603
Ferite Avatar asked Mar 19 '23 23:03

Ferite


1 Answers

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.

like image 119
David W. Avatar answered Apr 02 '23 00:04

David W.