1)Label【required】
该项服务的名称。
2)OnDemand【optional】
10.4提供的一个key值,功能与KeepAlive的基本功能相似,在10.5及之后的系统中被KeepAlive替代。KeepAlive扩展了更多的功能,除了使用单一的boolean作为key值之外,还能使用字典结合多个key值。
3)Program【ProgramArgument是required的,在没有ProgramArgument的情况下,必须要包含Program这个key】
指定可执行文件的路径。
4)RunAtLoad【optional】
标识launchd在加载完该项服务之后立即启动路径指定的可执行文件。默认值为false。
5)WorkingDirectory【optional】
该key在开启可执行文件之前,指定当前工作目录的路径。
6)KeepAlive【optional】
这个key值是用来控制可执行文件是持续运行呢,还是满足具体条件之后再启动。默认值为false,也就是说满足具体条件之后才启动。当设置值为ture时,表明无条件的开启可执行文件,并使之保持在整个系统运行周期内。
3.2 创建一个plist文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
"http://www.easck.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.yourcompany.HSPA_USB_MODEM</string>
<key>OnDemand</key>
<false/>
<key>Program</key>
<string>/Application/HSPA USB MODEM.app/Running</string>
<key>RunAtLoad</key>
<true/>
<key>WorkingDirectory</key>
<string>/Application/HSPA USB MODEM.app</string>
</dict>
</plist>
4. 三种方式的区别
初步了解了系统的启动过程之后,再来看这三种配置启动项的方式,就很容易理解这三种方式之间的差异了。
总的来说,LoginItems 和StartupItems的区别较明显。
| StartupItems | LoginItems | |
|---|---|---|
| Depedency Ordering | Need | Do not Need |
| Load Timing | during startup | after a user logs in |
| Launched by WHO | by root, but not necessarily | the user |
| Processes types | background processes and processes that terminate after running | any Mac OS X executable |
对于我们自定义的Launchd daemon,通常(同时也是系统推荐)是放在~/Library/LaunchAgents路径下。launchd进程需要在用户login之后才能加载。这种方式与LoginItems最大的区别在于,启动的进程不同。LoginItems是通过loginwindow去启动的,而Launchd daemon是通过com.apple.launchd.peruser启动。










