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 中找到了它。接下來,請按上面指導的步驟,禁用該服務。
新聞熱點
疑難解答