OS X进程管理之launchctl
Apple官方文档
简介
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方案(成功)
注意:进入单用户模式权限太大,太危险,可以为单用户模式加道锁,可选的组合方案如下
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