Skip to content

Instantly share code, notes, and snippets.

@dabrahams
Created November 16, 2012 17:46
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 dabrahams/4089352 to your computer and use it in GitHub Desktop.
Save dabrahams/4089352 to your computer and use it in GitHub Desktop.
launchd testbench
#!/bin/bash
# Findings:
#
# StartOnMount=true will cause the job to start when anything is
# mounted even if other conditions, e.g. QueueDirectories, say the
# job should not run
#
# The StartInterval timer begins ticking at the moment the plist is
# loaded. If QueueDirectories is preventing the job from running,
# but the queue becomes available, the job will only be started
# at the next StartInterval boundary.
#
# If something else causes the job to run (e.g. StartOnMount) and
# the StartInterval timer expires while the job is running, it
# will start again immediately.
set -e
TOP=/tmp/scratch
JOB=mac.test
# TEMP_PLIST=$TOP/$JOB.plist
PLIST=$TOP/$JOB.plist #/Library/LaunchDaemons/$JOB.plist
LOG=$TOP/test.log
IMG=$TOP/news.sparseimage
MNT=$TOP/news
QUEUE=$TOP/news/leaf.node
function msg() {
date "+%H:%M:%S \"$@\""
}
function msgn() {
msg "$@" | tr '\n' ' '
}
function load() {
msgn 'loading plist...'
sudo launchctl load $PLIST
echo 'Done.'
}
function unload() {
msgn 'unloading plist...'
sudo launchctl unload $PLIST
echo 'Done.'
}
function mount() {
msgn 'mounting image...'
mkdir -p $MNT
sudo chown -R _news $MNT
sudo hdiutil attach -quiet -nobrowse -notremovable -noautofsck -owners on -mountpoint $MNT $IMG
echo 'Done.'
}
function unmount() {
msgn 'unmounting image...'
sudo umount $MNT > /dev/null 2>&1
echo 'Done.'
}
#
# Clean up anything left over from a previous run
#
( ( unload ; unmount ; sudo rm -rf $TOP ) || True ) > /dev/null 2>&1
# create the log file
mkdir -p $TOP
touch $LOG
sudo chown _news $LOG
# prepare a sparse image
msgn 'preparing images...'
hdiutil create -quiet -fs HFS+ -size 1M -type SPARSE -volname news $IMG
mount > /dev/null
sudo chown -R _news $MNT/
sudo -u _news mkdir $QUEUE
sudo -u _news touch $QUEUE/foo
unmount > /dev/null
echo 'Done.'
cat > $PLIST<<EOF
<?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>UserName</key> <string>_news</string>
<key>Label</key> <string>$JOB</string>
<key>StartInterval</key> <integer>18</integer>
<key>ProgramArguments</key>
<array>
<string>/bin/bash</string>
<string>-c</string>
<string>
date +&quot;%H:%M:%S ------------------------&quot;
for i in &#123;1..12&#125; ; do
date +&quot;%H:%M:%S%t&lt;\$i&gt;&quot;
sleep 1
done
date +&quot;%H:%M:%S ==========================&quot;
</string>
</array>
<key>QueueDirectories</key>
<array>
<string>$QUEUE</string>
</array>
<key>StandardOutPath</key> <string>$LOG</string>
<key>StandardErrorPath</key> <string>$LOG</string>
</dict>
</plist>
EOF
sudo chown root $PLIST
msg "Starting log monitor"
tail -f "$LOG"&
MONITOR=$!
load
msg "...pause to prove launchd isn't starting us up without queue mounted..."
sleep 5
msg '** mounting with plist loaded'
mount
sleep 50
unload
unmount
msg "...pause..."
sleep 5
msg '** loading with queue mounted'
mount
load
sleep 5
unload
unmount
echo
msg "Stopping log monitor"
kill $MONITOR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment