launchd daemon not starting correctly on launch

Hi All,


I have a script that I like to have run anytime my wireless network changes. The script needs to be run under sudo, so I created a daemon inside of /Library/LaunchDaemons .


My plist file looks like this:

<?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>Disabled</key>

<false/>

<key>Label</key>

<string>my.plist.name</string>

<key>ProgramArguments</key>

<array>

<string>/path/to/script/to/run.sh</string>

</array>

<key>WatchPaths</key>

<array>

<string>/Library/Preferences/SystemConfiguration/com.apple.airport.pr eferences.plist</string>

</array>

</dict>

</plist>


So here's where the oddness starts happening. When I start up my computer, launchctl tells me that this daemon is loaded. (I run sudo launchctl list and it's there). However, the script does not run when it should.


However, if I manually unload and then load the plist using the launchctl command then everything works completely fine.


So the question is, why does it work when I load it manually, but not work when the system loads it on startup?


Thanks,

Q

MacBook Pro, Mac OS X (10.7.2)

Posted on Jan 7, 2012 11:40 AM

Reply
4 replies

Jan 7, 2012 1:34 PM in response to Quadari

well, first, I'd remove the disabled key entirely and use the Program key rather than the ProgramArguments key:


<?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>my.plist.name</string>

<key>Program</key>

<string>/path/to/script/to/run.sh</string>

<key>WatchPaths</key>

<array>

<string>/Library/Preferences/SystemConfiguration/com.apple.airport.preferences. plist</string>

</array>

</dict>

</plist>


But the plist looks well-formed, so most likely scenario is that the script itself is failing. open terminal and run launchctl list | grep my.plist.name: field 2 is the return value of the last time the script was run; if it's anything other than 0 the script threw an error and it has nothing to do with launchd.


are there any suspicious messages from launchd in console?

Jan 8, 2012 10:47 AM in response to twtwtw

No suspicious message in the console. When I sudo launchctl list it shows up with a 0 in the second field.


The only thing I can find in doing some google-ing is this:

https://managingosx.wordpress.com/2006/05/10/launchd-gotcha/


Which seems to imply that a launch daemon that is watching a file will fail to work if that file doesn't exist at all times. However, I would think that /Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist should always exist. So I'm still perplexed.


Right now I have a seomwhat convoluted system set up as the following:


My launch daemon (that needs to run as root) is set to watch a folder ~/random/

Then I have a launch agent which successfully watches for changes in /Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist and when it sees them touches a file in ~/random/.

The launch daemon then deletes that file and successfully runs the script I want it to run.


It seems weird/odd/annoying to have to set up a this double-headed process just to do something simple like this, so I'm open to suggestions as to what is going on.

Jan 8, 2012 11:04 AM in response to Quadari

Well, that actally makes sense. If you examine the com.apple.airport.preferences.plist file you'll see that the creation date is always the same as the modification date, implying that airport isn't just updating the file but actually overwriting it every time. That may be disabling the job (since it effectively deletes the file). You could watch the surrounding System Configuration folder instead - every time a file is deleted from or added to the folder the system will mark the folder as changed - but then you'd need to add in some logic testing whether it was airport preferences that changed or one of the other files.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

launchd daemon not starting correctly on launch

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