5

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.

4
  • What does that java app do? Does it require access to certain user data? Does it require an X server? Does it depend on certain environment variables to be set? Does the application log any error messages when you try this? If you add some echo lines into that script (to a log file) does the log file get written to? Commented Apr 8, 2014 at 1:24
  • By your sugestion, I added the line -echo "Some log" > /var/log/mylog- before the other commands. The log was not generated. So I think the problem is the rc.local not being executed at all. I have to say, before the issue starts to happen, I replaced the original file, but then I ran -chmod 755 /etc/rc.d/rc.local- Commented Apr 8, 2014 at 1:40
  • Put log commands in rc.local and see what happens. Remember that rc.local is not running as your user and so may or may not have permissions to files in your user's home directory/etc. Commented Apr 8, 2014 at 1:46
  • @EtanReisner , actually that was what I did. I put the log line (echo "Some log" > /var/log/mylog) before the first command in rc.local, but the log file was not created. That's why I think rc.local is not being executed. As for the permissions on the user directories, when I run the application from command line as root, it works fine, nevertheless I am going to do my test placing the app in a root subdirectory. Commented Apr 8, 2014 at 3:22

1 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Creating a /etc/init.d script based on Jenkin's and other example that I found, I was able to start the app as service. The command to add the script to the startup was -/sbin/chkconfig --level 345 SomeApp on-

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.