An Application Bundle / Shell Script for Starting and Stopping the Tomcat Server

Posted on October 3, 2007
Filed under Mac, Programming, Scripts

toggleTomcat application bundle icon I’ve recently had to install tomcat on my localhost to continue developing the Flex application I’m currently doing at work since it connects to web services that are implemented in Java. I wanted an easy way to start and stop the tomcat server, so I made this nice little app for it with the help of a bash shell script and Platypus.

Update (Dec 1, 07): Due to the fact that the icon-assigning script that I’ve made doesn’t support the 512×512 icon resolution introduced in Leopard, I’ve made a new version of this one, using the “seticon” utility from osxutils. Download: the app / the sources

You can just download the application bundle, go to its Resources folder, edit tomcatDir.cfg‘s contents to match your Tomcat installation folder, and you’re done (you can also do this in Finder by right-clicking on the application icon and selecting “Show Package Contents”):

nano ./toggleTomcat.app/Contents/Resources/tomcatDir.cfg

You can then start and stop tomcat by simply double-clicking on the application icon. The icon will change on every click to reflect tomcat’s current status.

Please note that your system needs to fulfill the following prerequisites for this to work:

Or if you want, you can take the script and its support files, edit them to your liking and make the whole thing into an application bundle yourself with Platypus. The directions for doing so are in the comments part in the beginning of the script.

The contents of the script are as follows (some unnecessary whitespace removed):

#!/bin/bash

#
# ----------------------------------------------------------------
#
# DESCRIPTION:
#
# Toggles tomcat on/off (runs startup.sh and shutdown.sh) on the
# local host
#
# NOTE: only to be used with Platypus - do not run this
#       from the command line!
#
# (c) Ali Rantakari, 2007
#     http://hasseg.org
#     fromtoggletomcatscript.20.hasseg@spamgourmet.com
#
# ----------------------------------------------------------------
#
# REQUIRES:
#
# - Growl and growlnotify (growlnotify is in the Growl
#   distribution package)
# - OS X developer tools (install from OS discs)
# - A Tomcat installation
# - Platypus (to make this into an app)
#
# ----------------------------------------------------------------
#
# SETTING IT UP:
#
# 1) Configure the script to your liking
#
# 2) Add the path to your tomcat installation folder to
#    tomcatDir.cfg
#
# 3) Make it into an application bundle with Platypus
#    
#    - select "runs in background" from the advanced options
#    - include the .icns files, the .png and the .cfg in the
#      bundle
#
# 4) Run the application by double-clicking on it
#
# ----------------------------------------------------------------
#
#



THISDIR="$1/Contents/Resources"



# get the full path to the /bin directory of
# your tomcat installation from config file
#
TOMCATBINDIR="`cat \"$THISDIR/tomcatDir.cfg\" | sed 's/ //g'`/bin"




# ----------------------------------------------------------------
#
# SETTINGS:
#
#



# the level of notifications you want to receive
#
# 0 = totally quiet
# 1 = inform of tomcat status when running
# 2 = inform of everything
#
NOTIFICATIONLEVEL=2



# locations of CLI apps used
#
# custom: (you need to get these yourself)
#
GROWLNOTIFY=/usr/local/bin/growlnotify
STARTUPSH="$TOMCATBINDIR"/startup.sh
SHUTDOWNSH="$TOMCATBINDIR"/shutdown.sh
#
# system: (these should be here)
#
OSASCRIPT=/usr/bin/osascript
TOUCH=/usr/bin/touch
FILE=/usr/bin/file
#
# dev tools: (from OS X disc)
#
SETFILE=/Developer/Tools/SetFile
REZ=/Developer/Tools/Rez


# the name of this application
# (to send to growlnotify)
#
THISAPP="Toggle Tomcat script"


# whether to use growl or stdout (or both) for notifications
#
# "stdout" -> notifications will be printed to stdout
# "growl"  -> notifications will be sent to growl
# "both"   -> notifications will be sent to growl _and_
#             printed to stdout
#
NOTIFICATIONS="growl"


# the icon to show in growl notifications
#
GROWLICON="$THISDIR/tomcat.png"




# - - - - - - - - - - - - - - - - - - - - - -
# settings end here.
# ----------------------------------------------------------------
#



# Script IMPLEMENTATION begins here ---------------------------
# -------------------------------------------------------------
#


# --- functions ------


# parameters:
#
# $1 = Title
# $2 = Message
# $3 = Minimum notification level
# $4 = Location of icon to show (or "" for no icon)
# $5 = "sticky" to make the growl message sticky
show_message()
{
    if [ $NOTIFICATIONLEVEL -gt $(( $3 - 1 )) ] || [ $VERBOSEMODE == 1 ];then
       
        if [ "$NOTIFICATIONS" != "stdout" ]; then
           
            if [ -e "$4" ] && [ "$4" != "" ];then
                THISGROWLICON="$4"
            else
                THISGROWLICON="$GROWLICON"
            fi
           
            STICKY=""
            if [ "$5" == "sticky" ];then
                STICKY="--sticky"
            fi
           
            $GROWLNOTIFY -n "$THISAPP" -t "$1" -m "$2" --image "$THISGROWLICON" $STICKY
           
        fi
       
        if [ "$NOTIFICATIONS" != "growl" ];then
           
            echo "** $1"
            echo "   $2"
            echo " "
           
        fi
   
    fi
}




# params:
#
# $1 = icon to set
# $2 = target
#
assign_icon()
{
   
    # see if parameters are set
    if [ -n "$1" ] || [ -n "$2" ];then
       
        # determine if second parameter is a file or a folder
        if [ "`$FILE -b \"$2\"`" == "directory" ];then
           
            echo "read 'icns' (-16455) \"$1\";" | $REZ -o "`printf "$2/Icon\r"`"
           
        else
           
            echo "read 'icns' (-16455) \"$1\";" | $REZ -o "$2"
           
        fi
       
        $SETFILE -a "C" "$2"
       
        if [ "$3" != "-r" ];then
            TARGETSPATH=`dirname $2`
            TARGETSFULLPATH="`cd $TARGETSPATH; pwd`/`basename $2`"
           
            $OSASCRIPT -e "tell application \"Finder\" to update POSIX file \"$TARGETSFULLPATH\""
        fi
       
    fi
   
}




tomcat_status()
{
   
    if [ "`ps xwww | grep -v grep | grep -c tomcat`" == "0" ];then
        echo "off";
    else
        echo "on";
    fi
   
}


# --- /functions ------




# script runs from here:
# --------------------------------------------



if [ -e "$STARTUPSH" ] && [ -e "$SHUTDOWNSH" ];then
   
    if [ "`tomcat_status`" == "off" ];then
       
        $STARTUPSH
       
        case $? in
            0) SUCCESS="true";;
            *) SUCCESS="false";;
        esac
       
        if [ "`tomcat_status`" == "off" ];then
            SUCCESS="false"
        fi
       
        if [ "$SUCCESS" == "true" ];then
            show_message "Tomcat is ON" "Tomcat has been successfully started" 1
            assign_icon "$THISDIR/iconOff.icns" "$1"
        else
            show_message "Error turning tomcat on!" "There was an error while turning tomcat on!" 1 "" "sticky"
        fi
       
    else
       
        $SHUTDOWNSH
       
        case $? in
            0) SUCCESS="true";;
            *) SUCCESS="false";;
        esac
       
        if [ "$SUCCESS" == "true" ];then
            show_message "Tomcat is OFF" "Tomcat has been successfully stopped" 1
            assign_icon "$THISDIR/iconOn.icns" "$1"
        else
            show_message "Error turning tomcat off!" "There was an error while turning tomcat off!" 1 "" "sticky"  
        fi
       
    fi
   
else
    show_message "Error! Can not find tomcat!" "Can not find \"$STARTUPSH\"" 0 "" "sticky"
    exit 127
fi

Comments

2 Responses to “An Application Bundle / Shell Script for Starting and Stopping the Tomcat Server”

Show/hide comments & reply form: