Apple’s Worldwide Developers Conference to kick off June 10 at 10 a.m. PDT with Keynote address

The Keynote will be available to stream on apple.com, the Apple Developer app, the Apple TV app, and the Apple YouTube channel. On-demand playback will be available after the conclusion of the stream.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

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)

Posted on Jan 7, 2012 5:25 PM

Reply
1 reply

Tell launchd to wait for a process to stop before shutdown

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.