本文主要是介绍创建守护进程的属性文件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
上一章说了人员和添加和删除守护进程,这章简单说说如何创建一个守护进程文件
守护进程文件是一个字典,苹果提供了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 letdemand and conditions control the invocation. The default is false and therefore only demand will startthe job. The value may be set to true to unconditionally keep the job alive. Alternatively, a dictio-nary dictionarynary of conditions may be specified to selectively control whether launchd keeps a job alive or not. Ifmultiple keys are provided, launchd ORs them, thus providing maximum flexibility to the job to refinethe logic and stall if necessary. If launchd finds no reason to restart the job, it falls back ondemand based invocation. Jobs that exit quickly and frequently when configured to be kept alive willbe 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 ofthe array of strings provided to the ProgramArguments will be used instead. This key is required inthe 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 thejob 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
这篇关于创建守护进程的属性文件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!