如何将 Linux 的多行 crontab 定时任务写在一个 macos 的 launchctl 控制的 plist 里面?

2021-12-23 20:49:32 +08:00
 mylovesaber

我写了一个自动更新 github host 的脚本,它往 /etc/crontab 里面写了三个功能:

*/30 * * * * root /usr/bin/bash /usr/bin/hosts-tool run
* * */3 * * root /usr/bin/bash /usr/bin/hosts-tool updatefrom gitee
* * */10 * * root /usr/bin/bash /usr/bin/hosts-tool rmlog

我想兼容一下 macos ,网上查了下,crontab 在 macos 中被弃用了,一律是 launchd 控制,类似 systemd 这种东西,然后编写可用的 plist 的话,网上查了半天好像都只有单个例子,对于一个程序有多个功能选项且不同功能选项的运行定时不同的情况,网上好像没找到写法,如果按照单个例子写的话得写三个 plist ,有没有办法可以把三个功能写在一个 plist 中啊?新手刚接触 macos 还不太熟,先谢过各位了 脚本:

https://github.com/mylovesaber/auto_update_github_hosts

1809 次点击
所在节点    macOS
7 条回复
EnochZack
2021-12-23 21:22:08 +08:00
你可以写进一个脚本里,再写一个 plist 执行这个脚本
minsheng
2021-12-23 22:06:10 +08:00
StartCalendarInterval <dictionary of integers or array of dictionary of
integers>
This optional key causes the job to be started every calendar interval as
specified. Missing arguments are considered to be wildcard. The semantics
are much like crontab(5). Unlike cron which skips job invocations when
the computer is asleep, launchd will start the job the next time the com-
puter wakes up. If multiple intervals transpire before the computer is
woken, those events will be coalesced into one event upon wake from
sleep.
minsheng
2021-12-23 22:06:24 +08:00
RayGZJ
2021-12-23 23:27:59 +08:00
试试 Apple Script ?
minamike
2021-12-24 01:09:51 +08:00
mac 的 crontab 还是能用的啊
mylovesaber
2021-12-24 01:14:48 +08:00
@minamike 是的,我因为信了网上说的苹果官方有意弃用 crontab 才折腾查找老半天,最终发现原来 launchd 就目前的功能性来看,是从根本上无法替代我这种用法下的 crontab ,为了实现相同功能,launched 需要写三百行左右的规则才能替代 crontab ,但一旦需要改动时间的话,等于推倒重来,然后直接 `echo "*/30 * * * * root /usr/bin/bash /usr/bin/hosts-tool run" >> /var/at/tabs/root` 就可以了。。。。
IgniteWhite
2021-12-24 01:37:34 +08:00
你就原样写到 mac 的 crontab 就好。mac 的 cronjob 只是由 launchd 接管,但是还在。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/824088

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX