Here is the script I use for shutdown the Indigo Server during debugging sometimes::
- Code: Select all
#!/bin/sh
####################
# Copyright (c) 2008, Perceptive Automation, LLC. All rights reserved.
# http://www.perceptiveautomation.com
####################
# --------------------------------------------------------------
#
SERVER_NAME="IndigoServer.app"
LAUNCH_AGENTS_PATH="Library/LaunchAgents"
LAUNCHD_PLIST="com.perceptiveautomation.IndigoServer2.plist"
SERVER_KILLED_VIA_UNLOAD=0
SERVER_KILLED_VIA_SIGNAL=0
WEBSERVER_NAME="IndigoWebServer"
# Yuck. The installer calls us as root user, but if we make launchctl calls
# under root it won't find the user-specific plists (I think it should since
# we provide the full user path but it does everything under the root domain
# if the current user is root). We must extract out the real non-root user
# by finding the owner of the $HOME directory and use su on launchctl.
LAUNCHCTL_USER=`ls -ld "$HOME" | awk '{print $3}'`
RUNNING_USER=`id -un`
# --------------------------------------------------------------
#
appRunningCount()
{
APP_RUNNING_COUNT=`ps -axww | grep "$1" | grep -v "grep" | grep -v "launchctl" | wc -l`
return $APP_RUNNING_COUNT
}
getAppPID()
{
APP_PID=`ps -axww | grep "$1" | grep -v "grep" | grep -v "launchctl" | awk '{print $1}'`
echo $APP_PID # echo, not return because PID will be greater than 255
}
waitForAppDeath()
{
for (( index = 0 ; index < 10 ; index++ ))
do
appRunningCount "$1"
if [ $? -eq 0 ]
then
return 0
fi
sleep 1
done
return 1
}
# --------------------------------------------------------------
#
killMainServer()
{
appRunningCount "$1"
if [ $? -eq 0 ]
then
echo "$1 was not running"
return 0
fi
if [ -z "$LAUNCHCTL_USER" ]
then
echo "Failed to get owner of $HOME"
return 0
fi
# First, try using launchctl to unload the plist. This only
# works if launchctl was used to start the process.
plistPath="$HOME/$LAUNCH_AGENTS_PATH/$LAUNCHD_PLIST"
if [ -a "$plistPath" ]
then
echo "Unloading and disabling $plistPath"
if [ "$LAUNCHCTL_USER" = "$RUNNING_USER" ]
then
launchctl unload -w "$plistPath"
else
echo "(forcing launchctl to run under user $LAUNCHCTL_USER)";
su -m "$LAUNCHCTL_USER" -c "launchctl unload -w \"$plistPath\""
fi
if [ $? -eq 0 ]
then
echo "Unloaded $LAUNCHD_PLIST"
waitForAppDeath "$1"
if [ $? -eq 0 ]
then
echo "$1 was stopped via launchctl unload"
SERVER_KILLED_VIA_UNLOAD=1
return 0
fi
# else process is still running for some reason, fall through
# to signal technique below
fi
fi
# Next, use signals (start nice, then send SIGKILL) to try
# shutting it down.
APP_PID=`getAppPID "$1"`
if [ -n "$APP_PID" ]
then
echo "Quitting $1 (SIGINT to $APP_PID)"
kill -s TERM "$APP_PID"
waitForAppDeath "$1"
if [ $? -eq 0 ]
then
echo "$1 was stopped via signal"
SERVER_KILLED_VIA_SIGNAL=1
return 0
fi
echo "Quitting $1 (SIGKILL to $APP_PID)"
kill -s KILL "$APP_PID"
waitForAppDeath "$1"
if [ $? -eq 0 ]
then
echo "$1 was killed via signal"
SERVER_KILLED_VIA_SIGNAL=1
return 0
fi
echo "Unable to kill $1"
return 1
fi
echo "Bad PID returned ($APP_PID)"
return 1
}
# --------------------------------------------------------------
#
killSecondaryProcess()
{
appRunningCount "$1"
if [ $? -eq 0 ]
then
echo "$1 was not running"
return 0
fi
# Next, use signals (start nice, then send SIGKILL) to try
# shutting it down.
APP_PID=`getAppPID "$1"`
if [ -n "$APP_PID" ]
then
echo "Quitting $1 (SIGTERM to $APP_PID)"
kill -s TERM "$APP_PID"
waitForAppDeath "$1"
if [ $? -eq 0 ]
then
echo "$1 was stopped via signal"
return 0
fi
echo "Quitting $1 (SIGKILL to $APP_PID)"
kill -s KILL "$APP_PID"
waitForAppDeath "$1"
if [ $? -eq 0 ]
then
echo "$1 was killed via signal"
return 0
fi
echo "Unable to kill $1"
return 1
fi
echo "Bad PID returned ($APP_PID)"
return 1
}
# --------------------------------------------------------------
#
killMainServer "$SERVER_NAME"
killSecondaryProcess "$WEBSERVER_NAME"
exit 0
And here is a script I use to start it:
- Code: Select all
#!/bin/sh
####################
# Copyright (c) 2006, Perceptive Automation, LLC. All rights reserved.
# http://www.perceptiveautomation.com
####################
# --------------------------------------------------------------
#
SERVER_NAME="IndigoServer.app"
LAUNCH_AGENTS_PATH="Library/LaunchAgents"
LAUNCHD_PLIST="com.perceptiveautomation.IndigoServer2.plist"
# Yuck. The installer calls us as root user, but if we make launchctl calls
# under root it won't find the user-specific plists (I think it should since
# we provide the full user path but it does everything under the root domain
# if the current user is root). We must extract out the real non-root user
# by finding the owner of the $HOME directory and use su on launchctl.
LAUNCHCTL_USER=`ls -ld "$HOME" | awk '{print $3}'`
RUNNING_USER=`id -un`
# --------------------------------------------------------------
#
startMainServer()
{
if [ -z "$LAUNCHCTL_USER" ]
then
echo "Failed to get owner of $HOME"
return 0
fi
# First, try using launchctl to unload the plist. This only
# works if launchctl was used to start the process.
plistPath="$HOME/$LAUNCH_AGENTS_PATH/$LAUNCHD_PLIST"
echo "Loading and enabling $plistPath"
if [ "$LAUNCHCTL_USER" = "$RUNNING_USER" ]
then
launchctl load -w "$plistPath"
else
echo "(forcing launchctl to run under user $LAUNCHCTL_USER)";
su -m "$LAUNCHCTL_USER" -c "launchctl load -w \"$plistPath\""
fi
if [ $? -eq 0 ]
then
echo "Started $SERVER_NAME"
else
echo "Failed to start via launchctl"
fi
return 0
}
# --------------------------------------------------------------
#
startMainServer
exit 0
Note the startup script uses launchd to start it up, which might present problems with Mavericks and AppleScript (despite the fact that launchd is the Apple approved way to launch services on boot/login). Your mileage may very.