Using launchd to work with services

Is there a way to have launchd respond immediately when a service (such as AppleSpell) exceeds a certain threshold of cpu cycles? I am currently trying to monitor when AppleSpell comes into play while the user types text in any Apple text processor application. The unix ps command shows the active services and their cpu rates.

In case there is no way to work the task above, I wish to know how to trigger a shell script at startup using launchd. I assumed it would simply activate all loaded plists within ~/Library/LaunchAgents/ however this does not appear to be the case.

You probably want to use the RunAtLoad key. From the launchd.plist man page:

Bruce:

Thanks for the tip about including the ‘RunAtLoad’ key. I added it to the launchd plist so my code looks like this:

#########################

<?xml version="1.0" encoding="UTF-8"?> Label SpellAwareStart ProgramArguments /Users/awnispel/bin/SpellAwareStartup.sh RunAtLoad #########################

Unfortunately, a new system startup still does not run my shell script (I reboot my computer). It seems that loading the plist with launchctl will not make the plist survive a system reboot. The lauchd plist does work, however, when I test it using ‘launchtcl run SpellAwareStart’. Do you have anymore advice?

Model: MacBook
Browser: Safari 523.12.2
Operating System: Mac OS X (10.5)

Never mind the post above: I found that I had not moved the launchd plist to the necessary folder ~/Library/LaunchAgents/ and loaded it from there.

I have to say that finding out how to launch a shellscript (without using the Login Items method that has the undesirable side affect of opening up the Terminal window) has been a frustrating experience. Researching the information on the Web produced many tutorials that were either obsolete or did not address how launchd works to run a shell script when the computer starts up. I really appreciate Bruces tip that RunAtLoad is required. When looking for this information within Apple’s documentation I did find it, however, it appears buried within a section entitled “Migration”–certainly not addressed to the non-professional scripter such as myself. In short, I’m surprised it took me about a week to figure this common issue out.

For the record, the directions for creating an automatic startup for your shell script (or AppleScript) that doesn’t use the awkward Login Items method will be found it you read one of the following online articles:

launchd in Depth by Josh Wisenbaker
http://www.afp548.com/article.php?story=20050620071558293

or MacTech’s article: LAUNCHD: Judge, Jury, Executioner by Edward Marczak at:
http://www.mactech.com/articles/mactech/Vol.21/21.06/launchd/index.html

Unfortunately, neither of these articles mentions anything about how to make the lauchd plist work so your script will start when the computer is booted up–the obvious task. That’s where Bruces tip for the need to include a RunAtLoad key is most helpful.