1. 程式人生 > >Mac OS啟動服務優化高階篇(launchd tuning)

Mac OS啟動服務優化高階篇(launchd tuning)

Mac下的啟動服務主要有三個地方可配置:
1,系統偏好設定->帳戶->登陸項
2,/System/Library/StartupItems 和 /Library/StartupItems/
3,launchd 系統初始化程序配置。

前兩種優化比較簡單,本文主要介紹的是第三種更為複雜的launchd配置優化。
launchd是Mac OS下,用於初始化系統環境的關鍵程序。類似Linux下的init, rc。

我們先來看一下Mac OS X的啟動原理:
1,mac韌體啟用,初始化硬體,載入BootX引導器。
2,BootX載入核心與核心擴充套件(kext)。
3,核心啟動launchd程序。
4,launchd根據 ?/System/Library/LaunchAgents ,?/System/Library/LaunchDaemons , ?/Library/LaunchDaemons,?Library/LaunchAgents , ~/Library/LaunchAgents?裡的plist配置,啟動服務守護程序。

看完了Mac OS X的啟動原理,我們不難發覺?/System/Library/LaunchAgents ,?/System/Library/LaunchDaemons , ?/Library/LaunchDaemons,?Library/LaunchAgents?五個目錄下的plist屬性檔案是優化系統的關鍵。

下面再來理解幾個基礎概念:
/System/Library和/Library和~/Library目錄的區別?
/System/Library目錄是存放Apple自己開發的軟體。
/Library目錄是系統管理員存放的第三方軟體。
~/Library/是使用者自己存放的第三方軟體。

LaunchDaemons和LaunchAgents的區別?


LaunchDaemons是使用者未登陸前就啟動的服務(守護程序)。
LaunchAgents是使用者登陸後啟動的服務(守護程序)。

上面提到的五個目錄下的plist檔案格式及每個欄位的含義:

Key Description Required
Label The name of the job yes
ProgramArguments Strings to pass to the program when it is executed yes
UserName The job will be run as the given user, who may not necessarily be the one who submitted it to launchd. no
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配置嗎?沒關係,我們的優化策略是完全解除安裝服務,所以我們不用關心plist裡的配置含義。

開始優化

禁用服務,我們需要用到Mac OS提供的一個工具指令-launchctl
launchctl 指令會針對服務設定一個禁用標誌,launchd啟動時會先檢查這個服務是否被禁用,從而確定是否需要啟用這個服務。

禁用服務的方法1
先找到禁用標誌檔案 /var/db/launchd.db/com.apple.launchd/overrides.plist,檢視你要禁用的服務是否已被禁用了。
有些服務已被禁用,但未列在overrides.plist裡。此時,你還需要檢查這個服務的plist檔案Label欄位是否已經標記為 Disable。

確認這個服務未禁用後,我們就可以通過呼叫如下命令,來禁用服務:
sudo launchctl unload plist檔案路徑
sudo launchctl unload -w?plist檔案路徑
比如,我想禁用spotlight,則輸入
sudo launchctl unload?/System/Library/LaunchAgents/com.apple.Spotlight.plist
sudo launchctl unload -w?/System/Library/LaunchAgents/com.apple.Spotlight.plist

禁用完服務以後,重啟Mac OS即可生效。

禁用服務的方法2,一種更有效且暴力的方法(推薦)
先解除安裝服務
sudo launchctl unload /System/Library/LaunchAgents/com.apple.Spotlight.plist
然後將plist檔案mv到其他目錄備份。重啟。搞定。是不是很簡單!

我個人比較喜歡這種禁用服務的方式,所以推薦一下。

如果發現服務禁用後,系統或軟體出現異常,可以通過如下命令,還原服務:
方法1:
sudo launchctl load -wF?plist檔案路徑
方法2:
將備份的plist檔案mv回原來的資料夾。
sudo launchctl load plist檔案路徑

注意:系統級服務的禁用要異常小心,請在禁用前google,確保你熟知這個服務的作用。否則可能導致系統無法啟動。
最安全的做法就是不要去禁用它了。

當然,使用者服務我們還是可以放心禁用的,有問題最多再啟用唄。

下面是我禁用的服務列表:
/System/Library/LaunchDaemons/com.apple.metadata.mds.plist (禁用spotlight的前提)
/System/Library/LaunchAgents/com.apple.Spotlight.plist (Spotlight)
/Library/LaunchDaemons/com.google.keystone.daemon.plist ?(Google Software Update)
/Library/LaunchAgents/com.google.keystone.root.agent ?(Google Software Update)
~/Library/LaunchAgents/com.google.keystone.agent.plist?(Google Software Update,使用者下的程序不需要加 sudo)
~/Library/LaunchAgents/com.apple.CSConfigDotMacCert-ken.wug\@me.com-SharedServices.Agent.plist (me.com的共享服務,我不用)
/System/Library/LaunchDaemons/org.cups.cupsd.plist (印表機)
/System/Library/LaunchDaemons/org.cups.cups-lpd.plist (印表機)
/System/Library/LaunchDaemons/com.apple.blued.plist (藍芽)
/System/Library/LaunchAgents/com.apple.AirPortBaseStationAgent.plist (apple無線基站,我沒有這個裝置)

知道守護程序(服務)名,如何找到對應的plist檔案?
將程序(服務)名拷貝,然後到 /System/Library/LaunchAgents ,?/System/Library/LaunchDaemons , ?/Library/LaunchDaemons,?Library/LaunchAgents , ~/Library/LaunchAgents 五個目錄裡,通過以下命令查詢:

ll|grep 程序(服務)名
比如
ll|grep blued
在 ?/System/Library/LaunchDaemons 中找到了它。接下來,請按上面指導的步驟,禁用該服務。