V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
mylovesaber
V2EX  ›  macOS

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

  •  
  •   mylovesaber · 2021-12-23 20:49:32 +08:00 · 1800 次点击
    这是一个创建于 848 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我写了一个自动更新 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

    7 条回复    2021-12-24 01:37:34 +08:00
    EnochZack
        1
    EnochZack  
       2021-12-23 21:22:08 +08:00 via Android
    你可以写进一个脚本里,再写一个 plist 执行这个脚本
    minsheng
        2
    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
        3
    minsheng  
       2021-12-23 22:06:24 +08:00
    RayGZJ
        4
    RayGZJ  
       2021-12-23 23:27:59 +08:00 via iPhone
    试试 Apple Script ?
    minamike
        5
    minamike  
       2021-12-24 01:09:51 +08:00 via iPhone
    mac 的 crontab 还是能用的啊
    mylovesaber
        6
    mylovesaber  
    OP
       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
        7
    IgniteWhite  
       2021-12-24 01:37:34 +08:00
    你就原样写到 mac 的 crontab 就好。mac 的 cronjob 只是由 launchd 接管,但是还在。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1078 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 23:13 · PVG 07:13 · LAX 16:13 · JFK 19:13
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.