[CI/CD翻译]如何使用 launchd 来运行 macOS 中的服务

1,934 阅读4分钟

介绍管理macOS服务的方法和实例。

原文链接:medium.com/swlh/how-to…

原文作者:medium.com/@kosalasana…

发布时间:2019年5月30日 - 5分钟阅读

image.png

在计算机领域,launchd是一个统一的操作系统服务管理框架,在macOS中启动、停止和管理守护程序、应用程序、进程和脚本。它与Mac OS X Tiger一起引入,并在Apache许可证下获得许可。—— Wikipedia

如果你熟悉任何版本的Linux,你肯定与cron作业打过交道。基本上, launchd就是macOS中的cron。除了执行cron式的脚本之外,launchd还能做很多事情。像Linux上的systemd一样,launchd是很多老式Unix工具的替代品,比如init、inetd、cron等。

在它的核心部分,launchd区分了守护进程和代理。守护进程是全系统的服务,总是在后台运行,而代理描述的是在用户特定事件中执行的常规服务。守护程序和代理程序由 launchd 框架管理,可以使用 launchctl 终端命令进行控制。

在 launchd 服务的创建过程中,有两个主要的组成部分,你需要遵循。

  1. 任务定义。在特殊的XML文件中定义服务。
  2. 操作。使用命令行工具控制该服务。

📋 工作定义

守护进程或代理的行为被定义在一个特殊的XML文件中,称为属性列表(.plist)文件。根据它的存储位置,它将被视为一个守护进程或代理。服务(守护进程和代理)被保存为.plist文件,可以根据你的要求存放在以下文件夹中。

图片来源:launchd.info

launchd在属性列表文件中支持超过36个不同的配置键,下面只描述了基本服务所需的主要键。( 来自 launchd.plist(5) manual)

Label <string> 
     This required key uniquely identifies the job to launchd.Program <string> 
     This key defines what to start. If this key is missing, then the first element of the array of strings provided to the ProgramArguments will be used instead.  This key is required in the absence of the ProgramArguments key.ProgramArguments <array of strings> 
     Use this one if your executable requires command line options. This key is required in the absence of the Program key.RunAtLoad <boolean> 
     This optional key is used to control whether your job is launched once at the time the job is loaded. The default is false.StartInterval <integer> 
     This optional key causes the job to be started every N seconds.  If the system is asleep, the job will be started the next time the computer wakes up.  If multiple intervals transpire before the computer is woken, those events will be coalesced into one event upon wake from sleep.StartCalendarInterval <dictionary of integers or array of dictionary of integers> 
     This optional key causes the job to be started every calendar interval as specified. Missing arguments are considered to be wildcard. The semantics are much like crontab(5) .

  Minute <integer> 
     The minute on which this job will be run.
  Hour <integer> 
     The hour on which this job will be run.
  Day <integer> 
     The day on which this job will be run.
  Weekday <integer> 
     The weekday on which this job will be run (0 and 7 are Sunday).
  Month <integer> 
     The month on which this job will be run.

请参考 launchd.plist(5) 手册或 launchd.info 了解属性列表文件中其他键的详细定义。

⚠️请注意:属性列表文件的名称应以".plist "结尾。另外,请注意,launchd属性列表文件被命名为<Label>.plist是预期的惯例。因此,举例来说,如果你的工作标签是 "org.wso2.am",你的plist文件应该命名为 "org.wso2.am.plist"。

⚙ 操作

本节将定义如何获取 launchd 服务的信息以及如何加载、卸载、启动和停止这些作业。所有这些都可以通过命令行工具 launchctl 来完成。

launchctl 与 launchd 对接,以加载、卸载守护进程/代理,并普遍控制 launchd。 launchctl 支持在命令行、交互式或甚至从标准输入重定向接收子命令。- launchctl(1)手册

launchctl中的一些常用命令在下文中定义。

  • 获取可用(加载)作业的信息 :
$ launchctl list
  • 获取某个作业的信息。
$ launchctl list | grep <LABEL>
  • 加载一个作业(全局守护程序)。
$ launchctl load /Library/LaunchDaemons/<LABEL>.plist
  • 卸载一个作业(一个全局守护进程)。
$ launchctl unload /Library/LaunchDaemons/<LABEL>.plist
  • 启动一个作业(一个已加载的作业)。
$ launchctl start <LABEL>
  • 停止一项工作(已加载的工作)。
$ launchctl stop <LABEL>
  • 重新启动一个作业(一个已加载的作业)。
$ launchctl restart <LABEL>

⚠️请注意:如果你正在使用Daemon作业,请在上述命令中使用sudo权限。

🎗简单的例子(使用WSO2 API管理器

在这篇文章中,我希望在macOS系统上运行一个WSO2 API Manager服务器作为服务,作为一个例子来理解上述解释的方法。WSO2 API Manager解决了完整的API生命周期管理、货币化和政策执行问题,它被评为The Forrester Wave™中的领导者。API管理解决方案,2018年第四季度报告中被评为领导者。

  1. 从WSO2官方网站下载最新的WSO2 API Manager发行版。
  2. 提取API Manager压缩包并进入WSO2 API Manager目录。( 我们将此目录的路径称为<PRODUCT_HOME>)
  3. 下载并安装OpenJDK 8或Oracle JDK 1.8.*并设置JAVA_HOME环境变量。
  4. 在"/Library/LaunchDaemons/"创建一个名为 "org.wso2.am.plist "的属性文件。用文本编辑器打开 "org.wso2.am.plist "文件,将以下内容复制到属性列表(.plist)文件中。请将<PRODUCT_HOME>替换为产品目录的路径。
<?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>org.wso2.am</string>
    <key>ServiceDescription</key>
    <string>WSO2 API Manager Server</string>
    <key>ProgramArguments</key>
    <array>             
        <string><PRODUCT_HOME>/bin/wso2server.sh</string>
    </array>
    <key>RunAtLoad</key>
    <false/>
</dict>
</plist>
  1. 运行以下命令,使用 launchctl 工具加载并启动 WSO2 API Manager 作为服务。
$ sudo launchctl load /Library/LaunchDaemons/org.wso2.am.plist
$ sudo launchctl start org.wso2.am
  1. 要检查作业的状态,请运行以下命令,你将能够观察到图像中给出的有关作业的需要信息。
$ sudo launchctl list | grep org.wso2.am

image.png

WSO2 API Manager作为一个服务运行,PID为27787

  1. 运行以下命令,停止运行中的进程,并从 launchd 框架中卸载守护服务。
$ sudo launchctl stop org.wso2.am
$ sudo launchctl unload /Library/LaunchDaemons/org.wso2.am.plist

因此,通过对launchd框架的简单例子的理解,现在你可以使用launchd来创建脚本,做一些事情,比如清理文件,按计划控制应用服务器,或者在某个文件出现时运行一个应用程序。

干杯!! 🍺🍺

关于 launchd 框架和 launchctl 工具的更多细节。

www.launchd.info

www.manpagez.com/man/5/launc…

www.manpagez.com/man/1/launc…


www.deepl.com