我应该将这个 cronjob 转换为 launchctl plist。
这可能吗?
0 8-18 * * 1-5 /usr/bin/curl -s --data "par1=a&par2=b&press=OK" http://mydomain.com/file.php /dev/null
答案1
基本上,您可以将这样的属性列表保存为,然后通过注销并重新登录来~/Libary/LaunchAgents/com.superuser.408777.plist
加载它。launchctl load ~/Libary/LaunchAgents/com.superuser.408777.plist
<?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>com.superuser.408777</string>
<key>ProgramArguments</key>
<array>
<string>curl</string>
<string>--data</string>
<string>par1=a&par2=b&press=OK</string>
<string>http://mydomain.com/file.php</string>
</array>
<!-- <key>Program</key>
<string>/absolute/path/to/script</string> -->
<key>StartCalendarInterval</key>
<dict>
<key>Weekday</key>
<integer>1</integer>
<key>Hour</key>
<integer>8</integer>
</dict>
<dict>
<key>Weekday</key>
<integer>1</integer>
<key>Day</key>
<integer>9</integer>
</dict>
<!-- <key>StartInterval</key>
<integer>3600</integer> --> this would just run the program arguments every hour
<key>KeepAlive</key>
<true/>
</dict>
</plist>
据我所知,除了单独列出每个组合外,无法指定日期范围,如 8-18 小时和 1-5 天。不过,您可以每小时运行一次外部脚本,并根据日期退出。
weekday=$(date +%w)
hour=$(date +%H)
[[ $weekday -le 0 || $weekday -ge 6 || $hour -le 7 || $hour -ge 19 ]] && exit
curl -s --data "par1=a&par2=b&press=OK" http://mydomain.com/file.php /dev/null