launchctl Tutorial   Leave a comment

launchctl allows users to start / stop applications that is typically processed via launchd. (MacOS’s equivalent of cron).

launchctl command configurations are stored as XML formatted .plist files located in directories/System/Library/LaunchAgents or /System/Library/LaunchDaemons for system wide start items, for user specific items the plist files are stored in ~/Library/LaunchAgents or ~/Library/LaunchDaemonsdirectories.

Agents or Daemons?
An agent is a program that requires access to specific users’ information. A daemon is a program that runs in the background and requires generally no input from any user.

For more a comparison between Agents and Daemons refer to this Apple technote.

What is in a .plist?
.plist file must contain at the very least the keys LabelProgramArguments array and a key to tell launchd how the application is run eg KeepAliveRunAtLoad for one time operations. orStartonMountStartIntervalStartCalendarInterval for repeating occurrences.

A complete dictionary of all the property keys can be found here. A few things it can do is to monitor modified paths (via the key WatchPath), HardResourceLimits etc.

Here is an example for com.companyname.agentorapplicationname.plist, which runs the applicationfull/path/to/binary every 60 seconds:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
< ?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”&gt;

<plist version=”1.0″>
<dict>
<key>Label</key>
<string>com.companyname.agentorapplicationname</string>
<key>ProgramArguments</key>
<array>
<string>/full/path/to/binary</string>
<string>-firstargument</string>
<string>valueoffirstargument</string>
<string>-secondargument</string>
<string>valueofsecondargument</string>
</array>
<key>StartInterval 60</key>
<true />
</dict>
</plist>


launctl operations

Command Description
launchctl list Lists the PID, status and name of loaded processes
launchctl list com.com.exampleco.eg Output the runtime information (eg. PATH) of com.exampleco.eg
launchctl start com.exampleco.eg Starts com.exampleco.eg
launchctl stop com.exampleco.eg Stop com.exampleco.eg
launchctl loads -w /path/example.plist Loads a process by its plist filename
launchctl unloads /path/example.plist Stop and unload a process by its plist filename
submit -l labelname -p /path/eg/binary -o /path2/stdout -e /path2/sterr Manually run binary under the label labelname with to specified stdout and sterr devices/files

Posted 2011年11月20日 by gw8310 in mac os

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: