Sunday, September 22, 2013

Use Launch Daemon to Start Tomcat Automatically on Mac

Start tomcat automatically on Mac.

1. create launchd_wrapper.sh at tomcat/bin
2. create /Library/LaunchDaemons/org.apache.tomcat.plist
3. restart your mac

Note: Replace "/Users/username/tomcat" with your tomcat path in launchd_wrapper.sh and plist file.

try to launch tomcat with the plist
"sudo launchctl load -w /Library/LaunchDaemons/org.apache.tomcat.plist"

If you got error: "launchctl: Dubious ownership on file (skipping)" [5]
Please try change owner and permission of the plist file
a. change the owner to root with "sudo chown root <filename>",
b. change the permissions with "sudo chmod 644 <filename>"
   (4 for read access, 2 for write access, 1 for execute access, added up. The first number is for the owner, the second for the group, the third for everyone.) 


-----  http://situee.blogspot.com ---------



#######################################
##         launchd_wrapper.sh        ##
#######################################
#!/bin/bash

# NOTE: this is an OSX launchd wrapper shell script for Tomcat (to be placed in $CATALINA_HOME/bin)

CATALINA_HOME=/Users/username/tomcat

function shutdown() {
    date
    echo "Shutting down Tomcat"
    $CATALINA_HOME/bin/catalina.sh stop
}

date
echo "Starting Tomcat"
export CATALINA_PID=/tmp/$$

# Uncomment to increase Tomcat's maximum heap allocation
# export JAVA_OPTS=-Xmx512M $JAVA_OPTS

. $CATALINA_HOME/bin/catalina.sh start

# Allow any signal which would kill a process to stop Tomcat
trap shutdown HUP INT QUIT ABRT KILL ALRM TERM TSTP

echo "Waiting for `cat $CATALINA_PID`"
wait `cat $CATALINA_PID`


#######################################
##      org.apache.tomcat.plist      ##
#######################################

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <dict>
        <key>Label</key>
        <string>org.apache.tomcat</string>
        <key>OnDemand</key>
        <false/>
        <key>RunAtLoad</key>
        <true/>
        <key>EnvironmentVariables</key>
        <dict>
            <key>CATALINA_HOME</key>
            <string>/Users/username/tomcat</string>
            <key>JAVA_HOME</key>
            <string>/Library/Java/Home</string>
        </dict>
        <key>ProgramArguments</key>
        <array>
            <string>/Users/username/tomcat/bin/launchd_wrapper.sh</string>
        </array>
        <key>ServiceDescription</key>
        <string>Tomcat</string>
        <key>StandardErrorPath</key>
        <string>/Users/username/tomcat/logs/launchd.stderr</string>
        <key>StandardOutPath</key>
        <string>/Users/username/tomcat/logs/launchd.stdout</string>
        <key>UserName</key>
        <string>root</string>
    </dict>
</plist>


#######################################
##              tomcat.sh            ##
#######################################

#!/bin/bash

# convenience script for easily starting/stopping tomcat on OSX
# (to be placed in /usr/local/bin. remember to run chmod +x on it)
if [ 'start' = "$1" ]; then
    echo Starting tomcat service...
    COMMAND=load
elif [ 'stop' = "$1" ]; then
    echo Stopping tomcat service...
    COMMAND=unload
fi

if [ 'restart' = "$1" ]; then
    echo Restarting tomcat service...
    sudo launchctl unload -w /Library/LaunchDaemons/org.apache.tomcat.plist
    sudo launchctl load -w /Library/LaunchDaemons/org.apache.tomcat.plist
elif [ -n "$COMMAND" ]; then
    sudo launchctl $COMMAND -w /Library/LaunchDaemons/org.apache.tomcat.plist
fi


============================

Reference :
[1] Start Confluence automatically on OS X using launchd
https://confluence.atlassian.com/display/CONF35/Start+Confluence+automatically+on+OS+X+using+launchd

[2] https://code.google.com/p/gbif-providertoolkit/wiki/TomcatInstallationMacOSX#7)_Running_Tomcat_as_a_service_daemon

[3] https://gist.github.com/mystix/661713

[4] A Complete Guide To Tomcat Start-Up
http://www.mulesoft.com/complete-guide-tomcat-start-manual-automatic-and-remote

[5] http://apple.stackexchange.com/questions/3250/why-am-i-getting-a-dubious-ownership-of-file-error-when-launch-agent-runs-my

1 comment:

  1. # Uncomment to increase Tomcat's maximum heap allocation
    # export JAVA_OPTS=-Xmx512M $JAVA_OPTS

    This will NOT increase Tomcat's maximum heap allocation!

    From $CATALINA_HOME/RUNNING.txt:

    Note: Do not use JAVA_OPTS to specify memory limits. You do not need much
    memory for a small process that is used to stop Tomcat. Those settings
    belong to CATALINA_OPTS.

    ReplyDelete