Mac进程管理浅谈

来源:互联网 发布:页面数据undefined 编辑:程序博客网 时间:2024/04/29 19:15

在Mac系统中,launchd进程类似于unix/liunx系统的init进程,充当系统initial的角色,同样类似于Linux系统init脚本,launchd加载读取plist文件启动daemon,实现操作系统的基本服务结构,在daemon驻守的基础上再依据用户的操作触发dependency进程。

launchd -> daemon/agent -> dependency

在Mac系统的语境里,传统的驻守进程又分为了两类:Daemon和Agent。

  1. 其中Daemon是系统级别的驻留进程,默认以root身份启动执行;Agent是用户级别的驻留进程,默认以系统用户身份启动执行。

  2. 此外也在plist文件可以通过UserName字段定义启动执行的identity,一般用来实现系统级别的进程以特定用户的身份执行。

注意:所有plist文件的owner都是root,也即是launchd读取加载plist文件,在以相应的identity执行命令触发进程。

/Library/LaunchDaemons/com.ericsson.tt.uecontrol.hostcontroller<?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.ericsson.tt.uecontrol.hostcontroller</string>    <key>UserName</key>    <string>ericsson</string>    <key>ProgramArguments</key>    <array>        <string>/Users/ericsson/documents/uec/uecontrol-CXC_173_6456-R38A02/hostcontroller.sh</string>    </array>    <key>KeepAlive</key>    <true/></dict></plist>

官方对plist文件的定义如下:

launchd.plist – System wide and per-user daemon/agent configuration files.

在Mac系统中launchd会读取plist文件的默认文件夹有以下五个:

plist Files Directory~/Library/LaunchAgents         Per-user agents provided by the user./Library/LaunchAgents          Per-user agents provided by the administrator./Library/LaunchDaemons         System-wide daemons provided by the administrator./System/Library/LaunchAgents   Per-user agents provided by Mac OS X./System/Library/LaunchDaemons  System-wide daemons provided by Mac OS X.

Daemons和Agents文件夹的区别在于Daemon文件夹里的plist文件在launchd启动后直接加载并执行配置,而Agents文件夹下plist文件需要用户登陆后才会被launchd加载并执行配置;HOME路径下agents plist文件只有对应的用户登陆系统才会加载执行,其余两个agents plist文件任何用户登陆系统都会加载执行。

reference from launchctl(1), sysctl(3), launchd(8), plist(5), launchd.plist(5)

另外在Mac系统里launchd不仅仅是起始进程这么简单,Unix/Linux系统中实现定时任务的Cron服务也由launchd实现,即可以在plist文件中定义程序执行时间点和时间间隔。

假定当前有一个可执行shell脚本example.sh,针对该脚本执行可构建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>        <!-- plist/daemon/agent name, need uniq globally -->        <key>Label</key>        <string>com.adobe.fpsaud</string>        <!-- program location to be executed -->        <key>Program</key>        <string>/Library/Application Support/Adobe/Flash Player Install Manager/fpsaud/example.sh</string>        <!-- the first arguments is the program command, the others is program parameters -->        <key>ProgramArguments</key>        <array>                <string>/Library/Application Support/Adobe/Flash Player Install Manager/fpsaud/example.sh</string>        </array>        <!-- time point to execute program, can only used without Startinterval -->        <key>StartCalendarInterval</key>        <dict>                <key>Minute</key>                <integer>30</integer>                <key>Hour</key>                <integer>11</integer>        </dict>        <!-- time slot to execute program, can only used without StartCalendarInterval -->        <key>StartInterval</key>        <integer>3600</integer>        <key>KeepAlive</key>        <false/>        <key>StandardOutPath</key>        <string>/Users/Ericsson/Documents/souche/task.log</string>        <key>StandardErrorPath</key>        <string>/Users/Ericsson/Documents/souche/task.err</string></dict></plist>   

plist加载&执行机制

  1. 并不是所有放置在LaunchDaemons/LaunchAgents路径下的plist文件都会被launchd加载,系统提供服务禁止标志文件筛选出disable的plist。(该功能由launchctl load/unload/start/stop提供,不过在最新的MAC系统该特性似乎被移除,launchctl只能控制当前系统中的plist加载情况,当系统重启后所有文件夹下的plist文件都会加载,not sure why?)。
  2. plist文件提供Disabled字段来禁止该plist的运行。
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"><dict>        <key>Disabled</key>        <true/>        <key>Label</key>        <string>com.apple.tftpd</string>        <key>ProgramArguments</key>        <array>                <string>/usr/libexec/tftpd</string>                <string>-i</string>                <string>/private/tftpboot</string>        </array>        <key>inetdCompatibility</key>        <dict>                <key>Wait</key>                <true/>        </dict>        <key>InitGroups</key>        <true/>        <key>Sockets</key>        <dict>                <key>Listeners</key>                <dict>                        <key>SockServiceName</key>                        <string>tftp</string>                        <key>SockType</key>                        <string>dgram</string>                </dict>        </dict></dict></plist>

launchctl

launchctl is the interface with launchd to load, unload daemons/agents and generally control launchd.

因此launchctl命令类似于Linux系统中的chkconfig/service命令。

针对自己定义的plist文件可以执行命令:

launchctl load com.adobe.fpsaud.plist       加载到驻留程序列表       launchctl unload com.adobe.fpsaud.plist     从驻留程序列表中祛除,相应地进程释放launchctl start com.adobe.fpsaud.plist      立刻执行程序launchctl stop com.adobe.fpsaud.plist       停止程序执行launchctl list  

reference from http://blog.csdn.net/guojin08/article/details/19925321

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Mac程序组织形式

  1. Application Package: xx.app/contents/yy/
  2. Executable Command
  3. pkg / dmg

plist字段列表

这里写图片描述

0 0