Skip to content

macOS 运维

我们有一台 Mac Mini M4 Pro,它是一台非常强大的服务器。本文记录 macOS 上的运维经验。很多资料来自 Apple 官方开发者网站:Apple Developer

launchd

Quote

launchd 是 MacOS 的服务管理器,类似于 Linux 的 systemd(它们都是系统启动时内核运行的第一个进程)。

  • luanchd 服务分为两类:

    服务类型 描述 目录
    Daemon 系统级别的服务,不需要用户登录就可以运行 /System/Library/LaunchDaemons/Library/LaunchDaemons
    Agent 用户服务,需要用户登录才能运行 ~/Library/LaunchAgents
  • launchd 的命令行工具是 launchctl,常用命令有:

    sudo launchtl load /Library/LaunchDaemons/otelcol-contrib.plist
    sudo launchctl start otelcol-contrib
    sudo launchctl list | grep otelcol-contrib
    sudo launchctl stop otelcol-contrib
    sudo launchctl unload /Library/LaunchDaemons/otelcol-contrib.plist
    
  • 服务配置文件是 plist 格式的文件。以 OpenTelemetry 为例,配置文件如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
            <key>Label</key>
            <string>otelcol-contrib</string>
            <key>RunAtLoad</key>
            <true/>
            <key>KeepAlive</key>
            <true/>
            <key>Program</key>
            <string>/usr/local/bin/otelcol-contrib</string>
            <key>ProgramArguments</key>
            <array>
                    <string>/usr/local/bin/otelcol-contrib</string>
                    <string>--config=/etc/otelcol-contrib/config.yaml</string>
            </array>
            <key>EnvironmentVariables</key>
            <dict>
                    <key>OTEL_BEARER_TOKEN</key>
                    <string>your_key_here</string>
                    <key>OTEL_CLOUD_REGION</key>
                    <string>zjusct-cluster</string>
            </dict>
            <key>StandardOutPath</key>
            <string>/var/log/otelcol-contrib.log</string>
            <key>StandardErrorPath</key>
            <string>/var/log/otelcol-contrib-err.log</string>
    </dict>
    </plist>
    

    完整定义见 launchd.plist(5) manual page。可以使用 plutil 命令检查 plist 文件语法是否正确。修改配置文件后需要 unloadload 服务。

  • 其他:

    • 配置为 KeepAlive 的服务会尝试保持运行,不管是异常退出还是手动停止都会重启。launchctl 没有 restart,但对于这样的服务 stop 就相当于 restart

文件系统

macOS 的文件系统布局与 UNIX 有一些显著的不同:

其他:

  • 同样使用 /etc/fstab 挂载 NFS,但参数有所不同,比如不支持 default 选项。

网络