V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
mylovesaber
V2EX  ›  macOS

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

  •  
  •   mylovesaber · Dec 23, 2021 · 2607 views
    This topic created in 1586 days ago, the information mentioned may be changed or developed.

    我写了一个自动更新 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 replies    2021-12-24 01:37:34 +08:00
    EnochZack
        1
    EnochZack  
       Dec 23, 2021 via Android
    你可以写进一个脚本里,再写一个 plist 执行这个脚本
    minsheng
        2
    minsheng  
       Dec 23, 2021
    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  
       Dec 23, 2021
    RayGZJ
        4
    RayGZJ  
       Dec 23, 2021 via iPhone
    试试 Apple Script ?
    minamike
        5
    minamike  
       Dec 24, 2021 via iPhone
    mac 的 crontab 还是能用的啊
    mylovesaber
        6
    mylovesaber  
    OP
       Dec 24, 2021
    @minamike 是的,我因为信了网上说的苹果官方有意弃用 crontab 才折腾查找老半天,最终发现原来 launchd 就目前的功能性来看,是从根本上无法替代我这种用法下的 crontab ,为了实现相同功能,launched 需要写三百行左右的规则才能替代 crontab ,但一旦需要改动时间的话,等于推倒重来,然后直接 `echo "*/30 * * * * root /usr/bin/bash /usr/bin/hosts-tool run" >> /var/at/tabs/root` 就可以了。。。。
    FurN1
        7
    FurN1  
       Dec 24, 2021
    你就原样写到 mac 的 crontab 就好。mac 的 cronjob 只是由 launchd 接管,但是还在。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5592 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 64ms · UTC 07:26 · PVG 15:26 · LAX 00:26 · JFK 03:26
    ♥ Do have faith in what you're doing.