系统原生应用程序的自自启动项

自启动脚本目录

/Library/StartupItems /System/Library/StartupItems

通过MacPorts安装的软件的自启动项

A StartupItem is a MacPorts facility to run "daemons," a Unix term for programs that run continuously in the background, rather than under the direct control of a user; for example, mail servers, network listeners, etc. Ports that use StartupItem keywords create Mac OS X scripts for launchd, which is the Apple facility introduced with Mac OS X 10.4 to replace xinetd for starting and managing daemons. To support launchd, a program named daemondo is provided by MacPorts base that serves as an adapter between Mac OS X's launchd and daemons (“executable” StartupItems) or traditional Unix startup scripts that start daemons (“script” StartupItems).

There are three categories of StartupItem keywords. Those that trigger StartupItem creation and logging, those that specify attributes of “executable” StartupItems, and those that specify attributes of “script” StartupItems.

  • Note
The variable startupitem_type in ${prefix}/etc/macports/macports.conf may be set to none to globally override all StartupItem keywords found in Portfiles; this prevents StartupItems from being created.

其中${prefix}值默认为/opt/local,在${prefix}/etc/macports/macports.conf中有定义。

launchctl机制

man launchctl

启动项属性

本节关键词是“可执行程序”或“脚本”可启动项。

可执行程序启动项

启动项属性名称 默认值 实例 说明
startupitem.create no startupitem.create yes 是否创建启动项
startupitem.name ${name} startupitem.name dhcpd 设置启动项名称,默认为port名称

脚本启动项

从launchd加载/卸载可启动项

A port with a StartupItem places a link to a .plist file for the port's daemon within /Library/LaunchDaemons/. A .plist file is an XML file; MacPorts installs .plist files tagged as “disabled” for the sake of security. You may enable a startup script (tag the.plist file as “enabled”) and load it into launchd with a single command as shown.

sudo launchctl load -w /Library/LaunchDaemons/org.macports.mysql5.plist

You may stop a running startup script, disable it (tag the.plist file as “disabled”), and unload it from launchd with a single command as shown.

sudo launchctl unload -w /Library/LaunchDaemons/org.macports.mysql5.plist

启动项内部细节

During port installation a MacPorts StartupItem creates a .plist file in ${prefix}/etc/LaunchDaemons/, and places a symbolic link to the .plist file within /Library/LaunchDaemons/.

For example, the StartupItem for the mysql5 port is org.macports.mysql5.plist, and it is linked as shown.

$ ls -l /Library/LaunchDaemons
org.macports.mysql5.plist ->
     /opt/local/etc/LaunchDaemons/org.macports.mysql5/org.macports.mysql5.plist

For “script” StartupItems, in addition to a .plist file, a wrapper is also created.

$ ls -l /opt/local/etc/LaunchDaemons/org.macports.mysql5/
-rwxr-xr-x   2 root  wheel  475 Aug  2 14:16 mysql5.wrapper
-rw-r--r--   2 root  wheel  975 Aug  2 14:16 org.macports.mysql5.plist

The wrapper manipulates the script as specified in the startupitem.start and startupitem.stop keywords. An example wrapper script snippet is shown below.

#!/bin/sh

# MacPorts generated daemondo support script

# Start
Start()
{
    /opt/local/share/mysql5/mysql/mysql.server start
}

# Stop
Stop()
{
    /opt/local/share/mysql5/mysql/mysql.server stop
}
#
    [... trimmed ...]

参考资料

返回顶部