上一章说了人员和添加和删除守护进程,这章简单说说如何创建一个守护进程文件
守护进程文件是一个字典,苹果提供了key值(相关的key值,可以参考文档https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man5/launchd.plist.5.html#//apple_ref/doc/man/5/launchd.plist,有详细说明)我们只需要设置value就可以了,
常用的属性值
Label:
This required key uniquely identifies the job to launchd. 例如你的应用程序bundleID com.yourname.appname
KeepAlive:
This optional key is used to control whether your job is to be kept continuously running or to let demand and conditions control the invocation. The default is false and therefore only demand will start the job. The value may be set to true to unconditionally keep the job alive. Alternatively, a dictio-nary dictionary nary of conditions may be specified to selectively control whether launchd keeps a job alive or not. If multiple keys are provided, launchd ORs them, thus providing maximum flexibility to the job to refine the logic and stall if necessary. If launchd finds no reason to restart the job, it falls back on demand based invocation. Jobs that exit quickly and frequently when configured to be kept alive will be throttled to converve system resources.
这个属性指明启动程序后是否一直保持在后台,属性为BOOL值,设为NO,就可以在开机启动一次,然后就会自己退出
Program:
This key maps to the first argument of execvp(3). If this key is missing, then the first element of the array of strings provided to the ProgramArguments will be used instead. This key is required in the absence of the ProgramArguments key. 这个就是你应用程序的路径,指向包中的可执行文件 例如:~/Desktop/appName/Contents/MacOS/appName
LaunchOnlyOnce:
This optional key specifies whether the job can only be run once and only once. In other words, if the job cannot be safely respawned without a full machine reboot, then set this key to be true.设置了该值为YES,则只执行一次,只有在完全重新启动一次才会再执行。苹果给的一个简单例子:<dict> <key>Label</key> <string>com.example.exampled</string> <key>ProgramArguments</key> <array> <string>exampled</string> </array> <key>KeepAlive</key> <true/> </dict> </plist>
相关的属性值介绍:https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man5/launchd.plist.5.html#//apple_ref/doc/man/5/launchd.plist
这样我们就可以自己创建一个简单的守护进程了。
更多详细信息,可以阅读苹果的官方文档:https://developer.apple.com/library/mac/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingLaunchdJobs.html#//apple_ref/doc/uid/10000172i-SW7-BCIEDDBJ