Created
November 16, 2012 17:46
-
-
Save dabrahams/4089352 to your computer and use it in GitHub Desktop.
launchd testbench
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 +"%H:%M:%S ------------------------" | |
for i in {1..12} ; do | |
date +"%H:%M:%S%t<\$i>" | |
sleep 1 | |
done | |
date +"%H:%M:%S ==========================" | |
</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