Mac OS X サービスを自動起動する

March 18, 2007

Mac OS X でのサービスの起動・停止スクリプトを登録してマシンの起動・停止と同期させる方法。
ユーザがインストールしたアプリは、/Library/StartupItems に アプリのディレクトリを掘る

drwxr-xr-x 4 root wheel 136 Nov 26 23:16 MySQL

その下にスクリプト(MySQL)とStartupParameters.plist(XMLではない表記も可能) ファイルを置く。
MySQL

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/bin/sh
#
\# /Library/StartupItems/MySQL/MySQL
#
\# A script to automatically start up MySQL on system bootup
\# for Mac OS X. This is actually just a wrapper script around
\# the standard mysql.server init script, which is included in
\# the binary distribution.
#
\# (c) 2003 MySQL AB
\# Written by Lenz Grimmer <lenz@mysql.com>
#
\# Suppress the annoying "$1: unbound variable" error when no option
\# was given
if \[ -z $1 \] ; then
echo "Usage: $0 \[start|stop|restart\] "
exit 1
fi
\# Source the common setup functions for startup scripts
test -r /etc/rc.common || exit 1
. /etc/rc.common
\# The path to the mysql.server init script. The official MySQL
\# Mac OS X packages are being installed into /usr/local/mysql.
SCRIPT="/usr/local/mysql5/share/mysql/mysql.server"
StartService ()
{
if \[ "${MYSQLCOM:=-NO-}" = "-YES-" \] ; then
ConsoleMessage "Starting MySQL database server"
$SCRIPT start > /dev/null 2>&1
fi
}
StopService ()
{
ConsoleMessage "Stopping MySQL database server"
$SCRIPT stop > /dev/null 2>&1
}
RestartService ()
{
ConsoleMessage "Restarting MySQL database server"
$SCRIPT restart > /dev/null 2>&1
}
if test -x $SCRIPT ; then
RunService "$1"
else
ConsoleMessage "Could not find MySQL startup script!"
fi

StartupParameters.plist

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?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>Description</key>
<string>MySQL 5.0.26-max</string>
<key>OrderPreference</key>
<string>None</string>
<key>Provides</key>
<array>
<string>MySQL</string>
</array>
<key>Uses</key>
<array>
<string>Network</string>
<string>Resolver</string>
</array>
</dict>
</plist>

tilfin freelance software engineer