苹果系统启动脚本的编写规则

发布于 2022-06-27 16:15:40 字数 7822 浏览 5 评论 0

苹果系统启动脚本的编写规则,以下是相关的资料供参考。

http://developer.apple.com/documentation/MacOSX/Conceptual/BPSystemStartup/Tasks/CreatingStartupItems.html

Creating a Startup Item

You can add specialized behavior to the booting sequence by creating new startup items. A startup item, as described in “Startup Items”, is a program or shell script that provides a basic service, such as deleting old files or performing other maintenance tasks prior to the first login session.

Important:   The use of startup items to launch daemons is deprecated. To launch daemons, you should register the daemon with the bootstrap environment as described in “Bootstrap Daemons”.

Contents:
About Startup Items
Creating the Startup Item Executable
Specifying the Startup Item Properties

About Startup Items

Startup items are run (through the SystemStarter program) as the final phase of the booting sequence. At that time, SystemStarter looks for startup items in the /System/Library/StartupItems and /Library/StartupItems directories. It gathers information from the property list of each startup item and uses that information to determine the execution order for the items. It then executes the startup items in groups based on the inter-dependencies among the items.

The /System/Library/StartupItems directory is reserved for startup items that ship with Mac OS X. All other startup items should be placed in the /Library/StartupItems directory. Note that this directory does not exist by default and may need to be created during installation of the startup item.

To create a new startup item, you must create a program or script to execute your code, and you must create a property list file to contain information about your startup item. The following sections describe these techniques in more detail.

Creating the Startup Item Executable

The startup item executable contains your startup item’s code and can be implemented as a command-line executable or shell script. Normally, your executable is called only at system startup time. However, SystemStarter does support the ability to call your executable at a later time (see “Managing Startup Items”).

To create a startup item:
        1.         
Create a directory for your startup item. The directory name should correspond to the behavior you’re providing.

Example: MyDBServer
        2.         
Add your executable to the directory. The name of your executable should be exactly the same as the directory name.

Example: MyDBServer/MyDBServer
        3.         
Create a property list with the name StartupParameters.plist and add it to the directory. See “Specifying the Startup Item Properties”.

Example: MyDBServer/StartupParameters.plist
        4.         
Create an installer to place your startup item in the /Library/StartupItems directory of the target system. (Your installer may need to create this directory first.)

If you are implementing your startup-item executable as a shell script, Mac OS X provides some code to simplify the process of creating your script. The file /etc/rc.common defines routines for processing command-line arguments and for gathering system settings. In your shell script, source the rc.common file and call the RunService routine, passing it the first command-line argument, as shown in the following example:

#!/bin/sh

. /etc/rc.common

#

# Your startup item code

#

RunService "$1"

The RunService routine looks for StartService, StopService, and RestartService routines in your shell script and calls them to start, stop, or restart your services as needed. You must provide implementations for all three routines, although the implementations can be empty for routines whose commands your service does not support.

If your startup-item executable contains code that might take a while to complete, you should consider running that code as a daemon or a background process. Executing lengthy startup tasks directly from your scripts delays system startup. Your startup item script should execute as quickly as possible and then exit.

Specifying the Startup Item Properties

In addition to the startup item executable, every startup item must have a property list with the name StartupParameters.plist in the startup item directory. This property list contains information about the startup item as well as any dependencies this item has on other startup items. Prior to launching any startup items, the SystemStarter program reads the property list of each startup item and uses the dependency information to determine a launch order.

Table 1 lists the key-value pairs to include in the StartupParameters.plist file of your startup item. All arrays and dictionaries contain string values. You can use the Property List Editor application in /Developer/Applications to create this property list.

Table 1 :  StartupParameters.plist key-value pairs

Key

Type

Value

Description

String

A short description of the startup item, used by administrative tools.

Provides

Array

The services provided by this startup item. Although a startup item can potentially provide multiple services, you should limit your startup items to only one.

Requires

Array

The services provided by other startup items that must be run before this startup item can be started. If the required services are not available, this startup item is not run.

Uses

Array

The services provided by other startup items that should be started before this startup item, but which are not mandatory. The startup item should be able to start without the availability of these services.

OrderPreference

String

For those startup items with the same execution order (as determined by the Requires and Uses values), the relative order in which they should be started. There are five order-preference values: First, Early, None, Late, and Last. The default is None. The order preference is an advisory value and might be ignored.

The values you specify for the Requires and Uses properties correspond to the service name on which your startup item is dependent. Service names may not always correspond directly to the name of the startup item that provides that service. The Provides property specifies the names of the services provided by a startup item, and while this name usually matches the name of the startup item, it is not required to do so. In particular, startup items that launch multiple services can have at most one service whose name matches the name of the startup item.

If two startup items provide a service with the same name, SystemStarter runs only the first startup item it finds with that name. For this reason, it is recommended that your startup items do not provide multiple services unless absolutely required by codependencies in your startup code.

The values of the Requires, Uses, and OrderPreference keys do not guarantee a particular launch order.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文