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

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

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相关推荐

  1. [OS复习]进程管理2

    问题:多个进程竞争内存资源 1.解决方法 方案一:采用交换技术,换出一部分进程到外存,以腾出内存空间 方案二:采用虚拟存储技术,每个进程只能装入一部分程序和数据(存储管理部分) 2.对换技术(交换技术 ...

  2. python中的os abort_Python::OS 模块 -- 进程管理

    这里我们介绍os模块中的进程管理相关的操作. os模块提供给了我们访问操作系统功能的接口,我们可以通过os模块提供给我们的进程管理接口,编写多进程程序,这对编写高效.并发的程序提供了方便. 下面是一个 ...

  3. python os模块进程管理

    2019独角兽企业重金招聘Python工程师标准>>> 有两种方式来实现并发性,一种方式是让每个"任务"或"进程"在单独的内在空间中工作,每个 ...

  4. [OS复习]进程管理5

    线程 1.多线程 操作系统中引入进程的目的: 为了描述和实现多个程序的并发执行,以改善资源利用率及提高系统的吞吐量. 操作系统引入线程的目的: 这是为了减少程序并发执行时系统所付出的额外开销(减少管理 ...

  5. [OS复习]进程管理4

    进程调度算法(Short-Term) 1.先来先服务(FCFS) 该方法按照进程到达的先后顺序排队,每次调度队首的进程(就像超市中购物付款一样). FCFS算法属于非剥夺调度方式,实现简单,看似公平. ...

  6. Python之OS模块进程管理介绍--os.fork()

    转自:http://davidbj.blog.51cto.com/4159484/1240586 有两种方式来实现并发性,一种方式是让每个"任务"或"进程"在单 ...

  7. [OS复习]进程管理3

    进程调度方式及类型 1.进程调度方式 根据执行进程的处理机是由进程自己释放,还是被强行剥夺,可以将进程调度方式分为非剥夺方式和剥夺方式两种. 1.1非剥夺方式 执行进程只有在执行完毕,或因申请I/O阻 ...

  8. OS之进程管理 --- 死锁

    什么是死锁 在正常操作模式下,进程按如下顺序来使用资源: 申请:进程请求资源 使用:进程对资源进行操作 释放:进程释放资源 当一组进程中的每一个进程度在等待一个事件,而这事件只能有一组进程的另一个进程 ...

  9. ActivityManagerService之进程管理(四)

    一.初步了解AMS的进程管理 引用邓凡平的深入理解系列的一些知识 1.  Linux进程调度优先级和调度策略 调度优先级和调度策略是操作系统中一个很重要的概念.简而言之,它是系统中CPU资源的管理和控 ...

最新文章

  1. 快速部署RDA Remote Diagnostic Agent
  2. 中科院博士因论文致谢走红后,回到母校演讲再刷屏!网友:是对寒门学子最好的激励...
  3. mysql utf8mb4 java_mysql中utf8和utf8mb4区别
  4. 广西计算机一级考试是机考吗,广西计算机一级考试机考模式探索及应用.doc
  5. she is so css什么意思,输入she is so什么意思 微信she is so什么梗
  6. MTK 驱动---(11) EMI定制
  7. 工作中任务管理的四个原则和四个技能
  8. spring 4.x下让http请求返回json串
  9. [加壳脱壳] VMP壳原理简介
  10. linux把m4s格式转换mp4,批量将哔哩哔哩缓存M4S格式视频改成MP4格式
  11. 单细胞测序系列之三:单细胞转录组测序
  12. java web服务_如何用Java实现Web服务器
  13. [论文阅读] (23)恶意代码作者溯源(去匿名化)经典论文阅读:二进制和源代码对比
  14. 常用的几种集合(Map另写)
  15. 用Python实现斗地主游戏(终端版)
  16. BS架构和CS架构的比对
  17. 移动联通电信wap和net接入点判断
  18. 宽度优先搜索算法(BFS)详解(超级详细讲解,附有大图)
  19. 上班族的英语学习计划
  20. 磁性元器件的分布参数

热门文章

  1. 【Qt】Qt窗口程序
  2. 【Linux】一步一步学Linux——consoletype命令(247)
  3. 【Linux】一步一步学Linux——curl命令(193)
  4. 【工具类】工具相关参考文档汇总
  5. redis 一般启动几个 哨兵_Redis 5.0.8 主从+哨兵的搭建
  6. 每天一道LeetCode-----计算给定序列中所有长度为k的滑动窗的最大值集合
  7. 搜索旋转排序数组—leetcode31
  8. android 屏幕禁止,Android应用禁止屏幕休眠的几种方法
  9. php 进制 小数,小数进制转换
  10. HDU Problem - 2732 Leapin' Lizards(最大流,拆点建边)