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是用戶登陸后啟動的服務(守護進程)。
開始優化
禁用服務,我們需要用到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到其他目錄備份。
sudo mv /System/Library/LaunchAgents/com.apple.Spotlight.plist ~/launchd.bak
重啟。搞定。是不是很簡單!
我個人比較喜歡這種禁用服務的方式,所以推薦一下。
最后,如果發現服務禁用后,系統或軟件出現異常,可以通過如下命令,還原服務:
方法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無線基站,我沒有這個設備)
順便給大家介紹一些常見進程:
UserEventAgent 提供用戶區程序的事件通知。
TISwitcher 輸入法切換。
SystemUIServer 頂部菜單欄。
Dock 底部菜單欄。。
loginwindow window登陸進程,注銷什么的。
launchd 不用說了,系統主進程。
fontd 字體進程。
bash shell環境主進程,終端。
Airport基站代理 顧名思義。
pboard 剪貼板,就是復制黏貼。
CCacheServer Kerberos tickets 安全模塊,緩存了一些票據,類似密鑰。
cvmsComp 跟opengl相關的進程。
kextd 內核擴展服務。
notifyd 系統內消息服務。
diskarbitrationd 磁盤監控服務,響應usb移動硬盤插入,移除等事件。
configd 系統配置,狀態監控服務。
syslogd 系統日志。
DirectoryService 文件系統權限驗證。
distnoted 外部消息服務,比如iphone, ipad與mac的連接。
ntpd 時間服務。
usbmuxd USB支持。
securityd 安全鑰匙。
mDNSResponder DNS支持。
KernelEventAgent 內核事件響應。
hidd 人體工程學設置支持。
fseventsd FSEvent文件系統事件支持。
dynamic_pager 虛擬內存。swap。
dpd display port支持。
autofsd 自動mount網絡文件系統。
coreservicesd 核心服務進程。
coreaudiod 音頻服務進程。
WindowServer UI界面。
ocspd 對OCRs的安全協議支持。
autofsd 自動mount文件系統,配置在 /etc/auto_master
如何在Mac OS X上創建一個Service服務進程
在Mac上,共有4鍾后台進程類型,可以參考Designing Daemons and Services。要實現啟動服務,又不依賴於用戶,可以使用Launch Daemon。
創建Launch Daemon
-
運行Xcode,創建一個簡單的命令行應用WebTwainService。
123456789101112131415161718#import <Foundation/Foundation.h>
int
main(
int
argc,
const
char
* argv[])
{
@autoreleasepool {
// insert code here...
NSLog(@
"Hello, World!"
);
}
while
(1)
{
sleep(100);
}
return
0;
}
-
編譯工程,把WebTwainService放到/Applications/Dynamsoft/WebTwainService.
-
參考 Create Launch Daemons and Agents,創建配置文件com.dynamsoft.WebTwainService.plist.
-
StandardErrorPath & StandardOutPath: service日志文件路徑
-
KeepAlive. 設置成true. 一旦WebTwainService異常崩潰, 系統會自動重啟服務。
-
Label: 一個唯一值
-
ProgramArguments: 可執行文件路徑
-
這個plist配置文件可放置的地方包括:
-
~/Library/LaunchAgents
-
/Library/LaunchAgents
-
/Library/LaunchDaemons
-
/System/Library/LaunchAgents
-
/System/Library/LaunchDaemons
-
把com.dynamsoft.WebTwainService.plist放到/Library/LaunchDaemons下。
-
要成功注冊服務,還必須修改目錄權限:sudo chown root /Library/LaunchDaemons/com.dynamsoft.WebTwainService.plist.
-
命令行中輸入sudo launchctl運行launchd
-
注冊服務load -D system /Library/LaunchDaemons/com.dynamsoft.WebTwainService.plist。然后我們可以看到服務被加載了:
-
輸入list可以查看運行的進程
-
現在重啟系統,然后打開日志/var/log/webtwain.log,查看服務是否在運行了: