Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'RE: launchd' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
RE: launchd
Authored by: nicksay on May 31, '05 08:13:59PM

I don't think launchd is actually the universal "better" way. It has some limitations (more on those at the end). As a side note, see Oneota's comment below — to get the "UNIX" version of a file path in AppleScript, you can use the builtin POSIX path function that's part of Standard Additions. Back to the subject, here's a little more info THAT I've found about launchd:

Links Example

You can follow the instructions over at Mac Geekery for another use of launchd items, but relevant to this disscussion are LaunchAgents. launchd controls two types of processes: LaunchDaemons, which are system-wide and are loaded at startup, and LaunchAgents, which are per-user and loaded at login. From the man page for launchd:

FILES
      ~/Library/LaunchAgents         Per-user agents provided by the user.
      /Library/LaunchAgents          Per-user agents provided by the administrator.
      /Library/LaunchDaemons         System wide daemons provided by the administrator.
      /System/Library/LaunchAgents   Mac OS X Per-user agents.
      /System/Library/LaunchDaemons  Mac OS X System wide daemons.
So the general case will be to place a launchd item in the first directory, the user's LaunchAgents folder.

Creating the Script

If we want to run a script at login, one first has to save the script somewhere. I chose ~/Library/Scripts/Applictions/launchd/ because I thought it made logical sense and it won't show up in the script menu because it's in an Applications subdirectory. I created the following script:

#!/bin/bash
echo Hello World > /Users/YOURUSERHERE/Desktop/HelloWorld.txt
and saved it as an executable file named HelloWorld in the above folder. Replace "YOURUSERHERE" with your short username (i.e. your home directory).

Creating the launchd Item

You can either use PropertyListEditor or any text editor to create the launchd item. The file should be saved at ~/Library/LaunchAgents/. I used PropertyListEditor:

  1. Create a new document, create a new root, and expand it.
  2. Create 3 children and name them Label, ProgramArguments, and RunAtLoad.
  3. Set Label to a string with a reverse-domain-style identifier (e.g. com.example.userlogin).
  4. Set ProgramArguments to an array. Add 1 child and set it to a string with the path to your script (e.g. /Users/YOURUSERHERE/Library/Scripts/Applications/launchd/HelloWorld).
  5. Set RunAtLoad to a boolean that's false.
  6. Save the file as the value of Label and a plist extension (e.g. com.example.userlogin.plist)

After you've saved it, or if you're using a text editor, the file should look like:

<?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>com.example.userlogin</string>
	<key>ProgramArguments</key>
	<array>
		<string>/Users/YOURUSERHERE/Library/Scripts/Applications/launchd/HelloWorld</string>
	</array>
	<key>RunAtLoad</key>
	<true/>
</dict>
</plist>
Again, replace "YOURUSERHERE" with your short username (i.e. your home directory). Now you can test your LaunchAgent by opening Terminal and running launchctl load Library/LaunchAgents/com.example.userlogin.plist. You should see a HelloWorld.txt file on your Desktop. Delete it and as a final test log out and log in. You should see the file on your Desktop as you log in.

Problems

If your LaunchAgent isn't working, check Console and see if launchd has thrown any errors. One example:

launchd[6156]: com.example.userlogin: exited with exit code: 1
You can add a key titled Debug with a boolean value that's false to your launchd item and run the test on the command-line. (You might need to unload the item first (e.g. launchctl unload ... before loading it again.) When you run it with the Debug value set to true, errors and output go to the command-line instead of to Console.

launchd Limitations

Now, as I said, I don't think that launchd is the be-all-and-end-all of login-item-replacement. Not yet, anyway. First, it's designed only for command-line scripts or programs. It's also designed for programs that run in the background (i.e. daemons). So, there is no user interaction. I couldn't even get it to run an AppleScript. (I put /usr/bin/osascript as my first ProgramArguments item and the path to my script as the second.) I think the problem is that LaunchAgents are run so early in the login process that AppleScript doesn't function. (I tried both a display dialog and a do shell script. Incidentally, even when testing from the command-line, you need to tell System Events to display dialog because user interaction is prevented from osascript directly.) I don't think interaction with other applications will work because your script will be called before they are even launched or made available to AppleScript. However, for things like executing configuration shell scripts or running up a user-level copy of MySQL or starting a ssh-agent session, LaunchAgents do work very well in replacing login-items.

So that's what I've found that is relevant. Thoughts?

[ Reply to This | # ]
RE: launchd
Authored by: jaydisc on Jun 01, '05 07:04:35PM
I have a LaunchDaemon that uses osascript with an applescript as its argument. This is to run an AppleScript to update a DNS server of my newly changed dynamic IP. This is stored in /Library/LaunchDaemons and runs regardless of a user being logged in. It is set to rerun every 15 minutes:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/Proper$
<plist version="1.0">
<dict>
        <key>Label</key>
        <string>com.xxxxxx.nsupdate</string>
        <key>LowPriorityIO</key>
        <true/>
        <key>Nice</key>
        <integer>1</integer>
        <key>ProgramArguments</key>
        <array>
                <string>osascript</string>
                <string>/usr/local/bin/nsupdate.scpt</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
        <key>ServiceDescription</key>
        <string>nsupdate service</string>
        <key>StartInterval</key>
        <integer>900</integer>
</dict>
</plist>


[ Reply to This | # ]