OS X进程管理之launchctl
Apple官方文檔
如果 Mac 無法完成啟動,請嘗試安全模式
如果 Mac 無法開機應如何處理
在 Mac OS X 中設置固件密碼保護 如何重置 Mac 上的 NVRAM 重置 Mac 上的系統管理控制器 (SMC) 在 Mac 上使用固件密碼簡介 launchd之于OS X相對于Unix like系統而言如init,systemd,rc等,OS X的服務管理文件為.plist 關于launchctl可以man launchctl獲得詳細幫助
OS X級別開機啟動項.plist /Library/StartupItems/
/System/Library/StartupItems/
DEPRECATED AND REMOVED FUNCTIONALITY
?? ?launchctl no longer has an interactive mode, nor does it accept commands from stdin.
?? ? The /etc/launchd.conf file is no longer consulted for subcommands to run during early
?? ? boot time; this functionality was removed for security considerations. While it was
?? ? documented that $HOME/.launchd.conf would be consulted prior to setting up a user's
?? ? session, this functionality was never implemented.
?? ? launchd no longer uses Unix domain sockets for communication, so the LAUNCHD_SOCKET
?? ? environment variable is no longer relevant and is not set.
?? ?launchd no longer loads configuration files from the network
FILES
?? ? ~/Library/LaunchAgents ? ? ? ? Per-user agents provided by the user.
?? ? /Library/LaunchAgents? ? ? ? ? Per-user agents provided by the administrator.
?? ? /Library/LaunchDaemons ? ? ? ? System wide daemons provided by the administrator.
?? ? /System/Library/LaunchAgents ? OS X Per-user agents.
?? ? /System/Library/LaunchDaemons? OS X System wide daemons.
插曲--Mac Pro無法啟動
本來想禁止apache開機自啟動
launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist
結果太大意,沒想到手快了一步,命令敲成了
launchctl unload -w /System/Library/LaunchDaemons/
我的個乖乖,突然觸摸板,鍵盤全部失靈,有點慌了,怎么回事兒,還好有點linux基礎,看了下命令,把系統的很多Daemons直接停掉并去除了開機啟動了,重啟后直接卡死在蘋果LOGO下,進度好容易走完了,卻看不能美美的桌面,完了。真的是沒法加載相關Daemon從而加載硬件驅動,系統無法正常啟動。
問了下度娘,有說重新加載SMC的,詳風蘋果官方文檔https://support.apple.com/zh-cn/HT201295
試了下,沒效果不說,反而風扇狂轉不止,更加擔心了,難道要重裝,oh, my god
又百度了下,看了下無法啟動的基本排查思路和順序,理了理頭緒,既然是不小心去掉了開機啟動項,還原回去就可以了,于是
A方案(失敗)
進Recovery模式,將unload掉的全部加入到開機啟動項后重啟
chroot /Volumes/Macintosh HD
cp -a System/Library/LaunchDaemons/* Library/StartupItems/
cp -a System/Library/LaunchDaemons/* System/Library/StartupItems/ exit launchctl reboot 失敗告終B方案(成功) 如何以單用戶模式或詳細模式啟動 Mac 注意:進入單用戶模式權限太大,太危險,可以為單用戶模式加道鎖,可選的組合方案如下 1.啟用FileVault,進入單用戶模式時會要求輸入密碼確認(性能比沒做磁盤加密肯定會低些) 2.設置固件密碼,阻止光盤,U盤等其它第三方介質啟動,只有關閉固件密碼后才能進入單用戶模式
Command+S進入單用戶模式 說明:OS X也有單用戶模式,和linux很像,字符界面 以讀寫模式掛載根 mount -uw / launchctl load -w /System/Library/LaunchDaemons/ 只看到一大堆提示加載什么成功,之后便進入到了美美的圖形界面,這時心里就有底了
可是問題來了 再次重啟,還是無法正常進入圖形
想想,只是在單用戶模式下臨時加載了daemon而啟動到了多用戶圖形模式,而該模式(正常的圖形模式)并沒有將unload的daemon加入到開機啟動項,所以仍然無法正常啟動。于是, 重復上述動作進入圖形后,再次執行launchctl load -wF /System/Library/LaunchDaemons/,讓圖形模式的開機啟動項中也有被unload掉的daemon 終于,再次重啟就一切回歸正常了,感謝萬能的度娘
man launchctl中有這么一小段,更加說明了此次無法正常啟動的緣由 簡單點講,就是放在/System/Library/LaunchDaemons某個第三方daemon可能導致系統無法正常啟動,而我的情況是/System/Library/LaunchDaemons下的所以daemon都被unload掉了,系統當然無法正常啟動
Note that allowing non-root write access to the
? ? ? ? ? ? ?/System/Library/LaunchDaemons directory WILL render your system unbootable.
? ? ? ? ? ? ?-w ? ? ? Overrides the Disabled key and sets it to false or true for the
?? ? ? ? ? ? ? ? ? ? ? load and unload subcommands respectively. In previous versions,
?? ? ? ? ? ? ? ? ? ? ? this option would modify the configuration file. Now the state of
?? ? ? ? ? ? ? ? ? ? ? the Disabled key is stored elsewhere on- disk in a location that
?? ? ? ? ? ? ? ? ? ? ? may not be directly manipulated by any process other than launchd.
正題 常用的幾個launchctl命令 launchctl start|stop|list|remove label launchctl load|unload paths launchctl print|kickstart|kill|enable|disable?service-target
以apache為例 /System/Library/LaunchDaemons/org.apache.httpd.plist ? #就是path org.apache.httpd #就是label
1.daemon stop
jlive@MacBook-Pro:~ $su -
Password:
MacBook-Pro:~ root# ls /System/Library/LaunchDaemons/org.apache.httpd.plist?
/System/Library/LaunchDaemons/org.apache.httpd.plist
MacBook-Pro:~ root# ps -ef|grep httpd
? ? 0? ? 87 ? ? 1 ? 0 11:18PM ?? ? ? ? ? 0:01.56 /usr/sbin/httpd -D FOREGROUND
?? 70 ? 246? ? 87 ? 0 11:18PM ?? ? ? ? ? 0:00.03 /usr/sbin/httpd -D FOREGROUND
? ? 0? 3971? 3964 ? 0? 3:20PM ttys000? ? 0:00.00 grep httpd
MacBook-Pro:~ root# launchctl stop org.apache.httpd
MacBook-Pro:~ root# ps -ef|grep httpd
? ? 0? 3988? 3964 ? 0? 3:20PM ttys000? ? 0:00.00 grep httpd
2.daemon start
MacBook-Pro:~ root# launchctl start org.apache.httpd
MacBook-Pro:~ root# ps -ef|grep httpd
? ? 0? 4000 ? ? 1 ? 0? 3:23PM ?? ? ? ? ? 0:00.15 /usr/sbin/httpd -D FOREGROUND
?? 70? 4001? 4000 ? 0? 3:23PM ?? ? ? ? ? 0:00.00 /usr/sbin/httpd -D FOREGROUND
3.daemon list
MacBook-Pro:~ root# launchctl list|grep apache
4035 0 org.apache.httpd
? ? 0? 4003? 3964 ? 0? 3:23PM ttys000? ? 0:00.00 grep httpd
說明:load|unload并不一定會start|stop相關服務,它只是在start或stop前載入相關配置環境
4.unload plist
MacBook-Pro:~ root# launchctl unload /System/Library/LaunchDaemons/org.apache.httpd.plist?
MacBook-Pro:~ root# ps -ef|grep httpd
? ? 0? 4237? 4221 ? 0? 4:01PM ttys001? ? 0:00.00 grep httpd
4.load plist
MacBook-Pro:~ root# launchctl load /System/Library/LaunchDaemons/org.apache.httpd.plist?
MacBook-Pro:~ root# ps -ef|grep httpd
? ? 0? 4241 ? ? 1 ? 0? 4:01PM ?? ? ? ? ? 0:00.16 /usr/sbin/httpd -D FOREGROUND
?? 70? 4243? 4241 ? 0? 4:01PM ?? ? ? ? ? 0:00.00 /usr/sbin/httpd -D FOREGROUND
? ? 0? 4245? 4221 ? 0? 4:01PM ttys001? ? 0:00.00 grep httpd
5.print service-target
MacBook-Pro:~ root# launchctl print system/org.apache.httpd|head
org.apache.httpd = {
active count = 1
path = /System/Library/LaunchDaemons/org.apache.httpd.plist
state = running
program = /usr/sbin/httpd-wrapper
arguments = {
/usr/sbin/httpd-wrapper
-D
?
FOREGROUND
6.kill service-target
MacBook-Pro:~ root# ps -ef|grep httpd
? ? 0?4417 ? ? 1 ? 0? 4:21PM ?? ? ? ? ? 0:00.16 /usr/sbin/httpd -D FOREGROUND
?? 70? 4423? 4417 ? 0? 4:21PM ?? ? ? ? ? 0:00.00 /usr/sbin/httpd -D FOREGROUND
? ? 0? 4429? 4221 ? 0? 4:21PM ttys001? ? 0:00.00 grep httpd
MacBook-Pro:~ root# launchctl kill SIGKILL system/org.apache.httpd
MacBook-Pro:~ root# ps -ef|grep httpd
? ? 0?4431 ? ? 1 ? 0? 4:21PM ?? ? ? ? ? 0:00.15 /usr/sbin/httpd -D FOREGROUND
?? 70? 4432? 4431 ? 0? 4:21PM ?? ? ? ? ? 0:00.00 /usr/sbin/httpd -D FOREGROUND
? ? 0? 4434? 4221 ? 0? 4:21PM ttys001? ? 0:00.00 grep httpd
7.disable service-target
MacBook-Pro:~ root# launchctl disable system/org.apache.httpd
MacBook-Pro:~ root# ps -ef|grep httpd
? ? 0? 4451? 4221 ? 0? 4:24PM ttys001? ? 0:00.00 grep httpd
MacBook-Pro:~ root# launchctl load /System/Library/LaunchDaemons/org.apache.httpd.plist?
/System/Library/LaunchDaemons/org.apache.httpd.plist: Service is disabled
8.enable service-target
MacBook-Pro:~ root# launchctl enable system/org.apache.httpd
MacBook-Pro:~ root# launchctl load /System/Library/LaunchDaemons/org.apache.httpd.plist?
MacBook-Pro:~ root# ps -ef|grep httpd
? ? 0? 4458 ? ? 1 ? 0? 4:25PM ?? ? ? ? ? 0:00.16 /usr/sbin/httpd -D FOREGROUND
?? 70? 4459? 4458 ? 0? 4:25PM ?? ? ? ? ? 0:00.00 /usr/sbin/httpd -D FOREGROUND
? ? 0? 4461? 4221 ? 0? 4:25PM ttys001? ? 0:00.00 grep httpd
轉載于:https://www.cnblogs.com/lixuebin/p/10814156.html
總結
以上是生活随笔為你收集整理的OS X进程管理之launchctl的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OpenCV 之 直方图处理
- 下一篇: Java---设计模块(单例的变形)(多