Writing an init script for a Java application

UPDATE, Oct, 1st 2014

This is an old post (from 5 years ago when I writing this update) and the solution isn’t the best. So take a look at the comments – specially that one by Tinoco.


The need make us study topics we think we’ll never need. Today I had to write a simple bash program to start (and stop) a Java application, a simple init script.

After much searching, I’ve found some simple explanations for some specific points of the bash script syntax.

I don’t know who is on the right side of this story, but seems that professionals who write manuals and tutorials for this language doesn’t think like common web programmers, such as Java or PHP programmers. I sought to much on Google to find things like how I could associate the returning value of a function to a variable.

But finally, I’ll leave aside the claims and move to example:

#!/bin/sh
#
# init script for a Java application
#

# Check the application status
#
# This function checks if the application is running
check_status() {

  # Running ps with some arguments to check if the PID exists
  # -C : specifies the command name
  # -o : determines how columns must be displayed
  # h : hides the data header
  s=`ps -C 'java -jar /path/to/application.jar' -o pid h`

  # If somethig was returned by the ps command, this function returns the PID
  if [ $s ] ; then
    return $s
  fi

  # In any another case, return 0
  return 0

}

# Starts the application
start() {

  # At first checks if the application is already started calling the check_status
  # function
  check_status

  # $? is a special variable that hold the "exit status of the most recently executed
  # foreground pipeline"
  pid=$?

  if [ $pid -ne 0 ] ; then
    echo "The application is already started"
    exit 1
  fi

  # If the application isn't running, starts it
  echo -n "Starting application: "

  # Redirects default and error output to a log file
  java -jar /path/to/application.jar >> /path/to/logfile 2>&1 &
  echo "OK"
}

# Stops the application
stop() {

  # Like as the start function, checks the application status
  check_status

  pid=$?

  if [ $pid -eq 0 ] ; then
    echo "Application is already stopped"
    exit 1
  fi

  # Kills the application process
  echo -n "Stopping application: "
  kill -9 $pid &
  echo "OK"
}

# Show the application status
status() {

  # The check_status function, again...
  check_status

  # If the PID was returned means the application is running
  if [ $? -ne 0 ] ; then
    echo "Application is started"
  else
    echo "Application is stopped"
  fi

}

# Main logic, a simple case to call functions
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  status)
    status
    ;;
  restart|reload)
    stop
    start
    ;;
  *)
    echo "Usage: $0 {start|stop|restart|reload|status}"
    exit 1
esac

exit 0

Just change the paths to jar (/path/to/application.jar) and log file (/path/to/logfile) and it works!

You can also change the application executable to any other program instead java -jar ...

Some useful links:

Advertisements

8 thoughts on “Writing an init script for a Java application

  1. Hola, contiene algunos errores, te paso una sintaxis corregida y probada
    Busca especificamente el programa, en este caso le espifico el nombre de mi .Jar
    ControlServer_SQL.jar
    Saludos, espero aportar.
    ——————————————————————————————

    #!/bin/sh
    #
    # init script for a Java application
    #

    # Check the application status
    #
    # This function checks if the application is running
    check_status() {

    # Running ps with some arguments to check if the PID exists
    # -C : specifies the command name
    # -o : determines how columns must be displayed
    # h : hides the data header
    touch /home/ronaldo/service/pidProcess
    cat /dev/null > /home/ronaldo/service/pidProcess
    ps -e -o pid,cmd > /home/ronaldo/service/pidProcess
    s=`cat /home/ronaldo/service/pidProcess | grep ControlServer_SQL.jar`
    s=`echo $s | cut -d’ ‘ -f 1`
    # s=`ps -C ‘java -jar /home/ronaldo/service/ControlServer_SQL.jar’ -o pid h`
    echo $s
    # If somethig was returned by the ps command, this function returns the PID
    if [ $s ] ; then
    return $s
    fi

    # In any another case, return 0
    return 0

    }

    # Starts the application
    start() {

    # At first checks if the application is already started calling the check_status
    # function
    check_status

    # $? is a special variable that hold the “exit status of the most recently executed
    # foreground pipeline”
    pid=$?

    if [ $pid -ne 0 ] ; then
    echo “The application is already started”
    exit 1
    fi

    # If the application isn’t running, starts it
    echo -n “Starting application: ”

    # Redirects default and error output to a log file
    java -jar /home/ronaldo/service/ControlServer_SQL.jar >> /home/ronaldo/service/logfile 2>&1 &
    echo “OK”
    }

    # Stops the application
    stop() {

    # Like as the start function, checks the application status
    check_status

    pid=$?

    if [ $pid -eq 0 ] ; then
    echo “Application is already stopped”
    exit 1
    fi

    # Kills the application process
    echo -n “Stopping application: ”
    kill -9 $pid &
    echo “OK”
    }

    # Show the application status
    status() {

    # The check_status function, again…
    check_status

    # If the PID was returned means the application is running
    if [ $? -ne 0 ] ; then
    echo “Application is started”
    else
    echo “Application is stopped”
    fi

    }

    # Main logic, a simple case to call functions
    case “$1” in
    start)
    start
    ;;
    stop)
    stop
    ;;
    status)
    status
    ;;
    restart|reload)
    stop
    start
    ;;
    *)
    echo “Usage: $0 {start|stop|restart|reload|status}”
    exit 1
    esac

    exit 0

  2. Oh yeah and one other thing:
    You also can’t use ‘ps -C’ like that. This ps filter only works on the binary. You will get the first process running on ‘/usr/bin/java’ and probably more than one result (if you have more java apps or if your application forks itself).
    I use this:
    pid=`ps -e -o pid,cmd –sort cmd | grep “java -jar $JAR” | grep -v “grep ” | tail -n1 | awk ‘{ print $1 }’`

  3. Just a quick comment to an old post, because it shows up high on google:
    return $s in check_status does not work, cause an exit status has a max value of 255 (you will get the result modulo 256). I’ve rewritten this to just set a global variable (pid=`blabla`) and referring to that in the rest of the script

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s