Tell launchd to wait for a process to stop before shutdown
Hi,
I have a script that I run on boot using launchd (http://developer.apple.com/library/mac/#documentation/MacOSX/Conceptual/BPSystem Startup/Chapters/CreatingLaunchdJobs.html#//apple_ref/doc/uid/10000172i-SW7-1053 27).
During shutdown, my script catches the TERM signal and starts cleaning up in order to terminate. Unfortunately, it takes ~60seconds to do this which results in OSX Lion "killing" my script before it has a chance to clean up.
How can I tell launchd to wait for 1 minute between sending the TERM and KILL signals, so that my script has enough time to shut down?
My launch daemon is defined as:
<?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>com.my.vm.gw</string>
<key>UserName</key>
<string>vmserver</string>
<key>ProgramArguments</key>
<array>
<string>/Users/vmserver/bin/runVM.sh</string>
<string>box1</string>
</array>
<key>KeepAlive</key>
<true/>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
The runVM script launches a VirtualBox instance, then waits for the TERM signal by launchd which it uses to shut down the machine:
#!/bin/sh
WAIT_SHUTDOWN_SECS=90
VBH=/usr/bin/VBoxHeadless
VBM=/usr/bin/VBoxManage
# send shutdown signal on term and interrupt
trap "$VBM controlvm $1 acpipowerbutton ; echo Shutdown request sent ; sleep $WAIT_SHUTDOWN_SECS ; echo Control script exiting... ; exit" TERM INT
# launch VM
$VBH -s $1 &
vboxpid=$!
echo Press Ctrl-C or send TERM signal to $$ for clean shutdown
wait $vboxpid
echo Goodbye...
Thanks!
Mac mini, Mac OS X (10.7.2)