Skip to content

Instantly share code, notes, and snippets.

@dmdeller
Created April 20, 2010 21:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmdeller/373103 to your computer and use it in GitHub Desktop.
Save dmdeller/373103 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Problem: DirectoryService crashes randomly. When this happens, AFP stops working until AFP is restarted.
#
# Workaround: This script looks for DirectoryService crashes, and upon detecting one, restarts AFP.
#
# Installation: Move this script to /usr/local/bin (or desired location) and chmod +x. Add to system crontab, run as root. I recommend running every minute to minimize AFP downtime, but the syslog command can be very resource-intensive, so it is ultimately at your discretion.
notify_email="me@example.com"
last_restart_file="/var/run/directoryservice_check_crash"
last_crash=`syslog -k Sender com.apple.launchd -k Time ge -4h | grep -E "DirectoryServices.+Job appears to have crashed" | tail -1`
function log {
syslog -s -l $1 -k Sender `basename $0` Message "$2"
}
if [ `id -u` != "0" ]
then
echo "Script needs to be run as root. Aborting."
log error "Script needs to be run as root. Aborting."
exit 1
fi
if [ "$last_crash" = "" ]
then
log notice "No recent DirectoryService crash found. No need to do anything."
exit 0
fi
if [ -f $last_restart_file ]
then
if [ "`cat $last_restart_file`" = "$last_crash" ]
then
log notice "Contents of $last_restart_file indicate that we've already restarted after this crash. There's no need to restart again."
exit 0
fi
fi
# for bonus points, disconnect all users gracefully and send them a nice message explaining what's going on.
# this should be possible with 'sudo serveradmin command' but I couldn't get it to work.
# see File_Server_Admin_v10.6.pdf for more (incomplete and typo-ridden) info
log warning "New DirectoryService crash found. Restarting AFP."
serveradmin stop afp
serveradmin start afp
crash_log_file="/Library/Logs/CrashReporter/"`ls -t /Library/Logs/CrashReporter | grep DirectoryService | head -1`
if [ -f $crash_log_file ]
then
crash_log=`cat $crash_log_file`
else
crash_log="No crash log found."
fi
log notice "Sending crash notice email to $notify_email"
mailtext="Open Directory service on `hostname` has crashed. Restarting File Sharing service.\n\n"
mailtext="A crash log is attached for your bemusement.\n\n\n\n"
mailtext="--\n\n"
mailtext=$mailtext"$crash_log"
echo -e "$mailtext" | mail -s "Directory Service crash" $notify_email
log notice "Saving information about last DirectoryService crash to $last_restart_file for future reference."
echo $last_crash > $last_restart_file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment