当代计算机系统中,定时任务是一种非常有用的工具,用于自动执行特定的任务或脚本。在 macOS 操作系统中,有几种不同的方法来设置和管理定时任务,其中 launchdlaunchctl 是两个强大的工具。

目录

[TOC]


1. 介绍 launchdlaunchctl

在 macOS 中,launchd 是一个统一的服务管理框架,用于启动、停止和管理守护进程、应用程序、进程和脚本等。而 launchctl 则是用于与 launchd 进行交互的命令行工具,它允许用户创建、加载、卸载和管理定时任务。这两个工具的强大之处在于它们可以让你根据自己的需求灵活地设置定时任务,无论是在系统启动时执行还是在用户登录后执行。

launchd由两部分组成:

  • launchd, 该工具主要有两个功能:
  • 启动系统

    • 加载和维护服务
  • launchctl: 是 launchd 提供的用于对用户交互的工具

对于 launchd 来说, 每一个 plist 文件即为一个任务. 系统启动时, launchd 会加载 /System/Library/LaunchDaemons/Library/LaunchDaemons 中的所有 plist 文件, 用户登录后, 会扫描 /System/Library/LaunchAgents, /Library/LaunchAgents, ~/Library/LaunchAgents 这三个目录的文件并加载它们

2.plist 放在不同位置时的区别

对于 launchd 来说, 有如下五个路径的 plist 文件会被读取加载, 他们被触发的时机并不相同, 总结如下:

位置类型以什么用户权限运行运行时机Provided
/System/Library/LaunchDaemonsSystem Daemonsroot / 指定用户开机时Apple
/System/Library/LaunchAgentsSystem Agents当前登录用户任意用户登录Apple
/Library/LaunchDaemonsGlobal Daemonsroot / 指定用户开机时Administrator
/Library/LaunchAgentsGlobal Agents当前登录用户任意用户登录Administrator
~/Library/LaunchAgentsUser Agents当前登录用户指定用户登录时User

总的来说, LaunchDaemonsLaunchAgents 主要有以下两个区别:

  1. 运行时机

    • LaunchDaemons 在按下开机按钮后, 用户还未输入密码时, 就已经运行了.
    • LaunchAgents 在用户输入密码后, 才开始运行.
  2. 运行用户

    • LaunchDaemons 是以 root / 其他指定用户运行
    • LaunchAgents 是以当前登录用户的权限运行

3. 使用 launchctl 创建定时任务

步骤一:准备工作

在开始创建定时任务之前,确保你已经完成了以下准备工作:

  • 如果你希望直接运行程序,可以跳过这一步。如果你计划执行自己的脚本或任务,确保它已经编写完毕。

步骤二:编写定时任务配置文件

launchctl 根据 .plist 配置文件来启动任务。这些配置文件可以存放在不同的目录中,具体取决于任务的类型和执行时机。

以下是一个示例 .plist 配置文件的内容:

<?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>com.example.mytask.plist</string>
    <!-- 指定使用命令的方式运行,参数和命令行相同,每一段为字符串数组的一项 -->
    <key>ProgramArguments</key>
    <array>
      <string>/usr/bin/python</string>
      <string>/Users/user/myscript.py</string>
    </array>
    <!-- 指定要定时执行的时间 -->
    <key>StartCalendarInterval</key>
    <dict>
      <key>Minute</key>
      <integer>0</integer>
      <key>Hour</key>
      <integer>3</integer>
    </dict>
    <!-- 标准输出日志文件 -->
    <key>StandardOutPath</key>
    <string>/Users/user/mytask.log</string>
    <!-- 标准输出错误日志文件 -->
    <key>StandardErrorPath</key>
    <string>/Users/user/mytask.err</string>
  </dict>
</plist>

在配置文件中,你可以指定任务的标识符(Label)、要运行的程序或脚本(ProgramArguments)、执行时间(StartCalendarInterval),以及标准输出和错误日志的路径。

下面是一个最简化版本的plist配置文件

<?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>com.example.anyName</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Path/to/your/Executable/file</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>

4. 加载和管理定时任务

使用 launchctl 命令来加载、管理和操作定时任务。以下是一些常用的命令示例:

  • 加载任务(使用 -w 选项将 .plist 文件中无效的键覆盖掉):

    $ launchctl load -w com.example.mytask.plist
  • 删除任务:

    $ launchctl unload -w com.example.mytask.plist
  • 查看任务列表(使用 grep 过滤):

    $ launchctl list | grep 'com.example'
  • 开始任务:

    $ launchctl start com.example.mytask.plist
  • 结束任务:

    $ launchctl stop com.example.mytask.plist

4. 定时任务配置示例

在上述示例中,我们介绍了如何创建一个定时任务,让系统每天凌晨3点运行一个 Python 脚本。但是,launchctl 支持多种配置方式,可以根据需要设置不同的执行时间,包括分钟、小时、天、星期和月份。

以下是一些常见的示例:

  • 每天晚上10点执行任务:

    <key>StartCalendarInterval</key>
    <dict>
      <key>Minute</key>
      <integer>0</integer>
      <key>Hour</key>
      <integer>22</integer>
    </dict>
  • 每隔30分钟执行一次任务:

    <key>StartInterval</key>
    <integer>1800</integer> <!-- 单位为秒 -->

5. 注意事项和扩展

  • 当配置定时任务时,注意确保相关的程序或脚本已经准备好,并且路径设置正确。
  • launchctl 支持秒级的定时设置,相比之下,cron 只能精确到分钟级。
  • 可以在不同的目录中存放 .plist 配置文件,根据需要选择合适的目录,例如 ~/Library/LaunchAgents/Library/LaunchDaemons
  • 如果定时任务涉及网络操作,注意如果电脑处于睡眠状态,任务可能无法执行。此时,可以定时唤醒屏幕来解决。

结语

总结来说,launchdlaunchctl 是 macOS 中强大的定时任务管理工具,允许你根据自己的需求创建和管理定时任务。通过编写 .plist 配置文件,你可以指定任务的标识、执行程序、执行时间等参数,实现灵活的定时任务管理。

Last modification:September 22, 2023
V50%看看实力