Plist 模板

常用场景的 LaunchAgent 起步模板。下载 plist 或复制 XML,修改路径和 Label 后再加载。

用户代理(User Agent):保存到 ~/Library/LaunchAgents/,执行 launchctl load ~/Library/LaunchAgents/your.plist。请将 com.example.* 改为你自己的反向 DNS 标识。

登录时运行脚本

User Agent · RunAtLoad

下载

加载代理时运行一次——适合登录时执行并退出的初始化脚本。

<?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.run-at-login</string>
	<key>ProgramArguments</key>
	<array>
		<string>/bin/sh</string>
		<string>-c</string>
		<string>/path/to/your/script.sh</string>
	</array>
	<key>RunAtLoad</key>
	<true/>
	<key>KeepAlive</key>
	<false/>
</dict>
</plist>

每日定时

User Agent · StartCalendarInterval

下载

每天 09:00 运行。可调整 Hour、Minute,或添加 Weekday 实现每周任务。

<?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.daily-schedule</string>
	<key>ProgramArguments</key>
	<array>
		<string>/bin/sh</string>
		<string>-c</string>
		<string>/path/to/your/backup.sh</string>
	</array>
	<key>StartCalendarInterval</key>
	<dict>
		<key>Hour</key>
		<integer>9</integer>
		<key>Minute</key>
		<integer>0</integer>
	</dict>
	<key>StandardOutPath</key>
	<string>/tmp/com.example.daily-schedule.log</string>
	<key>StandardErrorPath</key>
	<string>/tmp/com.example.daily-schedule.log</string>
</dict>
</plist>

间隔执行

User Agent · StartInterval

下载

每 3600 秒(1 小时)运行一次。RunAtLoad 为 true 时加载后也会立即执行一次。

<?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.interval-job</string>
	<key>ProgramArguments</key>
	<array>
		<string>/bin/sh</string>
		<string>-c</string>
		<string>/path/to/your/sync.sh</string>
	</array>
	<key>StartInterval</key>
	<integer>3600</integer>
	<key>RunAtLoad</key>
	<true/>
	<key>StandardOutPath</key>
	<string>/tmp/com.example.interval-job.log</string>
	<key>StandardErrorPath</key>
	<string>/tmp/com.example.interval-job.log</string>
</dict>
</plist>

常驻进程

User Agent · KeepAlive

下载

保持长驻进程运行——开发服务器、worker 或本地 API。进程退出后 launchd 会自动重启。

<?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.keepalive-process</string>
	<key>ProgramArguments</key>
	<array>
		<string>/usr/local/bin/node</string>
		<string>server.js</string>
	</array>
	<key>WorkingDirectory</key>
	<string>/path/to/your/project</string>
	<key>KeepAlive</key>
	<true/>
	<key>RunAtLoad</key>
	<true/>
	<key>StandardOutPath</key>
	<string>/tmp/com.example.keepalive-process.log</string>
	<key>StandardErrorPath</key>
	<string>/tmp/com.example.keepalive-process.log</string>
</dict>
</plist>

监听路径

User Agent · WatchPaths

下载

当监听目录中的文件发生变化时运行脚本——适用于配置热重载或简单文件触发。

<?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.watch-path</string>
	<key>ProgramArguments</key>
	<array>
		<string>/bin/sh</string>
		<string>-c</string>
		<string>/path/to/your/reload.sh</string>
	</array>
	<key>WatchPaths</key>
	<array>
		<string>/path/to/watch</string>
	</array>
	<key>RunAtLoad</key>
	<false/>
	<key>StandardOutPath</key>
	<string>/tmp/com.example.watch-path.log</string>
	<key>StandardErrorPath</key>
	<string>/tmp/com.example.watch-path.log</string>
</dict>
</plist>