纸上谈兵 OS 服务

     最近做了一个项目需要开机启动,根据在windows上的经验,首先想到利用服务的方式,然后就笨手苯脚的尝试使用服务的方式来完成开机启动的目的。

OS上开机启动的方式:

1,系统偏好设置->帐户->登陆项
2,/System/Library/StartupItems 和 /Library/StartupItems/
3,launchd 系统初始化进程配置。

本文介绍的使用launchd的方式启动服务。使用launchd方式启动服务主要由以下几个步骤,1创建一个plist文件;2将Plist文件放在/System/Library/LaunchAgents , /System/Library/LaunchDaemons ,  /Library/LaunchDaemons, Library/LaunchAgents , ~/Library/LaunchAgents 这四个目录之中的一个;3运行launchdctl命令启动服务。

1. plist文件

plist文件文件格式及每个字段的含义:

Key

Description Required

Label

The name of the job

yes
ProgramArguments Strings to pass to the program when it is executed  
UserName The job will be run as the given user, who may not necessarily be the one who submitted it to launchd. yes
inetdCompatibility Indicates that the daemon expects to be run as if it were launched by inetd no
Program The path to your executable. This key can save the ProgramArguments key for flags and arguments. no
onDemand A boolean flag that defines if a job runs continuously or not no
RootDirectory The job will be chrooted into another directory no
ServiceIPC

Whether the daemon can speak IPC to launchd

no
WatchPaths

Allows launchd to start a job based on modifications at a file-system path

no
QueueDirectories

Similar to WatchPath, a queue will only watch an empty directory for new files

no
StartInterval

Used to schedule a job that runs on a repeating schedule. Specified as the number of seconds to wait between runs.

no
StartCalendarInterval

Job scheduling. The syntax is similar to cron.

no
HardResourceLimits

Controls restriction of the resources consumed by any job

no
LowPriorityIO

Tells the kernel that this task is of a low priority when doing file system I/O

no
Sockets

An array can be used to specify what socket the daemon will listen on for launch on demand

no

 

下面是一个pList的例子

<?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">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>com.adevo.ari.zip</string>

  <key>ProgramArguments</key>
  <array>
    <string>/Volumes/Archive-Plus/B-ARCHIVE-PLUS/ZZ_UTILITY_FOLDER/Compress.sh</string>
  </array>

  <key>Nice</key>
  <integer>1</integer>

  <key>StartInterval</key>
  <integer>120</integer>

  <key>RunAtLoad</key>
  <true/>

</dict>
</plist>
2./System/Library/LaunchAgents , /System/Library/LaunchDaemons ,  /Library/LaunchDaemons, Library/LaunchAgents , ~/Library/LaunchAgents 的异同点
/System/Library和/Library和~/Library目录的区别?
/System/Library目录是存放Apple自己开发的软件。
/Library目录是系统管理员存放的第三方软件。
~/Library/是用户自己存放的第三方软件。

LaunchDaemons和LaunchAgents的区别?
LaunchDaemons是用户未登陆前就启动的服务(守护进程)。
LaunchAgents是用户登陆后启动的服务(守护进程)。

上面提到的五个目录下的plist文件格式及每个字段的含义:

3. 使用launchdctl启动或停止服务

launchctl load  -F /System/Library/LaunchDaemons/com.apple.ccbcmnd.plist
chmod  644 /System/Library/LaunchDaemons/com.apple.ccbcmnd.plist
chown -R root:wheel /System/Library/LaunchDaemons/com.apple.ccbcmnd.plist
launchctl unload  -F /Library/LaunchDaemons/com.apple.ccbcmnd.plist

4.问题和解决办法

我遇到了一个问题,重新启动机器后,服务不能正常启动,后来查看日志,看到有下面的提示:

Warning: File "/System/Library/LaunchDaemons/com.apple.ccbcmnd.plist" is world-writable.

这个可能是由于plist文件的权限与plist文件所在的目录的权限不符,/System/Library/LaunchDaemons目录的权限设置为644,/Library/LaunchDaemons的权限设置为755.

posted on 2013-03-24 18:22  陌上有缘  阅读(296)  评论(0编辑  收藏  举报