[launchd-dev] fork & exit in Shell Script (Revisited)

Charles Darwin DarwinsKernel at gmail.com
Wed Nov 5 10:26:41 PST 2008


On 3-Nov-08, at 12:15 PM, Charles Darwin wrote:

>
> On 29-Oct-08, at 2:59 PM, Charles Darwin wrote:
>
>>
>> On 29-Oct-08, at 2:22 PM, Damien Sorresso wrote:
>>
>>> On Oct 29, 2008, at 10:36 AM, Charles Darwin wrote:
>>>> Hi all,
>>>>
>>>> I have a shell script that runs RunAtLoad. Here is the script:
>>>>
>>>> ########
>>>> /bin/test -f /Volumes/tutorials || /bin/mkdir -p /Volumes/ 
>>>> tutorials && /sbin/mount_afp afp://username:password@url/ 
>>>> tutorials/ /Volumes/tutorials/
>>>>
>>>>
>>>> /bin/test -f /Volumes/homes || /bin/mkdir -p /Volumes/homes && / 
>>>> sbin/mount_afp afp://username:password@url/homes/ /Volumes/homes/
>>>> ########
>>>> and here is the plist:
>>>>
>>>> ------------------
>>>> <?xml version="1.0" encoding="UTF-8"?>
>>>> <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"  
>>>> "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
>>>> <plist version="1.0">
>>>> <dict>
>>>>         <key>Label</key>
>>>>         <string>label</string>
>>>>         <key>ProgramArguments</key>
>>>>         <array>
>>>>                 <string>path/to/script</string>
>>>>         </array>
>>>>         <key>StartCalendarInterval</key>
>>>>         <dict>
>>>>                 <key>RunAtLoad</key>
>>>>                 <true/>
>>>>         </dict>
>>>> </dict>
>>>> </plist>
>>>
>>> Firstly, it is convention to use the reverse-DNS scheme for your  
>>> job label (i.e. com.domain.something). Secondly, RunAtLoad  
>>> belongs under the root node of the plist, not under  
>>> StartCalendarInterval. Try restructuring your plist like this...
>>>
>>>> <?xml version="1.0" encoding="UTF-8"?>
>>>> <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"  
>>>> "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
>>>> <plist version="1.0">
>>>> <dict>
>>>>         <key>Label</key>
>>>>         <string>label</string>
>>>>         <key>ProgramArguments</key>
>>>>         <array>
>>>>                 <string>path/to/script</string>
>>>>         </array>
>>>>         <key>RunAtLoad</key>
>>>>         <true/>
>>>> </dict>
>>>
>>> -- 
>>> Damien Sorresso
>>> BSD Engineering
>>> Apple Inc.
>>>
>>
>> Solved. Thanks Damien.
>>
>> Charles
>>
>>
>
> Here we go again; same script, same daemon: what happens is that  
> when user turn on the machine and then logs in our daemon runs the  
> script with no problem. Now if user logs out and then logs back in  
> daemon sits there and does nothing.
>
> $ launchctl list
> <label>
> $ launchctl load /path/to/<label>.plist #and we are in business  
> again (except that `Workaround Bonjour: Unknown error: 0` error  
> message)
>
> What's missing?
>
> Thanks,
> Charles

I think it happens because launchd does not unload the job when user  
logs out so the next time user logs in (without shoutting down the  
machine) it doesn't load the job again (because it's set to  
RunAtLoad). So my question is how am I supposed to shut the daemon  
down. I have been experiencing with adding the following line at the  
end of the script with no luck so far.

/bin/launchctl stop <label> && /bin/launchctl unload /Users/user/ 
Library/LaunchDaemons/<label>.plist

What is the proper way of shutting down a daemon? Another daemon that  
runs a bit later?

Charles


More information about the launchd-dev mailing list