This page describes how to configure auto-start for case browser on Mac OS X via launchd.

Important Note

We need to make sure launchd uses UTF-8 locale, otherwise there will be encoding issues in case browser.

Configure UTF-8 locale


Main file with environment variables definition:

sudo vi /etc/environment
#!/bin/sh
set -e
syslog -s -l warn "Set environment variables with /etc/environment $(whoami) - start"
launchctl setenv LC_ALL     en_US.UTF-8
if [ -x /usr/libexec/path_helper ]; then
    export PATH=""
    eval `/usr/libexec/path_helper -s`
    launchctl setenv PATH $PATH
fi
osascript -e 'tell app "Dock" to quit'
syslog -s -l warn "Set environment variables with /etc/environment $(whoami) - complete"



Service definition to load environment variables for user applications (terminal, IDE, ...):

sudo vi /Library/LaunchAgents/environment.user.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>environment.user</string>
    <key>ProgramArguments</key>
    <array>
            <string>/etc/environment</string>
    </array>
    <key>KeepAlive</key>
    <false/>
    <key>RunAtLoad</key>
    <true/>
    <key>WatchPaths</key>
    <array>
        <string>/etc/environment</string>
    </array>
</dict>
</plist>


The same service definition for root user applications:

sudo vi /Library/LaunchDaemons/environment.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>environment</string>
    <key>ProgramArguments</key>
    <array>
            <string>/etc/environment</string>
    </array>
    <key>KeepAlive</key>
    <false/>
    <key>RunAtLoad</key>
    <true/>
    <key>WatchPaths</key>
    <array>
        <string>/etc/environment</string>
    </array>
</dict>
</plist>



And finally we should register these services:

 

$ launchctl load -w /Library/LaunchAgents/environment.user.plist
$ sudo launchctl load -w /Library/LaunchDaemons/environment.plist


What we get:

  • The only place to declare system environment variables: /etc/environment
  • Instant auto-update of environment variables after modification of /etc/environment file - just relaunch your application

Issues / problems:


In order your env variables were correctly taken by applications after system reboot you will need:

  • either login twice: login => logout => login
  • or close & re-open applications manually, where env variables should be taken
  • or do NOT use feature "Reopen windows when logging back".

This happens due to Apple denies explicit ordering of loaded services, so env variables are registered in parallel with processing of the "reopen queue".


Configure Launchd script


You can download the scripts mentioned in the attachments section and modify paths etc accordingly.
Create a wrapper script named "casebrowser-launchd.sh" and put it in home bin folder (~/bin).


casebrowser-launchd.sh
#! /bin/sh
# casebrowser-launchd.sh
#
# Wrapper script that starts Tomcat and waits for the Tomcat process
# to exit. This is needed for proper interaction with launchd.
#---------------------------------------------------------
# Helper functions
#---------------------------------------------------------
# NOTE: We are inheriting CATALINA_HOME from launchd, because its value
#       was defined in the launchd plist configuration file.
function shutdown() {

    # Bye Tomcat!
    echo "Shutting down Tomcat... "
    $CATALINA_HOME/bin/catalina.sh stop
    echo "done."

    # Cleaning up the temporary file
    rm -f $CATALINA_PID

}

function startup() {

    # Define the file where we want the Tomcat process ID to be stored.
    export CATALINA_PID=$(mktemp /tmp/`basename -s .sh $0`.XXXXXX)
    if [ $? -ne 0 ]
    then
        echo "$0: Failed to create temporary file. Aborting."
        exit 1
    fi
    rm -f $CATALINA_PID

    # Let's go!
    echo "Starting up Tomcat... "
    . $CATALINA_HOME/bin/catalina.sh start

    # Register the shutdown function as callback to execute when a signal
    # is sent to this process.
    trap shutdown HUP INT QUIT ABRT KILL ALRM TERM TSTP
    echo "done."

}

function wait_for_tomcat_to_exit() {
    echo "Waiting for Tomcat to exit (PID: `cat $CATALINA_PID`)... "
    wait `cat $CATALINA_PID`
    echo "done waiting for Tomcat to exit."
}

#---------------------------------------------------------
# Let's go
#---------------------------------------------------------
startup
wait_for_tomcat_to_exit



Set permission:

chmod +x ~/bin/casebrowser-launchd.sh



Create launchd configuration file:

sudo vim /Library/LaunchDaemons/com.pi.casebrowser.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>com.pi.casebrowser</string>
    <key>ServiceDescription</key>
    <string>Case Browser</string>
    <key>UserName</key>
    <string>pterobyte</string>
    <key>GroupName</key>
    <string>staff</string>
    <key>EnvironmentVariables</key>
    <dict>
        <key>CATALINA_HOME</key>
        <string>/Users/pterobyte/casebrowser</string>
    </dict>
    <key>ProgramArguments</key>
    <array>
        <string>/Users/pterobyte/bin/casebrowser-launchd.sh</string>
    </array>
    <key>StandardOutPath</key>
    <string>/Users/pterobyte/logs/casebrowser-launchd-stdout.log</string>
    <key>StandardErrorPath</key>
    <string>/Users/pterobyte/logs/casebrowser-launchd-stderr.log</string>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <true/>
</dict>
</plist>


Now test the launchd scripts: (please stop Nuxeo before the test)

sudo launchctl load /Library/LaunchDaemons/com.pi.casebrowser.plist



Attachements:


References:

  • No labels