launchdlaunchctl

launchdmacos零碎下的服务治理框架,用于启动,进行,治理守护过程和服务,是第一个过程,PID为1,创立了所有其余过程

launchctllaunchd的管理工具,二者之间的关系相似于linux零碎的systemdsystemctl

根本逻辑

  1. launchd启动后,扫描/System/Library/LaunchDaemons/Library/LaunchDaemons中的plist文件并加载,以root或者指定用户权限运行,在开机未输出明码的时候就开始运行,如果要配置开机自启动服务,举荐把plist文件放到/Library/LaunchDaemons
  2. 输出明码登入零碎后,扫描/System/Library/LaunchAgents/Library/LaunchAgentsplist文件所有用户可见)、~/Library/LaunchAgentsplist以后用户可见)这三个目录中的plist文件并加载,以以后用户权限运行,在开机并且输出账号密码之后开始运行
  3. 每一个plist文件,都叫一个Job(即一个工作),只有plist里设置了RunAtLoadtruekeepAlivetrue时,才会在加载这些plist文件的同时启动plist所形容的服务

上面开始编写plist文件并加载运行,重启验证

plist文件

通过.plist后缀结尾的xml文件来定义一个服务

创立一个配置文件/Library/LaunchDaemons/com.example.plist

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0">  <dict>    <key>Label</key>    <string>com.example</string>    <key>KeepAlive</key>    <true/>    <key>ProcessType</key>    <string>Background</string>    <key>ProgramArguments</key>    <array>      <string>sh</string>      <string>-c</string>      <string>while true; do pwd && date && sleep 1; done</string>    </array>    <key>UserName</key>    <string>root</string>    <key>GroupName</key>    <string>wheel</string>    <key>StandardErrorPath</key>    <string>/var/log/com.example.log</string>    <key>StandardOutPath</key>    <string>/var/log/com.example.log</string>    <key>WorkingDirectory</key>    <string>/Applications/example</string>  </dict></plist>

标签阐明

  • Label服务的名称,值是全局惟一的,并且值须要与.plist的文件名称局部保持一致,label须要以com.结尾
  • Keepalive: 决定程序是否须要始终运行,如果是false 则须要时才启动,默认false
  • ProcessType: 程序类型,零碎会根据不同的程序类型利用不同的资源限度策略,次要是限度CPU和磁盘I/O带宽,可选项如下

    • Backgroud: 通常是执行用户没有间接申请的过程,利用的资源限度旨在避免毁坏用户体验,资源限度最严格
    • Standard: 默认值,规范限度
    • Adaptive: 基于XPC连贯上的流动限度资源
    • Interactive: 交互式级别的限度,相当于app程序的资源限度,能够领有最高的性能,所以根本是无限度资源
  • ProgramArguments:程序的参数信息,蕴含程序调用的命令,命令的参数等信息
  • UserName: 程序运行用户权限
  • GroupName: 程序运行用户组
  • StandardErrorPath: 规范谬误输入日志的门路,如果不配置则无输入
  • StandardOutPath: 规范输入日志的门路,如果不配置则无输入
  • WorkingDirectory: 指定程序的工作目录地位,如果是放在/Library/LaunchDaemons/的文件,默认工作目录是/配置实现该参数之后须要保障该门路存在,否则服务不能失常启动

服务加载运行

加载服务配置

$ launchctl load /Library/LaunchDaemons/com.example.plist

卸载服务配置,如果配置批改之后,须要先卸载配置,而后再次执行加载

$ launchctl unload /Library/LaunchDaemons/com.example.plist

启动进行服务,因为服务配置Keepalive=true服务执行load之后就间接开始运行了,启动进行命令不失效

$ launchctl start com.example.plist$ launchctl stop com.example.plist

查看服务运行状态,共三列数据

$ launchctl list|grep com.example9919    0    com.example
  • 第一列示意过程PID,有值示意过程正在运行中
  • 第二列示意执行之后的返回码,如果是0示意执行无报错
  • 第三列示意服务的Label

根据配置StandardErrorPath/StandardOutPath查看日志输入

$ tail -f /var/log/com.example.log /Applications/exampleMon Jun 19 16:31:01 CST 2023/Applications/exampleMon Jun 19 16:31:02 CST 2023/Applications/exampleMon Jun 19 16:31:03 CST 2023/Applications/exampleMon Jun 19 16:31:04 CST 2023/Applications/exampleMon Jun 19 16:31:05 CST 2023

能够重启设施之后持续校验服务是否开机自启动

参考浏览

macOS服务治理 – launchd、launchctl、brew services详解

launchd.plist man page