在大多数现代 UNIX 上,cron 支持以下位置的单个 crontab 文件:/etc/cron.d
,其中每个任务都以指定用户的身份执行。这些文件编辑后会自动更新 crontab。它们允许各个软件包安装自己的自动化任务,而不会污染全局 crontab 或使用特定于用户的 crontab。
OSX 似乎不支持这个 \xe2\x80\x94 是真的吗?
\n\n如果是这样,实施它的最佳方法是什么?由于我在 OSX 上进行开发,但在 Linux 上运行生产代码,因此我需要使用 crond 而不是 launchd,即使后者具有潜在的好处。
\n在 OS X 上,您应该使用 launchd。为了实现它,我将用一个例子来解释。
转到文件夹/Users/your-username/Library/LaunchAgents
并保存以下 plist 文件。我命名了它com.username.testscript.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.your-username.testscript</string>
<key>ProgramArguments</key>
<array>
<string>/Users/your-username/bin/testscript.sh</string>
</array>
<key>Nice</key>
<integer>1</integer>
<key>StartInterval</key>
<integer>60</integer>
<key>RunAtLoad</key>
<true/>
<key>StandardErrorPath</key>
<string>/tmp/com.your-username.testscript.err</string>
<key>StandardOutPath</key>
<string>/tmp/com.your-username.testscript.out</string>
</dict>
</plist>
Run Code Online (Sandbox Code Playgroud)
该文件非常不言自明。它将每 60 秒启动一次命令/Users/your-username/bin/testscript.sh
,将在加载时启动,将保存错误/tmp/com.username.testscript.err
并登录/tmp/com.username.testscript.out
。
您还可以使用目录/Library/LaunchAgents/
或/Library/LaunchDaemos/
.